From: Tamas K Papp
Subject: why does tagbody return nil?
Date: 
Message-ID: <75rf1lF19cvqnU1@mid.individual.net>
I was writing some code that works best with tagbody (nope, not the
example below, that is deliberately simplified), and when I wanted to
use the value of the expression I realized that it returns nil.

My experience is that whenever I run into something in the standard
that doesn't make sense (to me), there is usually a design choice that
rationalizes it (which I usually realize later).  So what is the
reason for tagbody returning nil, and not the value of the last
expression it evaluated?  Eg

(lambda (s)
  "Find the first integer that is weakly larger than s.  The example
  is deliberately simplified, please don't propose improvements to the
  algorithm, or suggest that I use ceiling :-P"
  (let ((i 0))
    (tagbody 
     top
       (if (< i s)
	   (progn
	     (incf i)
	     (go top))
	   i))))     ; does not work, since tagbody always returns nil

I can of course work around it, but if I am using a closure, I need a
named block (or is there a better way?), like this:

(lambda (s)
  "Same purpose, with name block and return-from."
  (block closure
    (let ((i 0))
      (tagbody 
       top
	 (if (< i s)
	   (progn
	     (incf i)
	     (go top))
	   (return-from closure i))))))

Thanks,

Tamas

From: gugamilare
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <7d439b37-867a-4bb6-a892-e5ff58ecf6aa@d2g2000pra.googlegroups.com>
I don't know the reason, but can use prog instead of tagbody and use
return.

cl-user>er (lambda (s)
  "Find the first integer that is weakly larger than s.  The example
  is deliberately simplified, please don't propose improvements to the
  algorithm, or suggest that I use ceiling :-P"
  (let ((i 0))
    (prog ()
     top
       (if (< i s)
           (progn
             (incf i)
             (go top))
           (return i)))))
#<FUNCTION (lambda (s)) {10048B0039}>
cl-user> (funcall * 1.5)
2
From: budden
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <80278beb-9b3a-4b8e-b5c2-c9dab5b1c802@s38g2000prg.googlegroups.com>
prog?
From: Tamas K Papp
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <75rn9pF19o321U1@mid.individual.net>
On Wed, 29 Apr 2009 11:03:41 -0700, budden wrote:

> prog?

Thanks, but the question still remains: although prog allows return
statements, so I don't have to make an explicit block, it still gives
nil for a normal return.  I wonder things were designed that way.

Tamas
From: Pillsy
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <88e449a2-5057-473c-9675-7ffcae5ca663@c18g2000prh.googlegroups.com>
On Apr 29, 12:54 pm, Tamas K Papp <······@gmail.com> wrote:
[...]
> I can of course work around it, but if I am using a closure, I need a
> named block (or is there a better way?), like this:

> (lambda (s)
>   "Same purpose, with name block and return-from."
>   (block closure
>     (let ((i 0))
>       (tagbody
>        top
>          (if (< i s)
>            (progn
>              (incf i)
>              (go top))
>            (return-from closure i))))))

Depending on how simplified your answer function is, there is a
somewhat better way, which is to use PROG, which combines BLOCK NIL,
LET and TAGBODY all in one handy form.

(lambda (s)
  (prog ((i 0))
   top
     (when (< i s)
       (incf i)
       (go top))
     (return i)))

You'll still need a named BLOCK if you need to break through multiple
levels of TAGBODY or whatever, though.

Cheers,
Pillsy
From: ·····@franz.com
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <0ccef0d0-f970-453a-a482-8865ce970522@u9g2000pre.googlegroups.com>
On Apr 29, 9:54 am, Tamas K Papp <······@gmail.com> wrote:
> I was writing some code that works best with tagbody (nope, not the
> example below, that is deliberately simplified), and when I wanted to
> use the value of the expression I realized that it returns nil.
>
> My experience is that whenever I run into something in the standard
> that doesn't make sense (to me), there is usually a design choice that
> rationalizes it (which I usually realize later).  So what is the
> reason for tagbody returning nil, and not the value of the last
> expression it evaluated?

