Executes a function until it returns a nil, collecting its values into a list
Posted
by
Baldur
on Stack Overflow
See other posts from Stack Overflow
or by Baldur
Published on 2011-06-28T17:33:09Z
Indexed on
2012/07/08
3:16 UTC
Read the original article
Hit count: 401
I got this idea from XKCD's Hofstadter comic; what's the best way to create a conditional loop in (any) Lisp dialect that executes a function until it returns NIL
at which time it collects the returned values into a list.
For those who haven't seen the joke, it's goes that Douglas Hofstadter's “eight-word” autobiography consists of only six words: “I'm So Meta, Even This Acronym” containing continuation of the joke: (some odd meta-paraprosdokian?) “Is Meta” — the joke being that the autobiography is actually “I'm So Meta, Even This Acronym Is Meta”. But why not go deeper?
Assume the acronymizing function META
that creates an acronym from a string and splits it into words, returns NIL
if the string contains but one word:
(meta "I'm So Meta, Even This Acronym") ? "Is Meta"
(meta (meta "I'm So Meta, Even This Acronym")) ? "Im"
(meta (meta (meta "I'm So Meta, Even This Acronym"))) ? NIL
(meta "GNU is Not UNIX") ? "GNU"
(meta (meta "GNU is Not UNIX")) ? NIL
Now I'm looking for how to implement a function so that:
(so-function #'meta "I'm So Meta, Even This Acronym")
? ("I'm So Meta, Even This Acronym" "Is Meta" "Im")
(so-function #'meta "GNU is Not Unix")
? ("GNU is Not Unix" "GNU")
What's the best way of doing this?
© Stack Overflow or respective owner