Trying to multiply all elements in a list of sublists with elements in another list in lisp -
i'm trying multiply list n sublists list n scalars. it's supposed work this:
(kmult-matrix '((3 4 2 4) (2 5 6 9) (1 -2 8 10)) '(2 3 5)) => ((6 8 4 8) (6 15 18 27) (5 -10 40 50))
each nth sublist multiplied nth scalar in second list. here's got far:
(defun kmult-matrix (m k) (apply 'append (mapcar (lambda (x1) (mapcar (lambda (x2) (mapcar (lambda (x3) (* x3 x1)) x2)) m)) k)))
which results in:
((6 8 4 8) (4 10 12 18) (2 -4 16 20) (9 12 6 12) (6 15 18 27) (3 -6 24 30) (15 20 10 20) (10 25 30 45) (5 -10 40 50))
hope it's ok ask assigment , have feeling i'm not far away solution. btw, have use mapcar.
it's lot easier that. remember mapcar
can take more 1 list. thus:
(defun kmult-matrix (m k) (mapcar (lambda (list k) ;; inside here list , ;; it's corresponding multiplier k ...) m k))
the result outer map list many elements there sublists m
(and corresponding elements in k
). inside lambda map on lst
multiplication per element. inner map produces once list answer per element in outer map.
Comments
Post a Comment