Goal: implement unfold function using only two arguments.
The arguments:
- the first argument is f which takes an initial value of some type I and returns nil or a cons pair of two elements (the first of these two is the next element that goes in the list of some type A and the next initial value again of some type I).
- The second argument is an initial value of some type I and the return is a list of items of type A.
This is what I have so far and I am not sure why it is not working:
(define (descending i)
(if (= i 0)
(list)
(cons i (- i 1))))
(define nil (list))
(define (unfold f init)
(if (eq? (f init) '())
(list)
(cons init (unfold f (f init)))))
(unfold (descending 5))
should evaluate to
'(5 4 3 2 1)
This should be the result but isn’t. What am I doing wrong?
First, it should be
(unfold descending 5). Thenfwould produce a pair and you would use both components of it,But this has awful computational complexity as it calls
(f init)three times per iteration. A humbleletbinding remedies this.And a tail recursive form using named
letAnd using
match.