Skip to content

Instantly share code, notes, and snippets.

@seisvelas
Last active October 12, 2020 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seisvelas/bd97e2afb6f96ed466e19098f66b9778 to your computer and use it in GitHub Desktop.
Save seisvelas/bd97e2afb6f96ed466e19098f66b9778 to your computer and use it in GitHub Desktop.
Solution to cassidoo newsletter baby lisp problem
#lang racket/base
; unit tests
(require rackunit)
; Give (eval expr namespace) access to
; the current namespace via the label ns
(define-namespace-anchor a)
(define ns (namespace-anchor->namespace a))
(define add +)
(define subtract -)
(define multiply *)
(define divide /)
(define (baby-lisp expr)
(eval expr ns))
(check-equal?
30 (baby-lisp '(multiply 5 6)))
(check-equal?
-1 (baby-lisp '(subtract 5 6)))
(check-equal?
5/6 (baby-lisp '(divide 5 6)))
(check-equal?
11 (baby-lisp '(add 5 6)))
;5 * (6 + 5 - (5 / 6)) = 50.833...
(define complex-baby-lisp-expr
'(multiply
5 (add
6 (subtract
5 (divide 5 6)))))
(check-equal?
50 5/6 (baby-lisp complex-baby-lisp-expr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment