Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;A polynomial is expressed in list form. 1+3x+5x^2+7x^3 would be entered as '(1 3 4 7)
- (define (nzero n) ;;Returns a list of n zeros
- (if (< n 1) '()
- (cons 0 (nzero (- n 1)))))
- (define (polyAdd p1 p2) ;;adds two polynomials expressed in lists
- (if (null? p1) p2 (if (null? p2) p1
- (cons (+ (car p1) (car p2)) (polyAdd (cdr p1) (cdr p2))))))
- (define (polyAddList l) ;;adds a list of polynomials expressed in lists
- (if (null? l) '()
- (polyAdd (car l) (polyAddList (cdr l)))))
- ;;returns a list of lists
- ;;used to find the product of a polynomial by adding these lists:
- (define (polyMultHelper l1 l2 n)
- (if (null? l1) '()
- (cons (append (nzero n) (map (lambda (x) (* (car l1) x)) l2))
- (polyMultHelper (cdr l1) l2 (+ n 1)))))
- (define (polyMult p1 p2) ;;returns the product of two polynomials
- (polyAddList (polyMultHelper p1 p2 0))) ;;WHAT SHOULD THIS n BE INSTEAD OF 0??
Advertisement
Add Comment
Please, Sign In to add comment