From: hbn
Subject: Multiplication of lists
Date: 
Message-ID: <a913fdbd-da04-4874-bdd7-1cacc34cfbf8@q30g2000prq.googlegroups.com>
Hi there,

I'm rather new to LISP as you might be able to tell from my simple
question:

I wan't to write a function that multiplies a list of numbers by all
the numbers in another list. The result should be a list of lists.

Given the input

(1 2 3) and (1 5 10 20)

the result should be

((1 2 3) (5 10 15) (10 20 30) (20 40 60))

I've tried various version of mapcar but can't quite seem to get it
right. Any help is greatly appreciated.

From: William James
Subject: Re: Multiplication of lists
Date: 
Message-ID: <9825c094-db83-45e3-a537-87bdb5021d1f@v13g2000pro.googlegroups.com>
On Nov 6, 3:48 pm, hbn <············@gmail.com> wrote:
> Hi there,
>
> I'm rather new to LISP as you might be able to tell from my simple
> question:
>
> I wan't to write a function that multiplies a list of numbers by all
> the numbers in another list. The result should be a list of lists.
>
> Given the input
>
> (1 2 3) and (1 5 10 20)
>
> the result should be
>
> ((1 2 3) (5 10 15) (10 20 30) (20 40 60))
>
> I've tried various version of mapcar but can't quite seem to get it
> right. Any help is greatly appreciated.

Ruby:

[1,5,10,20].map{|x| [1,2,3].map{|y| x*y}}
    ==>[[1, 2, 3], [5, 10, 15], [10, 20, 30], [20, 40, 60]]
From: Tamas K Papp
Subject: Re: Multiplication of lists
Date: 
Message-ID: <6nh7slFl9ol4U2@mid.individual.net>
On Thu, 06 Nov 2008 13:48:05 -0800, hbn wrote:

> Hi there,
> 
> I'm rather new to LISP as you might be able to tell from my simple
> question:
> 
> I wan't to write a function that multiplies a list of numbers by all the
> numbers in another list. The result should be a list of lists.
> 
> Given the input
> 
> (1 2 3) and (1 5 10 20)
> 
> the result should be
> 
> ((1 2 3) (5 10 15) (10 20 30) (20 40 60))
> 
> I've tried various version of mapcar but can't quite seem to get it
> right. Any help is greatly appreciated.

A simple version using closures:

(defun outer-product (xs ys)
  (mapcar (lambda (y)
	    (mapcar (lambda (x)
		      (* x y))
		    xs))
	  ys))

(outer-product '(1 2 3) '(1 5 10 20))

HTH,

Tamas
From: hbn
Subject: Re: Multiplication of lists
Date: 
Message-ID: <3063f32a-d2fa-4381-bc19-6f66b3669487@s9g2000prg.googlegroups.com>
On 6 Nov., 23:03, Tamas K Papp <······@gmail.com> wrote:
> On Thu, 06 Nov 2008 13:48:05 -0800, hbn wrote:
> > Hi there,
>
> > I'm rather new to LISP as you might be able to tell from my simple
> > question:
>
> > I wan't to write a function that multiplies a list of numbers by all the
> > numbers in another list. The result should be a list of lists.
>
> > Given the input
>
> > (1 2 3) and (1 5 10 20)
>
> > the result should be
>
> > ((1 2 3) (5 10 15) (10 20 30) (20 40 60))
>
> > I've tried various version of mapcar but can't quite seem to get it
> > right. Any help is greatly appreciated.
>
> A simple version using closures:
>
> (defun outer-product (xs ys)
>   (mapcar (lambda (y)
>             (mapcar (lambda (x)
>                       (* x y))
>                     xs))
>           ys))
>
> (outer-product '(1 2 3) '(1 5 10 20))
>
> HTH,
>
> Tamas

That's exactly what I was looking for. I was so close in my own code,
yet so far away :-)

