Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 43 |
Nodes: | 6 (0 / 6) |
Uptime: | 98:23:17 |
Calls: | 290 |
Files: | 905 |
Messages: | 76,483 |
Johan wrote:
I want to map over a list, but to treat the first object differently
from the rest. My previous experience with Lisp was Interlisp, which
has dynamic binding. In Interlisp I could code my problem similar to
this:
(defun foo ()
(let ((first-time t))
(mapcar #'bar '(1 2))))
(defun bar (n)
(cond
(first-time
(setf first-time nil)
(+ n 1))
(t (+ n 2))))
and (foo) would return (2 4). This doesn't work with lexical binding,
and I don't want to make first-time a global variable. What is the
best design pattern in Common Lisp?
(loop for n in '(1 2)
for x = (+ n 1) then (+ n 2)
collect x)