this post was submitted on 16 Feb 2022
40 points (93.5% liked)

Programmer Humor

32371 readers
425 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] yogthos@lemmy.ml 0 points 2 years ago

best of both world with Lisp macros :)

(defmacro gen-fib [size]
  (let [values (->> (iterate (fn [[a b]] [b (+ a b)]) [0 1])
                    (map first)
                    (take size)
                    (vec))]
    `(defn ~'fib [~'n] (~values ~'n))))

(macroexpand-1 '(gen-fib 10))
;=> (clojure.core/defn fib [n] ([0 1 1 2 3 5 8 13 21 34] n))

(gen-fib 10)

(fib 9)
;=> 34