Re: Multiplication of lists
- From: hbn <xxx.greyarea@xxxxxxxxx>
- Date: Thu, 6 Nov 2008 14:35:49 -0800 (PST)
On 6 Nov., 23:28, Tamas K Papp <tkp...@xxxxxxxxx> wrote:
On Thu, 06 Nov 2008 14:22:17 -0800, hbn wrote:
On 6 Nov., 23:13, Tamas K Papp <tkp...@xxxxxxxxx> 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.
.
- References:
- Multiplication of lists
- From: hbn
- Re: Multiplication of lists
- From: Tamas K Papp
- Re: Multiplication of lists
- From: hbn
- Re: Multiplication of lists
- From: Tamas K Papp
- Re: Multiplication of lists
- From: hbn
- Re: Multiplication of lists
- From: Tamas K Papp
- Multiplication of lists
- Prev by Date: Re: Multiplication of lists
- Next by Date: Re: Good Reference on DSL's
- Previous by thread: Re: Multiplication of lists
- Next by thread: Re: Multiplication of lists
- Index(es):
Relevant Pages
|