From: Shin
Subject: Really basic questions
Date: 
Message-ID: <37f34999.6518328@news.iddeo.es>
Hello,

I am self-studying Lisp and have (at least :) this very basic doubts,
could you help me please?

1) Browsing HyperSpec I haven't been able to find a function to compute
just the quotient of the integer division of two integers... is
something like

   (defmacro quotient (n) `(values (floor (/ ,n ,m))))

the Common Lisp way to obtain it or there is something better?

2) I have read in HyperSpec that the way to get the first value returned
by a form that gives multiples values is to use VALUES as in the macro
above. What about the nth-value? How could I get it?

3) I don't understand this two remarks about WHEN from HyperSpec:

              (quoting \HyperSpec\Body\mac_whencm_unless.html)

   a) If two or more forms follow the form-test they are said to be
      an implicit progn, so I expect that
      
         (when t 'first 'second)

      returns

         SECOND

      but you read:

   b) "In a when form, if the test-form yields true, the forms are
      evaluated in order from left to right and the values returned by
      the forms are returned from the when form."

      So this seems to say that the result(s) should be

         FIRST, SECOND

      didn't it?

CLisp and Corman Lisp give SECOND... what does b) mean then?

Well, these are some questions that have arisen these days... I'll
appreciate any clarifications. Thank you very much.

-- Shin

From: Shin
Subject: Re: Really basic questions
Date: 
Message-ID: <37f44c46.7203488@news.iddeo.es>
On Thu, 30 Sep 1999 11:39:38 GMT, ···@retemail.es (Shin) wrote:

:    (defmacro quotient (n) `(values (floor (/ ,n ,m))))

Sorry, this should have been

   (defmacro quotient (n m) `(values (floor (/ ,n ,m))))

of course.

-- Shin
From: Tim Bradshaw
Subject: Re: Really basic questions
Date: 
Message-ID: <ey3yadoshc0.fsf@lostwithiel.tfeb.org>
* Shin  wrote:

>    (defmacro quotient (n) `(values (floor (/ ,n ,m))))

A better approach would be to notice that FLOOR takes an optional
DIVISOR argument, so the first value of (FLOOR n m) is the result you
want.  Except, you might want ROUND not FLOOR?

I would tend to just use FLOOR / ROUND in code if I wanted this -- the
additional value is ignored if you don't use it.  If I wanted to give
the thing a name as a function, I'd define it as one:

	(declaim (inline iquot))
	(defun iquot (n m) (values (floor n m)))

Using a macro would be considered bad style, I think.


> 2) I have read in HyperSpec that the way to get the first value returned
> by a form that gives multiples values is to use VALUES as in the macro
> above. What about the nth-value? How could I get it?

NTH-VALUE

> 3) I don't understand this two remarks about WHEN from HyperSpec:

>               (quoting \HyperSpec\Body\mac_whencm_unless.html)

>    a) If two or more forms follow the form-test they are said to be
>       an implicit progn, so I expect that
      
>          (when t 'first 'second)

>       returns

>          SECOND

>       but you read:

>    b) "In a when form, if the test-form yields true, the forms are
>       evaluated in order from left to right and the values returned by
>       the forms are returned from the when form."

>       So this seems to say that the result(s) should be

>          FIRST, SECOND

>       didn't it?

(a) is correct, I find (b) misleading. The important thing is that the
body of a WHEN or UNLESS is an `implicit PROGN' and it does what PROGN
does, which is evaluate the forms from left to right and return any
values that the last form returns.

I hope this helps

--tim
From: Pierre R. Mai
Subject: Re: Really basic questions
Date: 
Message-ID: <87r9jgxz0e.fsf@orion.dent.isdn.cs.tu-berlin.de>
···@retemail.es (Shin) writes:

> I am self-studying Lisp and have (at least :) this very basic doubts,
> could you help me please?
> 
> 1) Browsing HyperSpec I haven't been able to find a function to compute
> just the quotient of the integer division of two integers... is
> something like
> 
>    (defmacro quotient (n) `(values (floor (/ ,n ,m))))
> 
> the Common Lisp way to obtain it or there is something better?

The Common Lisp way to obtain just the quotient is

(truncate n m)

or

(floor n m)

depending on how you define integer division (the truncate version is
IMHO the normal one).

> 2) I have read in HyperSpec that the way to get the first value returned
> by a form that gives multiples values is to use VALUES as in the macro
> above. What about the nth-value? How could I get it?

No, to get the first value returned by a form that gives multiple
values you don't have to do anything special:

(+ (truncate 7 3) 1)

