Guest User

Untitled

a guest
May 18th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.71 KB | None | 0 0
  1. #lang scheme
  2.  
  3. (define (p= a b)
  4.   (cond
  5.     ((and (number? a)(number? b)) (= a b))
  6.     ((and (list? a)(list? b)) (equal? a b))
  7.     (#t #f)))
  8.  
  9.  
  10.  
  11. (define (variable-greater? a b)
  12.   (let ((avar (second a))
  13.         (aexp (third a))
  14.         (bvar (second b))
  15.         (bexp (third b)))
  16.     (cond
  17.       ((> aexp bexp) #t)
  18.       ((< aexp bexp) #f) ;; after this it means the exps are equal
  19.       ((string<? (symbol->string avar) (symbol->string bvar)) #t)
  20.       (#t #f))))
  21.  
  22.  
  23. (define (normalize-term t)
  24.     (append (list '* (second t))
  25.             (sort (cddr t) variable-greater?)))
  26.  
  27.  
  28. (define (var-list-comp a b)
  29.   (cond
  30.     ((and (empty? a) (empty? b)) 0)
  31.     ((empty? a) -1)
  32.     ((empty? b) 1)
  33.     ((variable-greater? (car a) (car b)) -1)
  34.     ((variable-greater? (car b) (car a)) 1)
  35.     (#t (var-list-comp (rest a) (rest b)))))
  36.  
  37.  
  38. (define (term-greater? a b)
  39.   (let ((acoef (second a))
  40.         (bcoef (second b))
  41.         (vars-order (var-list-comp (cddr a) (cddr b))))
  42.     (cond
  43.       ;; first check to see if the terms are of differnt order
  44.       ((< vars-order 0) #t)
  45.       ((> vars-order 0) #f)
  46.       ;; next check to see if the coefficients are different
  47.       ((< acoef bcoef) #t)
  48.       (#t #f))))
  49.      
  50.  
  51. (define (normalize-poly p)
  52.   (cons '+
  53.         (sort
  54.          (map normalize-term (rest p))
  55.          term-greater?)))
  56.  
  57.  
  58.  
  59.          
  60. (define (qw)
  61.   (var-list-comp '((^ x 3) (^ y 2) (^ x 2)) '((^ x 4) (^ y 2) (^ x 2))))
  62.  
  63.  
  64. (define (qwe)
  65.   (term-greater? '(* 5 (^ x 5) (^ y 4) (^ x 2))
  66.               '(* 5)))
  67.  
  68. (define (test)
  69.   (normalize-poly '(+ (* 4 (^ x 3) (^ y 2))
  70.                       (* 5 (^ x 3) (^ y 2) (^ x 2))
  71.                       (* 10 (^ x 0))
  72.                       )))
Add Comment
Please, Sign In to add comment