This file contains hidden or 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
# scheme.py | |
# | |
# Challenge: Can you implement a mini-scheme interpreter? | |
env = { | |
'+' : lambda x, y: x + y, | |
'-' : lambda x, y: x - y, | |
'*' : lambda x, y: x * y, | |
'/' : lambda x, y: x / y, | |
'!=' : lambda x, y: x != y, | |
'=' : lambda x, y: x == y, |
This file contains hidden or 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
assert seval(23, {}) == 23 | |
assert seval("x", {"x": 23}) == 23 | |
assert seval(("+", 1, 2), env) == 3 | |
assert seval(("+", 1, ("*", 2, 3)), env) == 7 | |
seval(("define", "x", 13), env) == 7 | |
assert seval(("x"), env) == 13 | |
assert seval(("if", ("<", 2, 3), 4, 5), env) == 4 | |
assert seval(("if", (">", 2, 3), 4, 5), env) == 5 |
This file contains hidden or 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
class WidthVisitor implements BoxVisitor { | |
int visitBob(BobBox box) { | |
return box.getWidth(); | |
} | |
int visitAlice(AliceBox box) { | |
return Math.abs(box.getX2() - box.getX1()); | |
} | |
} |
This file contains hidden or 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
interface BoxVisitor { | |
int visitBob(BobBox box); | |
int visitAlice(AliceBox box); | |
} | |
... | |
abstract class Box { | |
abstract void accept(BoxVisitor visitor); | |
} |
This file contains hidden or 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 | |
(define (attach-tag tag item) | |
(cons tag item)) | |
(define (contents tagged-item) | |
(cdr tagged-item)) | |
... | |
This file contains hidden or 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 | |
(define (make-alice-box x1 y1 x2 y2) | |
(cons (cons x1 y1) (cons x2 y2))) | |
(define (alice-width box) | |
(abs (- (car (cdr (box)) | |
(car (car (box))))) | |
(define (alice-height box) |
This file contains hidden or 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 | |
(define (make-bob-box x y w h) | |
'bob-box (cons (cons x y) (cons w h))) | |
(define (bob-width box) | |
(car (cdr (box)))) | |
(define (bob-height box) | |
(cdr (cdr (box)))) |
This file contains hidden or 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 | |
(define (make-bob-box x y w h) | |
(attach-tag 'bob-box (cons (cons x y) (cons w h)))) | |
(define (bob-width box) | |
(car (cdr (box)))) | |
(define (bob-height box) | |
(cdr (cdr (box)))) |
This file contains hidden or 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
> (define pair (cons 2 3)) | |
> pair | |
'(2 . 3) | |
> (car pair) | |
2 | |
> (cdr pair) | |
3 |
This file contains hidden or 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
; 1.43 | |
(define (repeated operation n) | |
(define (operation-iter iterator composed-function) | |
(if (= iterator 1) | |
(lambda(x) (composed-function x)) | |
(operation-iter (- iterator 1) (compose operation composed-function)) | |
)) | |
(operation-iter n operation) | |
) |