Odd closure issue with cond and =>
Example program:
(define procs
(map (lambda (arg)
(cond
((memq arg '(a b c)) =>
(lambda (tail)
(lambda () (format #t "tail is ~A~%" tail))))
(else
(lambda () (format #t "~A not found.~%" arg)))))
'(b c d a)))
(for-each (lambda (f) (f)) procs)
outputs:
tail is (a b c)
tail is (a b c)
a not found.
tail is (a b c)
Expected (And verified with guile and chicken):
tail is (b c)
tail is (c)
d not found.
tail is (a b c)
All the new functions returned by cond seem to be using the same values of the very last invocation of the mapped function, instead of capturing copies of each individual element.