This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

strange error with mapcan and quote.

2 posts · 2250 views

I get a weird behavior with sbcl when I try the following code at the repl.
 (mapcan (lambda (x) (if (evenp x) '(even))) (list 1 2 3 4))
Sbcl hangs at 100% CPU without returning an error.

Why do I get such a behavior?
Apparently, this is related to the quote '(even).
If I replace it by (list 'even), I get the expected answer (EVEN EVEN).

As far as I understand from the spec, the mapcan call should be equivalent to
(apply #'nconc (mapcar (lambda (x) (if (evenp x) '(even))) (list 1 2 3 4)))
but then this code gives a heap exhaustion. I am puzzled.

Re: strange error with mapcan and quote.

Mapcan operates destructively on the lists returned by the function. Because your function returns a literal list mapcan operates destructively on a literal object. The error will go away if you replace '(even) by (list 'even).