Re: mapcar use.



On 2006-02-26 03:33:21 +0000, "lispuser" <amit_h123@xxxxxxxxxxx> said:

Hello all,
I am trying to use the mapcar function for the multiplication of 2
lists as :
(1 2) and (4 5 6)
so that the result which i get is
as
((4 5 6) (8 10 12))
I am still not gettin how to use eval and mapcar to achieve this...
Can somebody help me...
thanks

(defun mul-list (list-1 list-2)
"Multiplies all elements of list-2 by the successive elements of list-1"
(let ((res nil))
(dolist (x list-1)
(setf res
(append res
(list
(mapcar (lambda (a)
(when a
(* a x)))
list-2)))))
res))

CL-USER 2 > (mul-list '(1 2) '(4 5 6))
((4 5 6) (8 10 12))

CL-USER 3 > (mul-list '(1 2 3) '(4 5 6))
((4 5 6) (8 10 12) (12 15 18))


But I'm sure more advanced lispers will come up with
lovely one-liners :-)
--
JFB

.



Relevant Pages

  • Re: GUMBY GRUB
    ... >>Res, sorry about the hastle you have received over this. ... >Ive been on redhat lists for years and years and years, ... >I dont accept the feedback on this list either, ... >without a clue have no access to my desktop, mail servers, or RH's mail ...
    (Fedora)
  • Re: mapcar use.
    ... I am trying to use the mapcar function for the multiplication of 2 ... And I've been taught about scalar product ... But I've no idea what the multiplication of two lists can be! ...
    (comp.lang.lisp)
  • Re: Extract groups of like numbers from list
    ... case, a list of lists, the first being the uniques, and the others the ... foreach item $list {lappend a$item} ... } else {lappend res $a} ...
    (comp.lang.tcl)
  • Re: Sendmail aliases, wildcards, and mail header manipulations
    ... Res wrote: ... a bunch of other suppliers. ... lists, but I find it easier to setup a heavily filtered throw away ...
    (comp.mail.sendmail)
  • Re: flatten a level one list
    ... The problem arises in coneverting lists of coordinates into a single list of coordinates eg ... input data. ... for x in pair: res.append(x) return res ...
    (comp.lang.python)