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 | |
; Metacircular evaluator with lazy evaluation | |
; New features | |
; "Thunk:" An unevaluated expression along with an environment (where it would evaluate) | |
(define (delay-it sexp env) | |
(list 'thunk sexp env)) | |
(define (thunk? obj) |
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 | |
; Metacircular evaluator | |
; Evaluate a "scheme" expression | |
(define (seval sexp env) | |
(cond ((primitive? sexp) sexp) | |
((symbol? sexp) (lookup-environment env sexp)) | |
; Special forms | |
((define? sexp) (seval-define sexp env)) | |
((if? sexp) (seval-if sexp env)) |
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 ready-to-call (make-queue)) | |
(define (call-soon proc) | |
((ready-to-call 'insert!) proc)) | |
(define (run) | |
(cond ((ready-to-call 'empty?) 'done) | |
(else | |
(let ((proc (ready-to-call 'front))) | |
(proc) ; execute the 0-argument lambda |
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
; 3.22 | |
(define (make-queue) | |
(let ((front-ptr '()) | |
(rear-ptr '())) | |
(define (empty?) | |
(null? front-ptr)) | |
(define (insert! 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
(define (countdown n) | |
(cond ((= n 0) 'countdown-done) | |
(else | |
(display "Down ") | |
(displayln n) | |
(call-soon (lambda () (countdown (- n 1))))))) | |
(define (up stop) | |
(define (iter x) | |
(cond ((> x stop) 'up-done) |
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 ones (stream-cons 1 ones)) ;<---has not evalutated ones yet, as ones is a stream | |
(stream-ref ones 1) ;-->1 | |
(stream-ref ones 10) ;-->1 | |
(stream-ref ones 37) ;-->1 (an infinite stream of ones) | |
(define integers (stream-cons 1 (add-streams ones integers))); <--counts up from 1 | |
(define fibonacci (stream-cons 0 (stream-cons 1 (add-streams (stream-rest fibonacci) fibonacci)))) ; <--Displays fibonacci numbers |
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 Procedure: | |
def __init__(self, parameters, expressions, env): | |
self.parameters = parameters | |
self.expressions = expressions | |
self.env = env # PROCEDURE MUST REMEMBER THE ENVIRONMENT AT THE TIME THAT IT WAS DEFINED | |
def __call__(self, *args): | |
# Args are the argument values | |
# Make a new scope for the local variables | |
local_env = {} |
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_environment.py | |
# | |
# Challenge: Can you add mutable state to scheme?: | |
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
def substitute(exp, name, value): | |
if exp == name: | |
return value | |
elif isinstance(exp, tuple): | |
return tuple([substitute(part, name, value) for part in exp]) | |
else: | |
return exp #Unchanged |
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 Procedure: | |
def __init__(self, parameters, expressions, env): | |
self.parameters = parameters | |
self.expressions = expressions | |
self.env = env | |
def __call__(self, *args): | |
for expression in self.expressions: # Substitute the arguments for parameter names | |
for names, values in zip(self.parameters, args): | |
expression = substitute(expression, names, values) |