Deutsch   English   Français   Italiano  
<vam31l$3b0or$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Multiple arguments to mapcar?
Date: Wed, 28 Aug 2024 02:46:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <vam31l$3b0or$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Wed, 28 Aug 2024 04:46:17 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b4840cee797d6edfc6cdaa9e88ed1b49";
	logging-data="3506971"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1897g2u7Fwok76Mm3qHXnQM"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:HafepHsTkLY+ql25t1PZLcuTYus=
Bytes: 1971

Pascal Costanza wrote:

> 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)

Gauche Scheme

(use gauche.parameter)

(define first-time (make-parameter 'unset))

(define (foo)
  (parameterize ((first-time #t))
    (map bar '(400 500 600))))

(define (bar n)
  (cond
    ((first-time)
     (first-time #f)
     (+ n 1))
    (#t (+ n 2))))

gosh> (foo)
(401 502 602)
gosh> (first-time)
unset