Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang scheme
- (define (p= a b)
- (cond
- ((and (number? a)(number? b)) (= a b))
- ((and (list? a)(list? b)) (equal? a b))
- (#t #f)))
- (define (variable-greater? a b)
- (let ((avar (second a))
- (aexp (third a))
- (bvar (second b))
- (bexp (third b)))
- (cond
- ((> aexp bexp) #t)
- ((< aexp bexp) #f) ;; after this it means the exps are equal
- ((string<? (symbol->string avar) (symbol->string bvar)) #t)
- (#t #f))))
- (define (normalize-term t)
- (append (list '* (second t))
- (sort (cddr t) variable-greater?)))
- (define (var-list-comp a b)
- (cond
- ((and (empty? a) (empty? b)) 0)
- ((empty? a) -1)
- ((empty? b) 1)
- ((variable-greater? (car a) (car b)) -1)
- ((variable-greater? (car b) (car a)) 1)
- (#t (var-list-comp (rest a) (rest b)))))
- (define (term-greater? a b)
- (let ((acoef (second a))
- (bcoef (second b))
- (vars-order (var-list-comp (cddr a) (cddr b))))
- (cond
- ;; first check to see if the terms are of differnt order
- ((< vars-order 0) #t)
- ((> vars-order 0) #f)
- ;; next check to see if the coefficients are different
- ((< acoef bcoef) #t)
- (#t #f))))
- (define (normalize-poly p)
- (cons '+
- (sort
- (map normalize-term (rest p))
- term-greater?)))
- (define (qw)
- (var-list-comp '((^ x 3) (^ y 2) (^ x 2)) '((^ x 4) (^ y 2) (^ x 2))))
- (define (qwe)
- (term-greater? '(* 5 (^ x 5) (^ y 4) (^ x 2))
- '(* 5)))
- (define (test)
- (normalize-poly '(+ (* 4 (^ x 3) (^ y 2))
- (* 5 (^ x 3) (^ y 2) (^ x 2))
- (* 10 (^ x 0))
- )))
Add Comment
Please, Sign In to add comment