From: rif
Subject: &rest parameters vs. multiple arguments?
Date: 
Message-ID: <wj0he7ct8gm.fsf@five-percent-nation.mit.edu>
The following idiom will produce a list whose i'th element is the
maximum of the i'th element of the input lists:

* (mapcar #'max '(3 4 5) '(4 2 6) '(7 2 1))
(7 4 6)

Now suppose I want a function that takes a variable number of lists as
arguments that performs the same operations.  My first attempt is:

(defun max-of-lists (&rest args) (mapcar #'max args))

But this fails, because the list arguments get turned into a single
list-of-lists when they get put into the &rest parameter.  I feel like
I'm probably missing something obvious here.

I can see a solution that works as a recursive function, but is there
a simple way to do it in terms of built-in mapping operations?

rif

From: Barry Margolin
Subject: Re: &rest parameters vs. multiple arguments?
Date: 
Message-ID: <qnQBa.23$Wb3.212@paloalto-snr1.gtei.net>
In article <···············@five-percent-nation.mit.edu>,
rif  <···@mit.edu> wrote:
>
>The following idiom will produce a list whose i'th element is the
>maximum of the i'th element of the input lists:
>
>* (mapcar #'max '(3 4 5) '(4 2 6) '(7 2 1))
>(7 4 6)
>
>Now suppose I want a function that takes a variable number of lists as
>arguments that performs the same operations.  My first attempt is:
>
>(defun max-of-lists (&rest args) (mapcar #'max args))
>
>But this fails, because the list arguments get turned into a single
>list-of-lists when they get put into the &rest parameter.  I feel like
>I'm probably missing something obvious here.

(defun max-of-lists (&rest args) (apply #'mapcar #'max args))

-- 
Barry Margolin, ··············@level3.com
Level(3), Woburn, 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.
From: rif
Subject: Re: &rest parameters vs. multiple arguments?
Date: 
Message-ID: <wj0brxi269j.fsf@five-percent-nation.mit.edu>
OK, thanks for the help everyone!  I think I get it now.

The real key for me was going back to Graham's ANSI CL and reading his
version of -mapcar in Appendix B, and then watching it on some
examples.

Thanks again.

Cheers,

rif
From: Wade Humeniuk
Subject: Re: &rest parameters vs. multiple arguments?
Date: 
Message-ID: <qtQBa.2901$MM4.62085@news0.telusplanet.net>
"rif" <···@mit.edu> wrote in message ····················@five-percent-nation.mit.edu...
> 
> The following idiom will produce a list whose i'th element is the
> maximum of the i'th element of the input lists:
> 
> * (mapcar #'max '(3 4 5) '(4 2 6) '(7 2 1))
> (7 4 6)
> 
> Now suppose I want a function that takes a variable number of lists as
> arguments that performs the same operations.  My first attempt is:
> 
> (defun max-of-lists (&rest args) (mapcar #'max args))

(defun max-of-lists (&rest args) (apply #'max args))

Wade
From: Wade Humeniuk
Subject: Re: &rest parameters vs. multiple arguments?
Date: 
Message-ID: <1wQBa.2902$MM4.62490@news0.telusplanet.net>
"Wade Humeniuk" <····@nospam.nowhere> wrote in message
·························@news0.telusplanet.net...
>
> (defun max-of-lists (&rest args) (apply #'max args))
>
> Wade

I am not sure what you really meant, but the func above does

CL-USER 1 > (defun max-of-lists (&rest args) (apply #'max args))

MAX-OF-LISTS

CL-USER 2 > (mapcar #'max-of-lists '(3 4 5) '(4 2 6) '(7 2 1) '(10 3 1))

(10 4 6)

CL-USER 3 >

Wade
From: Ivan Boldyrev
Subject: Re: &rest parameters vs. multiple arguments?
Date: 
Message-ID: <pn7sqx9sj.ln2@elaleph.borges.cgitftp.uiggm.nsc.ru>
"Wade Humeniuk" <····@nospam.nowhere> writes:

> (defun max-of-lists (&rest args) (apply #'max args))

Does #'max-of-lists realy differ from #'max? ;)

* (max-of-lists 1 3 4)
4

* (max 1 3 4)
4

-- 
Ivan Boldyrev

                       Perl is a language where 2 x 2 is not equal to 4.
From: Kaz Kylheku
Subject: Re: &rest parameters vs. multiple arguments?
Date: 
Message-ID: <cf333042.0306030857.63bf79a1@posting.google.com>
Ivan Boldyrev <···············@cgitftp.uiggm.nsc.ru> wrote in message news:<·············@elaleph.borges.cgitftp.uiggm.nsc.ru>...
> "Wade Humeniuk" <····@nospam.nowhere> writes:
> 
> > (defun max-of-lists (&rest args) (apply #'max args))
> 
> Does #'max-of-lists realy differ from #'max? ;)
> 
> * (max-of-lists 1 3 4)
> 4
> 
> * (max 1 3 4)
> 4

Yes; it's an extension of MAX which allows the last argument to be a
list of numbers. :)
From: Kalle Olavi Niemitalo
Subject: Re: &rest parameters vs. multiple arguments?
Date: 
Message-ID: <87u1b73ws5.fsf@Astalo.kon.iki.fi>
···@ashi.footprints.net (Kaz Kylheku) writes:

> Ivan Boldyrev <···············@cgitftp.uiggm.nsc.ru> wrote in message news:<·············@elaleph.borges.cgitftp.uiggm.nsc.ru>...
>
>> Does #'max-of-lists realy differ from #'max? ;)
>
> Yes; it's an extension of MAX which allows the last argument to be a
> list of numbers. :)

No, it doesn't.

  * (defun max-of-lists (&rest args) (apply #'max args))

  MAX-OF-LISTS
  * (max-of-lists 1 2 3 '(4 5 6))

  Argument X is not a REAL: (4 5 6).

Perhaps you were thinking of (apply #'apply #'max args), but
that *requires* the last argument to be a list of numbers.
From: Christopher C. Stacy
Subject: Re: &rest parameters vs. multiple arguments?
Date: 
Message-ID: <uy90oqdfr.fsf@dtpq.com>
>>>>> On 30 May 2003 17:45:13 -0400, rif  ("rif") writes:

 rif> The following idiom will produce a list whose i'th element
 rif> is the maximum of the i'th element of the input lists:

 rif> * (mapcar #'max '(3 4 5) '(4 2 6) '(7 2 1))
 rif> (7 4 6)

 rif> Now suppose I want a function that takes a variable number
 rif> of lists as arguments that performs the same operations.
 rif> My first attempt is:

 rif> (defun max-of-lists (&rest args) (mapcar #'max args))

 rif> But this fails, because the list arguments get turned into a
 rif> single list-of-lists when they get put into the &rest parameter.
 rif> I feel like I'm probably missing something obvious here.

Your function could either take &REST ARGS, or require the caller
to do the consing and your function takes a single LIST-OF-ARGS.
In any case, once you have consed up a list of args that need to 
be applied to a function, you will of course want to use APPLY.

  (defun max-of-lists (&rest args) (apply #'mapcar #'max args))