Question

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???

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top