Scheme Calculator

If your studying computer science, I highly recommend you learn some form of LISP, whether that be Scheme, Clojure, Racket, or Common Lisp. You might never use these languages at your job, but they will teach you a tremendous amount about interpreters, recursion, closures, macros, and mathematics. I built this simple calculator after reading The Structure and Interpretation of Computer Programs by Harold Abelson and Gerald Jay Sussman with Julie Sussman.

(define env
  (list [cons '+ +]
        [cons '- -]
        [cons '* *]
        [cons  *]
        [cons '/ /]
        [cons  /]
        [cons '^ expt]
        [cons '√ sqrt]
        [cons  3.141592653589793]
        [cons 'e 2.718281828459045]
        [cons  1.618033988749894]))

(define (lookup var env)
  (let ([result (assq var env)])
    (if result
        (cdr result)
        (error "unbound operator or constant: " var))))

(define (compute expr)
  (cond
    [(symbol? expr) (lookup expr env)]
    [(number? expr) expr]
    [(pair? expr)
     (apply (compute (car expr))
            (map (lambda (x) (compute x))
                 (cdr expr)))]
    [else (error "invalid expression: " expr)]))
A Scheme calculator and symbolic interpreter.