One possible explanation: The consistency you are looking for is due
to the consistency of other Lisp constructs that are progn-like, that
is, they proceed from start to finish and there is most definitely a
final form.  However, tagbody is not like progn, in that it is meant
to be used with tags (hence the name :-), which then imply go forms.
Because a nontrivial tagbody (one with tags and go's to those tags)
does not have a specific order, it is hard to know how to define "the
last expression", and in fact it might be an error that causes
execution to drop off the end of the tagbody.  There should be a
consistent value returned in that situation, and it seems like nil
would be the best value for it.

In other words, a tagbody that is being used like a progn isn't being
used like a tagbody, and so the defaults that apply to tagbody
shouldn't necessarily match that of progn.

Duane
From: gugamilare
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <575ebf4f-289b-4ee2-96dc-631c932221c2@u9g2000pre.googlegroups.com>
I don't really know the reason, but you can use prog instead:

(lambda (s)
  "Find the first integer that is weakly larger than s.  The example
  is deliberately simplified, please don't propose improvements to the
  algorithm, or suggest that I use ceiling :-P"
  (prog ((i 0))
     top
       (if (< i s)
           (progn
             (incf i)
             (go top))
           (return i))))
From: ··················@gmail.com
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <94b6536d-ef2d-4676-948d-478a60aeccf6@z23g2000prd.googlegroups.com>
On Apr 29, 12:54 pm, Tamas K Papp <······@gmail.com> wrote:
> I was writing some code that works best with tagbody (nope, not the
> example below, that is deliberately simplified), and when I wanted to
> use the value of the expression I realized that it returns nil.
>
> My experience is that whenever I run into something in the standard
> that doesn't make sense (to me), there is usually a design choice that
> rationalizes it (which I usually realize later).  So what is the
> reason for tagbody returning nil, and not the value of the last
> expression it evaluated?  Eg

Tagbody is a more primitive construction it is closer to something
like 'go'.

> (lambda (s)
>   "Find the first integer that is weakly larger than s.  The example
>   is deliberately simplified, please don't propose improvements to the
>   algorithm, or suggest that I use ceiling :-P"
>   (let ((i 0))
>     (tagbody
>      top
>        (if (< i s)
>            (progn
>              (incf i)
>              (go top))
>            i))))     ; does not work, since tagbody always returns nil

This algorithm would be better if you used ceiling :-P

> I can of course work around it, but if I am using a closure, I need a
> named block (or is there a better way?), like this:
>
> (lambda (s)
>   "Same purpose, with name block and return-from."
>   (block closure
>     (let ((i 0))
>       (tagbody
>        top
>          (if (< i s)
>            (progn
>              (incf i)
>              (go top))
>            (return-from closure i))))))
>
> Thanks,
>
> Tamas

You might be able to use throw and catch.
Is hard to know what you want from the example, as it is indeed, very
simplified. Can you post something more substantial?

if it is a lambda (this works with labels too), you can probably do
variable-capture magick and do like:

(let (i)
  ((lambda (s)
    (setf i 0)
      (tagbody
        top
        (if (< i s)
            (progn
              (incf i)
              (go top)
             ))
       )) s)
 stuff you want to do with i here)
From: ······@lisp.de
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <ef397732-390d-42f9-aada-1ba1db02547a@d7g2000prl.googlegroups.com>
On Apr 29, 6:54 pm, Tamas K Papp <······@gmail.com> wrote:
> I was writing some code that works best with tagbody (nope, not the
> example below, that is deliberately simplified), and when I wanted to
> use the value of the expression I realized that it returns nil.
>
> My experience is that whenever I run into something in the standard
> that doesn't make sense (to me), there is usually a design choice that
> rationalizes it (which I usually realize later).  So what is the
> reason for tagbody returning nil, and not the value of the last
> expression it evaluated?  Eg
>
> (lambda (s)
>   "Find the first integer that is weakly larger than s.  The example
>   is deliberately simplified, please don't propose improvements to the
>   algorithm, or suggest that I use ceiling :-P"
>   (let ((i 0))
>     (tagbody
>      top
>        (if (< i s)
>            (progn
>              (incf i)
>              (go top))
>            i))))     ; does not work, since tagbody always returns nil
>
> I can of course work around it, but if I am using a closure, I need a
> named block (or is there a better way?), like this:
>
> (lambda (s)
>   "Same purpose, with name block and return-from."
>   (block closure
>     (let ((i 0))
>       (tagbody
>        top
>          (if (< i s)
>            (progn
>              (incf i)
>              (go top))
>            (return-from closure i))))))
>
> Thanks,
>
> Tamas

Use

(let ((i 0))
   (tagbody ...)
  i)

TAGBODY is IIRC introduced by Common Lisp as a primitive construct.
Before that there was only PROG as the primitive construct for tags
and jumps. PROG was already in Lisp 1.5 and it returned NIL. So, there
is the issue of compatibility with older constructs.

Also:

(tagbody

   (go end)
   ...
   end)

vs.

(tagbody

   (go end)
   ...
   end
   (something))

A single symbol (here at the end) will not be evaluated, but is a tag.
It would not be evaluated as a symbol. This would be strange:

(let ((quit nil))
  (tagbody
    ...
    ...
    quit))

A programmer might be expecting from looking at the code that this
would return the value of QUIT.

It is best to think of TAGBODY in this way:

* all symbols are tags

* all other forms will be evaluated, but it only makes sense if they
have side-effects
From: Kaz Kylheku
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <20090510053111.807@gmail.com>
On 2009-04-29, Tamas K Papp <······@gmail.com> wrote:
> I was writing some code that works best with tagbody (nope, not the
> example below, that is deliberately simplified), and when I wanted to
> use the value of the expression I realized that it returns nil.
>
> My experience is that whenever I run into something in the standard
> that doesn't make sense (to me), there is usually a design choice that
> rationalizes it (which I usually realize later).  So what is the
> reason for tagbody returning nil, and not the value of the last
> expression it evaluated?  Eg
>
> (lambda (s)
>   "Find the first integer that is weakly larger than s.  The example
>   is deliberately simplified, please don't propose improvements to the
>   algorithm, or suggest that I use ceiling :-P"
>   (let ((i 0))
>     (tagbody 
>      top
>        (if (< i s)
> 	   (progn
> 	     (incf i)
> 	     (go top))
> 	   i))))     ; does not work, since tagbody always returns nil

Just move a parenthesis over the i, so that it's returned from the LET.
Also, use WHEN instead of IF and PROGN. Why pretend that the code
is functional?

 (lambda (s)
   "Find the first integer that is weakly larger than s.  The example
   is deliberately simplified, please don't propose improvements to the
   algorithm, or suggest that I use ceiling :-P"
   (let ((i 0))
     (tagbody 
     top
       (when (< i s)
         (incf i)
         (go top)))
     i))

See? Since we've already subscribed to imperative programming, we should have
no qualms against letting the TAGBODY fall thorugh, and then returning the
value of i.

The combination of local variables with LET, TAGBODY and a BLOCK is known as
PROG.  PROG is a ``de luxe'' imperative programming construct:

  (lambda (s)
    (prog ((i 0))
    :top
      (when (< i s)
        (incf i)
        (go :top))
      (return i)))

A possible reason why PROG and TAGBODY don't return NIL by default is that they
are purely imperative constructs, not functional. But by this rationale, they
should return nothing, as if by (VALUES), rather than the value NIL.
From: Tobias C. Rittweiler
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <87ocufxm4i.fsf@freebits.de>
Tamas K Papp <······@gmail.com> writes:

> I was writing some code that works best with tagbody (nope, not the
> example below, that is deliberately simplified), and when I wanted to
> use the value of the expression I realized that it returns nil.
>
> My experience is that whenever I run into something in the standard
> that doesn't make sense (to me), there is usually a design choice that
> rationalizes it (which I usually realize later).  So what is the
> reason for tagbody returning nil, and not the value of the last
> expression it evaluated?  Eg

I was bitten by that a few days ago myself. When I thought about it, I
came up that it's easily ambiguous. For example, what should (TAGBODY
(GO :FOO) :FOO) return?

