Basic Function Help
Posted: Sun Oct 17, 2010 7:13 pm
Hello fellow board members,
I have sort of hit a dead end with ideas for a simple function I am trying to write called sumprod. Basically, it finds the sum of the product of two separate lists. So for example: (sumprod '(1 2 3) '(2 3)) --> 12, since (1*2*3) + (2*3) = 12.
I know how to find the product of one list, but I am not sure how I would go about finding the product of the second let alone adding its total to the product of the first list.
Here is the necessary Common Lisp code to find the product of one list.
Here is a slightly modified version that also finds the product of the second list but multiplies it with the product of the first list instead of adding it. I know exactly why its doing that, but it's the closest I have coming to solving this one.
If anything is unclear or needs more explanation, let me know. Thanks for any insight or advice you might be able to supply.
Brock
I have sort of hit a dead end with ideas for a simple function I am trying to write called sumprod. Basically, it finds the sum of the product of two separate lists. So for example: (sumprod '(1 2 3) '(2 3)) --> 12, since (1*2*3) + (2*3) = 12.
I know how to find the product of one list, but I am not sure how I would go about finding the product of the second let alone adding its total to the product of the first list.
Here is the necessary Common Lisp code to find the product of one list.
Code: Select all
(defun sumprod (D E)
(cond
((null D) 1)
(t (* (car D) (sumprod (cdr D) E)))
)
)
Code: Select all
(defun sumprod (D E)
(cond
((null E) 0)
((null D) (*(car E) (sumprod D (cdr E))))
((* (car D)(sumprod (cdr D) E)))
)
)
Brock