Reputation: 191
I learned quite a bit of scheme from SICP but am more interested in common lisp now. I know common lisp's fold
is reduce
, with special arguments for left or right folding, but what is the equivalent of unfold
? Googling has not helped much. In fact I get the impression there is no unfold???
Upvotes: 9
Views: 1388
Reputation: 26519
Common Lisp has (loop ... collect ...)
. Compare
(loop for x from 1 to 10 collect (* x x))
with its equivalence using unfold
:
(unfold (lambda (x) (> x 10)) (lambda (x) (* x x)) (lambda (x) (+ x 1)) 1)
In general, (unfold p f g seed)
is basically
(loop for x = seed then (g x) until (p x) collect (f x))
Edit: fix typo
Upvotes: 13
Reputation: 9705
The common lisp hyperspec doesn't define an unfold
function, but you can certainly write your own. Its scheme definition translates almost symbol for symbol.
Upvotes: 3