> I can of course work around it, but if I am using a closure, I need a
> named block (or is there a better way?), like this:

Use PROG.

  -T.
From: John Thingstad
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <op.us686iamut4oq5@pandora>
På Wed, 29 Apr 2009 18:54:13 +0200, skrev Tamas K Papp <······@gmail.com>:

> I was writing some code that works best with tagbody (nope, not the
> example below, that is deliberately simplified), and when I wanted to
> use the value of the expression I realized that it returns nil.
>
> My experience is that whenever I run into something in the standard
> that doesn't make sense (to me), there is usually a design choice that
> rationalizes it (which I usually realize later).  So what is the
> reason for tagbody returning nil, and not the value of the last
> expression it evaluated?  Eg
>
> (lambda (s)
>   "Find the first integer that is weakly larger than s.  The example
>   is deliberately simplified, please don't propose improvements to the
>   algorithm, or suggest that I use ceiling :-P"
>   (let ((i 0))
>     (tagbody
>      top
>        (if (< i s)
> 	   (progn
> 	     (incf i)
> 	     (go top))
> 	   i))))     ; does not work, since tagbody always returns nil
>
> I can of course work around it, but if I am using a closure, I need a
> named block (or is there a better way?), like this:
>
> (lambda (s)
>   "Same purpose, with name block and return-from."
>   (block closure
>     (let ((i 0))
>       (tagbody
>        top
> 	 (if (< i s)
> 	   (progn
> 	     (incf i)
> 	     (go top))
> 	   (return-from closure i))))))
>
> Thanks,
>
> Tamas

tagbody is low level. Basically just a set of adresses and go. In assembly  
you don't return either unless you explicitly call ret. This is the reason  
prog, prog1, prog2 and progn are buildt on top of it.

---------------------
John Thingstad
From: John Thingstad
Subject: Re: why does tagbody return nil?
Date: 
Message-ID: <op.us69hs1gut4oq5@pandora>
På Thu, 30 Apr 2009 14:33:44 +0200, skrev John Thingstad  
<·······@online.no>:

>
> tagbody is low level. Basically just a set of adresses and go. In  
> assembly you don't return either unless you explicitly call ret. This is  
> the reason prog, prog1, prog2 and progn are buildt on top of it.
>

By the way this is not neccesairy bad.. See my hangman game  
(gallow-matrix) for a application:  
http://home.chello.no/~jthing/games/hangman.html

---------------------
John Thingstad