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
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.
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
"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
"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.
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. :)
···@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))