Reputation:
I'm working on a Lisp project for a college class, so I'm just starting to code in this language.
We were asked to implement a Lisp library to manage monomials and polynomials.
Monomials are rappresented as: (M coefficient total-deegre (vars-and-powers)) e.g. 3ywt^3 is (M 3 5 ((V 3 T) (V 1 W) (V 1 Y))).
I'm not sure about how the implementation of the function which has to return a list of the variables in M. I've already defined a function to get the vars-and-powers, called varpowers.
My idea is: I'm using the list returned by varpowers to get the variables using the last function. Here is my code:
(defun var-of (m)
(setq a (varpowers m))
(cond ((null a) nil)
(t (cons (last (car a))
(var-of (cdr a))))))
I guess the recursion is not working because everytime var-of is called in the last line a is redefined and its car is always the same ((V 3 T)). How can I fix this problem?
Thanks in advance.
Upvotes: 0
Views: 280
Reputation: 1934
Change setq
by let
:
(defun var-of (m)
(let ((a (varpowers m)))
(cond ((null a) nil)
(t (cons (last (car a))
(var-of (cdr a)) )))))
Upvotes: 4