Multiplication of lists

This is a discussion on Multiplication of lists within the lisp forums in Programming Languages category; 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....

Go Back   Application Development Forum > Programming Languages > lisp

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 11-06-2008, 04:48 PM
hbn
Guest
 
Default Multiplication of lists

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.
Reply With Quote
  #2  
Old 11-06-2008, 05:03 PM
Tamas K Papp
Guest
 
Default Re: Multiplication of lists

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
Reply With Quote
  #3  
Old 11-06-2008, 05:09 PM
hbn
Guest
 
Default Re: Multiplication of lists

On 6 Nov., 23:03, Tamas K Papp <tkp...@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.
Reply With Quote
  #4  
Old 11-06-2008, 05:13 PM
Tamas K Papp
Guest
 
Default Re: Multiplication of lists

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
Reply With Quote
  #5  
Old 11-06-2008, 05:15 PM
William James
Guest
 
Default Re: Multiplication of lists

On Nov 6, 3:48*pm, hbn <xxx.greya...@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]]
Reply With Quote
  #6  
Old 11-06-2008, 05:22 PM
hbn
Guest
 
Default Re: Multiplication of lists

On 6 Nov., 23:13, Tamas K Papp <tkp...@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 :-)
Reply With Quote
  #7  
Old 11-06-2008, 05:28 PM
Tamas K Papp
Guest
 
Default Re: Multiplication of lists

On Thu, 06 Nov 2008 14:22:17 -0800, hbn wrote:

> On 6 Nov., 23:13, Tamas K Papp <tkp...@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
Reply With Quote
  #8  
Old 11-06-2008, 05:35 PM
hbn
Guest
 
Default Re: Multiplication of lists

On 6 Nov., 23:28, Tamas K Papp <tkp...@gmail.com> wrote:
> On Thu, 06 Nov 2008 14:22:17 -0800, hbn wrote:
> > On 6 Nov., 23:13, Tamas K Papp <tkp...@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.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 11:49 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.