开发者

"unfold" for common lisp?

I learned quite a bit of scheme from SICP but am more interested in common lisp no开发者_如何转开发w. 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???


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