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.
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]]
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
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.
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
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 :-)
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
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.
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