Skip to content

Instantly share code, notes, and snippets.

@lazywithclass
Last active April 29, 2017 23:47
Show Gist options
  • Save lazywithclass/5e82e7e23d7af2ba25a560c9107bd6ab to your computer and use it in GitHub Desktop.
Save lazywithclass/5e82e7e23d7af2ba25a560c9107bd6ab to your computer and use it in GitHub Desktop.
[RC Diary] Scheme interpreter, lambda calculus, and wtf is this (-32)

[RC Diary] Scheme interpreter, lambda calculus, and wtf is this (-32)

tl;dr: dead

(I'm the second from the left)

We expanded the interpreter to allow more cases:

(define eval-expr
  (lambda (expr env)
    (pmatch expr
      [`(if ,c ,p ,a)
       (if (eval-expr c env)
           (eval-expr p env)
           (eval-expr a env))]
      [`,x (guard (or (boolean? x) (number? x)))
        x]
      [`,x (guard (symbol? x))
        (env x)]
      [`(lambda (,x) ,body)
        (lambda (arg)
          (eval-expr body (lambda (y)
                            (if (eq? x y)
                                arg
                                (env y)))))]
      [`(,rator ,rand1 ,rand2)
       ((eval-expr rator env)
        (eval-expr rand1 env)
        (eval-expr rand2 env))]
      [`(,rator ,rand)
       ((eval-expr rator env)
        (eval-expr rand env))])))

if, booleans, numbers, and a function call with two parameters, for example (+ 41 1).

We also called it with a more complicated function:

(eval-expr '(((lambda (rf)
                (lambda (n)
                  ((rf rf) n)))
              (lambda (!)
                (lambda (n)
                  (if (zero? n)
                      1
                      (* n ((! !) (sub1 n)))))))
             5)
           (lambda (arg)
             (cond
               ((eq? arg 'zero?) zero?)
               ((eq? arg 'sub1) sub1)
               ((eq? arg '*) *)
               ((eq? arg '+) +)
               (else (environment arg)))))

I am going to talk about this in a future blog post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment