Fibonacci in a Few Lines
Posted on January 3, 2016
by Josh
Fibonacci Toy Example
If you’ve played around with haskell, or other typed “functional” languages, you’ve probably noticed that “pattern matching” plays a prominent role. While clojure has made a tradeoff on the whole typing debate, we can still pattern match!
Notice how nice it is to not need to do control flow with (if (= x 1) ...)
. Note, cond
would be great in this toy example, and match becomes useful in more complex examples.
(defn fib [x]
(match [x]
[1] 1
[_] (* x (fib (dec x)))))
(fib 3) ;; => 6