2

I'm creating a function that multiplies to polynomials together. Part of this involves determining whether I'm multiplying two of the same variables (5x * 3x) or two different variables (5x * 3y).

The function takes two parameters (p1 and p2) which are the polynomials to be multiplied. And they are entered in the form:

'(5 (x 2)) '(3 (y 3))

= 5x^2 * 3y^3.

(These could be anything though..)

So i have an if statement to determine whether the variables are the same:

(if (EQUAL (car(car (cdr p1))) (car(car (cdr p2)))).

Where (car(car (cdr p1)) and (car(car (cdr p2)) are the x and y.

I now want to add these two variable into a list so that the list looks like (x y).

Here is what I've tried:

(setq pList (append (car(car ( cdr p1 )))))
(setq pList (append (car(car ( cdr p2 )))))

But what seems to happen is the first line of code add's the X into the list, but the second line of code replaces the x with a y, so I'm just left with (y). I've tried all sorts of things but nothing that seems to work!

Any help would be much appreciated!

1 Answer 1

2
(defun multp (p1 p2)
  (let ((x1 (caadr p1)) (x2 (caadr p2)))
    (format t "x1 is ~a, x2 is ~a~%" x1 x2)
    (let ((plist (if (equal x1 x2) (list x1) (list x1 x2))))
      (format t "plist is ~a~%" plist))))

then

(multp '(5 (x 2)) '(3 (x 3)))
=> 
x1 is X, x2 is X
plist is (X)
NIL

and

(multp '(5 (x 2)) '(3 (y 3)))
=> 
x1 is X, x2 is X
plist is (X Y)
NIL
Sign up to request clarification or add additional context in comments.

5 Comments

How come if I try and print x1 and x2 within the function, it returns the error: Undefined Variables : X1 X2. Surely they've just been defined?
Works perfectly thanks. How would I add the (X Y) to my list 'pList'? Would I have to do it within the let statement?
I would use let, but it depends on what's next in your code. See my edit.
How come when I try to print pList out of the let, it returns 'pList is unbound'?
Think I've got it, thank you very much for you help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.