Thank you for helping.
From: Tamas K Papp
Subject: Re: Multiplication of lists
Date: 
Message-ID: <6nh8geFl9ol4U4@mid.individual.net>
On Thu, 06 Nov 2008 14:09:29 -0800, hbn wrote:

>> A simple version using closures:
>>
>> (defun outer-product (xs ys)
>>   (mapcar (lambda (y)
>>             (mapcar (lambda (x)
>>                       (* x y))
>>                     xs))
>>           ys))
>>
>> (outer-product '(1 2 3) '(1 5 10 20))
>
> That's exactly what I was looking for. I was so close in my own code,
> yet so far away :-)
> 
> Thank you for helping.

Don't mention it -- a year ago I was a newbie myself, and got tons of 
help here.

BTW, if you are using this code in real life for actual computations, I 
would use vectors/arrays.  My array-operations package has an outer-
product function in it.

HTH,

Tamas
From: hbn
Subject: Re: Multiplication of lists
Date: 
Message-ID: <2d33742a-8247-4c37-9275-a278c3468012@p31g2000prf.googlegroups.com>
On 6 Nov., 23:13, Tamas K Papp <······@gmail.com> wrote:
> On Thu, 06 Nov 2008 14:09:29 -0800, hbn wrote:
> >> A simple version using closures:
>
> >> (defun outer-product (xs ys)
> >>   (mapcar (lambda (y)
> >>             (mapcar (lambda (x)
> >>                       (* x y))
> >>                     xs))
> >>           ys))
>
> >> (outer-product '(1 2 3) '(1 5 10 20))
>
> > That's exactly what I was looking for. I was so close in my own code,
> > yet so far away :-)
>
> > Thank you for helping.
>
> Don't mention it -- a year ago I was a newbie myself, and got tons of
> help here.
>
> BTW, if you are using this code in real life for actual computations, I
> would use vectors/arrays.  My array-operations package has an outer-
> product function in it.
>
> HTH,
>
> Tamas

Hi again,

This is only for learning the language and doing hobby projects, so
speed is not really important.

But I'm wondering why this solution will perform better with vectors/
arrays? The way I see it the suggested solution will only perform
exactly the required multiplications, so I can't see how LISPs linked
list will cause a performance problem? But as I said, I'm new to the
language, so please enlighten me :-)
From: Tamas K Papp
Subject: Re: Multiplication of lists
Date: 
Message-ID: <6nh9caFlouv2U1@mid.individual.net>
On Thu, 06 Nov 2008 14:22:17 -0800, hbn wrote:

> On 6 Nov., 23:13, Tamas K Papp <······@gmail.com> wrote:
>> On Thu, 06 Nov 2008 14:09:29 -0800, hbn wrote:
>> >> A simple version using closures:
>>
>> >> (defun outer-product (xs ys)
>> >>   (mapcar (lambda (y)
>> >>             (mapcar (lambda (x)
>> >>                       (* x y))
>> >>                     xs))
>> >>           ys))
>>
>> >> (outer-product '(1 2 3) '(1 5 10 20))
>>
>> > That's exactly what I was looking for. I was so close in my own code,
>> > yet so far away :-)
>>
>> > Thank you for helping.
>>
>> Don't mention it -- a year ago I was a newbie myself, and got tons of
>> help here.
>>
>> BTW, if you are using this code in real life for actual computations, I
>> would use vectors/arrays.  My array-operations package has an outer-
>> product function in it.
>>
>> HTH,
>>
>> Tamas
> 
> Hi again,
> 
> This is only for learning the language and doing hobby projects, so
> speed is not really important.
> 
> But I'm wondering why this solution will perform better with vectors/
> arrays? The way I see it the suggested solution will only perform
> exactly the required multiplications, so I can't see how LISPs linked
> list will cause a performance problem? But as I said, I'm new to the
> language, so please enlighten me :-)

With this small example, there should be no difference.  With larger 
examples (eg 5000x5000 arrays), Lisp would have to construct the lists, 
which is called 'consing'.  This is pretty fast in most Lisps, but not as 
fast as vector element access.  Also consider the memory requirements: 
Lisp arrays can be specialized to a particular element type, saving space 
and computation time.

