Last active
October 12, 2020 16:48
-
-
Save seisvelas/bd97e2afb6f96ed466e19098f66b9778 to your computer and use it in GitHub Desktop.
Solution to cassidoo newsletter baby lisp problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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