bobbicodes / bobbi-lisp

Interactive Lisp environment for learning Clojure
https://bobbicodes.github.io/bobbi-lisp/
0 stars 0 forks source link

Implement `recur` with `fn` target #22

Closed bobbicodes closed 11 months ago

bobbicodes commented 11 months ago

Proof:

(defn myfn [s res]
  (if (empty? s) res
    (recur (rest s) (conj res (first s)))))

(myfn (range 10) [])
=> [0 1 2 3 4 5 6 7 8 9] 

(defn myfn
  ([s] (myfn s []))
  ([s res]
  (if (empty? s) res
    (recur (rest s) (conj res (first s))))))

(myfn (range 10))
=> [0 1 2 3 4 5 6 7 8 9]

(defn factorial
   ([n] (factorial n 1))
   ([n accumulator]
    (if (zero? n)
      accumulator
      (recur (dec n) (* accumulator n)))))

(factorial 5)
=> 120 
bobbicodes commented 11 months ago

Closes: #9