Re: Multiplication of lists



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.
.



Relevant Pages

  • Lisp collections
    ... Has anyone written a library for this in Lisp? ... I hear a lot that in Lisp, you should use lists, and then switch to arrays ... tree of cons cells. ...
    (comp.lang.lisp)
  • Re: functional style compared to procedural style and comments on a Mathematica function I saw.
    ... decent book on matrix computations. ... which of the many other compression techniques in the literature for sparse arrays you think is superior and how they should be represented in lisp? ... Which you can use in Mathematica but not in Lisp. ... That is Owith lists and Owith an array of arrays. ...
    (sci.math.symbolic)
  • Re: How powerful macros are?
    ... >> for software engineers to use arrays for everything, ... Did I miss something or is #an impossibly unbearable syntax in Lisp? ... and arrays are often slower for very short lists. ... I've also extended the language so that I have re-readable hash tables ...
    (comp.lang.lisp)
  • Re: Lisp and very large arrays
    ... an attempt to do a little bit of Lisp evangelism, ... LispWorks Personal 5.0.2 on Mac OS X won't even create the arrays; ... Experimentation from the sbcl prompt showed that: ... (defun main ... ...
    (comp.lang.lisp)
  • Re: LISPPA
    ... > of arbitrary objects in a programming language. ... Both Lisp and LISPPA have relation to the list processing. ... Not only Python uses polymorphic arrays. ...
    (comp.lang.lisp)