Guest User

Untitled

a guest
Mar 20th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;A polynomial is expressed in list form. 1+3x+5x^2+7x^3 would be entered as '(1 3 4 7)
  2.  
  3.  
  4. (define (nzero n) ;;Returns a list of n zeros
  5.   (if (< n 1) '()
  6.       (cons 0 (nzero (- n 1)))))
  7.  
  8. (define (polyAdd p1 p2) ;;adds two polynomials expressed in lists
  9.   (if (null? p1) p2 (if (null? p2) p1
  10.    (cons (+ (car p1) (car p2)) (polyAdd (cdr p1) (cdr p2))))))
  11.  
  12. (define (polyAddList l) ;;adds a list of polynomials expressed in lists
  13.   (if (null? l) '()
  14.   (polyAdd (car l) (polyAddList (cdr l)))))
  15.  
  16.  
  17. ;;returns a list of lists
  18. ;;used to find the product of a polynomial by adding these lists:
  19. (define (polyMultHelper l1 l2 n)
  20.    (if (null? l1) '()
  21.    (cons (append (nzero n) (map (lambda (x) (* (car l1) x)) l2))
  22.          (polyMultHelper (cdr l1) l2 (+ n 1)))))
  23.    
  24. (define (polyMult p1 p2) ;;returns the product of two polynomials
  25.   (polyAddList (polyMultHelper p1 p2 0))) ;;WHAT SHOULD THIS n BE INSTEAD OF 0??
Advertisement
Add Comment
Please, Sign In to add comment