I am in the process of learning Clojure. I am fairly new to functional programming and would like to know if my code smells or if there are any performance implications with my approach.
; Returns the the given sequence with the given item appended to it.
(defn snoc [xs x] (concat xs [x]))
; Returns the Fibonacci sequence up to the highest number less than max.
(defn fib [max]
(loop [a 1, b 1, acc [1]]
(if (> b max)
acc
(recur b (+ a b) (snoc acc b)))))
; Project Euler Problem 2: Attempt A
(defn pe2a []
(reduce +
(filter even? (fib 4000000))))
For reference:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.