multiplying each element of a list with each element of another list in lisp -


i want write function accepts 2 lists argument , return multiplication of them in list. this:

(3 4) (3 5 6) => (9 15 18 12 20 24)

this code i've came receive error telling me have few arguments map.

(defun multip (lst lst2)     ;this function flatten result    (defun flatten (tree)      (let ((result '()))        (labels ((scan (item)            (if (listp item)                (map nil #'scan item)                (push item result))))                   (scan tree))                        (nreverse result))) (flatten (map (lambda (i) (map (lambda (j) (* j)) lst )) lst2))  )     (write  (multip '(3 4 6) '(3 2) )) 

i can not understand doing wrong. appreciate comment.

you should use mapcar instead of map:

(mapcar (lambda (i) (mapcar (lambda (j) (* j)) lst )) lst2)) 

these 2 different functions: mapcar maps function on 1 or more lists, , requires @ least 2 arguments, while map equivalent kind of sequences (e.g. vectors), , requires additional argument specifying type of result. see reference map here, , reference mapcar here.

style

you using defun inside defun: not style, since every time multip called redefines globally function flatten. should either define flatten externally, once, or use local declaration of function flet or labels (as internal function scan inside flatten.)

for alternative , more simple definitions of flatten, can see this question in so.


Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -