Here’s my question: For one of my assignments, I was tasked with developing a lisp program that takes 2 lists as input, one symbolizing a shopping cart (L1) with an item name and quantity, the second one symbolizing a price list (L2), with the item name and price. Everything follows this format:
(calcTotal '(shirtA 3 shirtB 1) '(shirtA 25 shirtB 55))
The total is 155.00
Here’s my code below:
(defun calcTotal (L1 L2 &aux(Ttl 0))
(cond
(
(and (listp L1) (listp L2))
(do
((tLst1 L1 (cddr tLst1)))
((equal tLst1 nil) Ttl)
(do
((tLst2 L2 (cddr tLst2)))
((equal tLst2 nil) nil)
(cond
(
(equal (car tLst1) (car tLst2))
(print (+ Ttl (* (cadr tLst1) (cadr tLst2))))
)
)
)
)
)
)
)
Basically, what it does is check for the name of the item in the first list, then search for it in the second list. Once it finds a match, multiply their values together to get the total for that item, then remove the first two elements in the first list, and repeat. The problem is that the total (Ttl) does not accumulate. I can get the amount for each item specifically, but for some reason Ttl returns as 0. Can anyone tell me why?
You just print out the value. To keep it for later, also set the variable: