1

for example: (3x2 - 5x + 2)(7x + 1) and you simplify it like this:

((3 2)(-5 1)(2 0))((7 1)(1 0))
((21 3)(3 2)(-35 2)(-5 1)(14 1)(2 0))
(21 3)(32 2)(9 1)(2 0)

and you get this answer: 21x3 + 32x2 + 9x + 2

i need this solution in lisp please help

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
iulia
  • 19
  • 3
  • Is this homework? If so, you should tag it as such. – Marcelo Cantos May 15 '10 at 06:46
  • First, check your algebra -- your answer is incorrect. Then, show us what you've tried so far. What are you having trouble with? – Jim Lewis May 15 '10 at 06:48
  • and why is it wrong? you write: (3x2-5x+2) like ((3 2) (-5 1) (2 0)) so you first write the 3 and then the power 2 if 5 has a simple x then it's 1, if we dont have x then it's 0 next step you multyply and add ((a b)(c d))((f g)(j k))=(((a*f) (b+g)) ((a*j)(b+k))((c*f)(d+g))((c*j)(d+k))) and then you combine: (3 2) with (-35 2) and (-5 1) with (14 1) and then you transform and calculate 21x3+32x2+9x+2 – iulia May 15 '10 at 08:13

1 Answers1

0

For the first stage, you need to pair up every LHS component with every RHS component; a cartesian product of the two sets. This requires a two-level map followed by a concatenation of the second-level lists of pairs into a single top level list (think (apply #'append ...).

The second stage can be done with a reduce that builds up an association list, keyed on the exponent.

EDIT: Let me solve a different problem for you, and let you figure out how to translate it into the solution for your problem:

Compute (a + b + ... + k) * (l + m + ... + z) by first expanding into pairs and then summing the products:

(defun mul-sums (aa bb)
  (reduce #'+
          (apply #'append
                 (map 'list
                      #'(lambda (a)
                          (map 'list
                               #'(lambda (b)
                                   (* a b))
                               bb))
                      aa))))
; Compute (1 + 2 + 3) * (3 + 4).
> (mul-sums '(1 2 3) '(3 4))
42
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365