October 2023
Intermediate to advanced
384 pages
9h 12m
English
Consider the following boldfaced change to our program that calculates a list of Fibonacci numbers:
(declare fib)
(defn fib-w [n]
(cond
(< n 1) nil
(<= n 2) 1
:else (+ (fib (dec n)) (fib (- n 2)))))
(def fib (memoize fib-w))
(defn lazy-fibs []
(map fib (rest (range)))
)
The lazy-fibs function may look a little strange to you. Let’s walk through it. You already understand the map function. The rest function takes a list and returns that list without the first element. And that brings us to the range function.
The range function, as called here, returns a list of integers starting at zero. How many integers, ...
Read now
Unlock full access