[racket] Mutable state vs RAM on fire

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed May 2 17:16:29 EDT 2012

I forgot about your desire to remove 0-coefficent terms: 

(define (add-term a n q)
  (if (= a 0) q (cons (cons a n) q)))

(define (poly-simp p)
  (define-values (a n q)
    (for/fold ((a 0) (n 0) (q '())) ((x (sort p < #:key cdr)))
      (define b (car x))
      (define k (cdr x))
      (if (= n k)
          (values (+ a b) n q)
          (values b k (add-term a n q)))))
  (add-term a n q))

Posted on the users mailing list.