;; Delete list item in scheme

;; The easy way:
(import srfi-1)
(delete 'item list)

;; Or we can sorta re-invent the wheel the following ways:

;; NOTE: named let, e.g. (let loop ...), is nice. We use this a lot for loops,
;; where you don't want to go defining a whole helper function with `define`.
;; It sorta lets you logically think about it less like 'function + helper
;; function' and more like 'function + iteration construct' even though this
;; sorta just syntax sugar.

;; NOTE: In chicken scheme, to know what something expands to, you can use ,x
;; on the REPL. So, type ,x (let loop ...) to see what that would expand to -
;; for example, ,x (let loop () (loop)) - an infinite loop that does nothing.
;; Sometimes it's ugly, or using weird internal constructs. Sometimes it's
;; actually readable. Have fun.

;; This technically uses iterative recursion (the last call in the let loop is
;; loop itself), which can be better optimized, but is probably less efficient
;; cause we have to reverse the whole thing at the end. Or, it could be more
;; efficient if you don't cre about the order and just remove the call to
;; reverse. Reverse is defined in srfi-1
(define (delete x lst)
   (let loop ((lst lst)
              (res '()))
     (cond ((null? lst)
           ((equal? x (car lst))
            (loop (cdr lst) res))
            (loop (cdr lst) (cons (car lst) res)))))))

;; The 'regular' recursion way
(define (delete x lst)
  (let loop ((lst lst))
    (cond ((null? lst)
          ((equal? x (car lst))
           (loop (cdr lst)))
           (cons (car lst) (loop (cdr lst)))))))