Then there is the questions of what you do with the result.  If you 
access elements of the matrix constructed above, you will have to find 
the jth element of the ith list, which is slow compared to (aref matrix i 
j).

But don't worry about this at the moment.  Just know Lisp code can be 
made very fast if the need arises.

HTH,

Tamas
From: hbn
Subject: Re: Multiplication of lists
Date: 
Message-ID: <e26b38f3-c1b6-4347-8197-e1e38a14f09a@r36g2000prf.googlegroups.com>
On 6 Nov., 23:28, Tamas K Papp <······@gmail.com> wrote:
> On Thu, 06 Nov 2008 14:22:17 -0800, hbn wrote:
> > On 6 Nov., 23:13, Tamas K Papp <······@gmail.com> wrote:
> >> On Thu, 06 Nov 2008 14:09:29 -0800, hbn wrote:
> >> >> A simple version using closures:
>
> >> >> (defun outer-product (xs ys)
> >> >>   (mapcar (lambda (y)
> >> >>             (mapcar (lambda (x)
> >> >>                       (* x y))
> >> >>                     xs))
> >> >>           ys))
>
> >> >> (outer-product '(1 2 3) '(1 5 10 20))
>
> >> > That's exactly what I was looking for. I was so close in my own code,
> >> > yet so far away :-)
>
> >> > Thank you for helping.
>
> >> Don't mention it -- a year ago I was a newbie myself, and got tons of
> >> help here.
>
> >> BTW, if you are using this code in real life for actual computations, I
> >> would use vectors/arrays.  My array-operations package has an outer-
> >> product function in it.
>
> >> HTH,
>
> >> Tamas
>
> > Hi again,
>
> > This is only for learning the language and doing hobby projects, so
> > speed is not really important.
>
> > But I'm wondering why this solution will perform better with vectors/
> > arrays? The way I see it the suggested solution will only perform
> > exactly the required multiplications, so I can't see how LISPs linked
> > list will cause a performance problem? But as I said, I'm new to the
> > language, so please enlighten me :-)
>
> With this small example, there should be no difference.  With larger
> examples (eg 5000x5000 arrays), Lisp would have to construct the lists,
> which is called 'consing'.  This is pretty fast in most Lisps, but not as
> fast as vector element access.  Also consider the memory requirements:
> Lisp arrays can be specialized to a particular element type, saving space
> and computation time.
>
> Then there is the questions of what you do with the result.  If you
> access elements of the matrix constructed above, you will have to find
> the jth element of the ith list, which is slow compared to (aref matrix i
> j).
>
> But don't worry about this at the moment.  Just know Lisp code can be
> made very fast if the need arises.
>
> HTH,
>
> Tamas

Oh, I see, thanks for clarifying.

I really like how it's so easy to do complicated things with just a
few lines of LISP code - the language is very powerful indeed.
From: Andrew Main
Subject: Re: Multiplication of lists
Date: 
Message-ID: <491a5728$1@clear.net.nz>
hbn wrote:
> Hi there,
> 
> I'm rather new to LISP as you might be able to tell from my simple
> question:
> 
> I wan't to write a function that multiplies a list of numbers by all
> the numbers in another list. The result should be a list of lists.
> 
> Given the input
> 
> (1 2 3) and (1 5 10 20)
> 
> the result should be
> 
> ((1 2 3) (5 10 15) (10 20 30) (20 40 60))
> 
> I've tried various version of mapcar but can't quite seem to get it
> right. Any help is greatly appreciated.

mapcar can do this (see Tamas's example), but that's not how I'd code it.
This version uses LOOP, and it's rather short:

(defun outer-product (xs ys)
   (loop for y in ys collect
     (loop for x in xs collect (* x y))))

(outer-product '(1 2 3) '(1 5 10 20))
-> ((1 2 3) (5 10 15) (10 20 30) (20 40 60))

  Main