Subject: [clisp-list] Named let?

From: "Dave Richards" <[email-masked]>
Date: Thu Oct 3 21:26:02 2002

Scheme has a construct called a named let, which in addition to a normal
let/let* construct arranges for a label to be created around the construct
such that it can be invoked inside the construct.  This was a very elegant
and tight construct for recursive loops, e.g.:

	(let loop ((v-1 init-1)
	           (v-2 init-2))
	  ...
	  (loop (cdr v-1) (cdr v-2)))

This can easily be converted to:

	(labels
       ((loop (v-1 v-2)
          ...
          (loop (cdr v-1) (cdr v-2))))
	 (loop v-1 v-2))

I know, but does ANSI LISP provide such a macro/construct?  It's one of
those "I'd use it if it existed" things.  I don't see such a cnstruct, but I
thought I'd ask in case it's buried inside the spec somewhere.

	Thanks!

	Dave

---

From: Marco Antoniotti <[email-masked]>
Date: Fri Oct 4 09:05:11 2002

> From: "Dave Richards" <[email-masked]>
> X-Priority: 3 (Normal)
> X-MSMail-Priority: Normal
> Importance: Normal
> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
> X-OriginalArrivalTime: 04 Oct 2002 04:25:29.0250 (UTC) FILETIME=[11FA2820:01C26B5E]
> Sender: [email-masked]
> X-Original-Date: Thu, 3 Oct 2002 21:25:27 -0700
> Date: Thu, 3 Oct 2002 21:25:27 -0700
> X-UIDL: NJo"!!g5!!RP/!!JL<"!
> 

ANSI CL does not provide it.  However, it is just one macro away.

    (defmacro named-let (name bindings &body forms)
      `(labels ((,name ,(mapcar #'first bindings)
		       ,@forms))
	 (,name ,@(mapcar #'second bindings))))

Your example will become

     (named-let looping ((x 1))
	   (when (> x 10)
	     (return-from loop 1234))
	   (print x)
	   (looping (1+ x)))

Note that it is not a good thing to name the NAMED-LET "LOOP" in
CL (hence the LOOPING), as you may have some "real" call to the CL
LOOP macro in the body.  This should come to no surprise to the
schemer accustomed at single namespace, no packages environments.

Just to be paranoid

    (defmacro named-let (name bindings &body forms)
      (when (string-equal name "LOOP")
        (warn "NAMED-LET called with name \"LOOP\":~@
               this may conflict with the ANSI LOOP macro."))
      `(labels ((,name ,(mapcar #'first bindings)
		       ,@forms))
	 (,name ,@(mapcar #'second bindings))))

Cheers

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
715 Broadway 10th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.

---

From: Arseny Slobodjuck <[email-masked]>
Date: Fri Oct 4 20:13:02 2002

Hi,

> schemer accustomed at single namespace, no packages environments.

Why do you call it looping ? Isn't it a recursion ?

-- 
Best regards,
 Arseny

---

From: "Dave Richards" <[email-masked]>
Date: Fri Oct 4 20:55:02 2002

> Why do you call it looping ? Isn't it a recursion ?

I was a Scheme programmer before I was a Common LISP programmer.  Since
Scheme is guarenteed to be properly tail recursive I quickly found that a
named let was really the only construct I ever used for iteration/recursion,
i.e. I stopped thinking about which control behavior wss being exhibited.

Yes, properly speaking it is recursion, but it acts like a loop.  I fear I
will *never* feel comfortable with loop.  It's big and bulky and clumsy and
I don't have a feel for it.  I think I need to buy "LOOP For Dummies", which
includes examples and test questions at the end of each chapter. :)

	Dave

---

From: Pascal Bourguignon <[email-masked]>
Date: Fri Oct 4 21:12:02 2002

> Date: Fri, 4 Oct 2002 20:54:31 -0700
> 

The problem with loop is that it's not lisp.

    ;; Common-Lisp code here
    (fun arg arg arg)
    
    (loop ;; loop code here
       
       for i from 0 to 10
       collect i into reslist
       do

            ;; Common-Lisp code here
            (print i)

       ;; loop code from here
       finally return reslist)

    ;; Common-Lisp code here   

Otherwise, one can get accustomed to it.

-- 
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
 The name is Baud,...... James Baud.

---

From: Arseny Slobodjuck <[email-masked]>
Date: Sat Oct 5 06:10:03 2002

Hello Dave,

> includes examples and test questions at the end of each chapter. :)

I found CL loop macro outstanding, greatest loop of all programming
languages and urge you to love it too! Not that I know all the details
of it though. "Nonfunctional", loop has it's own theory and practice.
And there is no need to buy a book - try to start with CLTL2,
Loop, Iteration control (26.6). Seven for/as clauses - the core of
loop as I see it.

There is examples in chapter, but no test questions ;)

-- 
Best regards,
 Arseny