=> 3

The advice of using values applies to situations, in which you want to 
avoid passing all the other values along to your caller, since
e.g. progn (and the implicit progn's of forms like defun, let, etc.)
passes along all values of the last form transparently:

(let ((x 4) (y 3))
  (format t "~D / ~D~%" x y)
  (truncate x y))

returns both values of the last form, namely 1 and 3,

whereas

(let ((x 4) (y 3))
  (format t "~D / ~D~%" x y)
  (values (truncate x y)))

returns only 1, with 3 being discarded by the values form.

But in normal circumstances, you don't have to worry about accessing
only the first value, since the default is to only consider the first
value and ignore any other values.  In other words, you have to do
special things to access more than the first value, by using forms
like multiple-value-bind, multiple-value-call, nth-value, etc.

> 3) I don't understand this two remarks about WHEN from HyperSpec:
> 
>               (quoting \HyperSpec\Body\mac_whencm_unless.html)
> 
>    a) If two or more forms follow the form-test they are said to be
>       an implicit progn, so I expect that
>       
>          (when t 'first 'second)
> 
>       returns
> 
>          SECOND
> 
>       but you read:
> 
>    b) "In a when form, if the test-form yields true, the forms are
>       evaluated in order from left to right and the values returned by
>       the forms are returned from the when form."
> 
>       So this seems to say that the result(s) should be
> 
>          FIRST, SECOND
> 
>       didn't it?

The b) paragraph should be considered a typo, the intended meaning
should be the one of a), i.e. all the values of the _last_ form are
returned, not all the values of all forms (c.f. the definition of
progn).  Of course this is a common sense interpretation of the
standard, and thus hasn't any legal bearing.  But it is nonetheless
the only useful reading, so you should be able to expect that any
implementor will agree with it, and therefore return SECOND ony.

Regs, Pierre.

-- 
Pierre Mai <····@acm.org>         PGP and GPG keys at your nearest Keyserver
  "One smaller motivation which, in part, stems from altruism is Microsoft-
   bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]
From: Lieven Marchand
Subject: Re: Really basic questions
Date: 
Message-ID: <m31zbgfhgu.fsf@localhost.localdomain>
····@acm.org (Pierre R. Mai) writes:

> ···@retemail.es (Shin) writes:
> 
> >               (quoting \HyperSpec\Body\mac_whencm_unless.html)
> > 
> >    b) "In a when form, if the test-form yields true, the forms are
> >       evaluated in order from left to right and the values returned by
> >       the forms are returned from the when form."
> > 
> The b) paragraph should be considered a typo, the intended meaning
> should be the one of a), i.e. all the values of the _last_ form are
> returned, not all the values of all forms (c.f. the definition of
> progn).  Of course this is a common sense interpretation of the
> standard, and thus hasn't any legal bearing.  But it is nonetheless
> the only useful reading, so you should be able to expect that any
> implementor will agree with it, and therefore return SECOND ony.
> 

I don't think it's a typo. "values" in the phrase 'the values
returned' is a hyperlink to the definition in the glossary where you
find under (1c) the case of an implicit progn that indeed it is "one
of possibly several objects that result from the evaluation of the
last form, or nil if there are no forms."

-- 
Lieven Marchand <···@bewoner.dma.be>
If there are aliens, they play Go. -- Lasker
From: Barry Margolin
Subject: Re: Really basic questions
Date: 
Message-ID: <gJPI3.360$854.12368@burlma1-snr2>
In article <··············@orion.dent.isdn.cs.tu-berlin.de>,
Pierre R. Mai <····@acm.org> wrote:
>···@retemail.es (Shin) writes:
>
>> I am self-studying Lisp and have (at least :) this very basic doubts,
>> could you help me please?
>> 
>> 1) Browsing HyperSpec I haven't been able to find a function to compute
>> just the quotient of the integer division of two integers... is
>> something like
>> 
>>    (defmacro quotient (n) `(values (floor (/ ,n ,m))))
>> 
>> the Common Lisp way to obtain it or there is something better?
>
>The Common Lisp way to obtain just the quotient is
>
>(truncate n m)
>
>or
>
>(floor n m)

You still need to wrap it in (values ...) or (nth-value 0 ...) if you want
to throw away the remainder.

However, in practice you generally don't worry about the second value.
Normally these functions are used in a context that only uses the first
value, so the remainder will be ignored automatically.  The only places
where the second value could show up unexpectedly is if you were using the
function within a MULTIPLE-VALUE-CALL or MULTIPLE-VALUE-LIST form.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.