Skip to content

Instantly share code, notes, and snippets.

View rgm's full-sized avatar

Ryan McCuaig rgm

View GitHub Profile
@rgm
rgm / explicit-control.scm
Created June 19, 2014 06:14
The explicit-control evaluator from SICP §5.4
;; Explicit control evaluator from SICP 5.4
;; required "machine" operations from underlying Scheme
(define eceval-operations
(list (list 'self-evaluating? self-evaluating)
(list 'variable? variable)
;... etc
;self-evaluating?
;variable?
;quoted?
(define (fib n)
(if (< n 2)
n
(+ (fib (- n 1)) (fib (- n 2)))))
;; becomes
(controller
(assign continue (label fib-done))
fib-loop
@rgm
rgm / logic.scm
Created April 24, 2014 16:32
Query interpreter from SICP §4.4
;; SICP 4.4 Query Interpreter
;; http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-29.html#%_sec_4.4
;; scheme -load logic.scm
;; ==================================================
;; 0 table data structure, see 3.3.3
;; ==================================================
(define (make-table)
(let ((local-table (list '*table*)))
@rgm
rgm / analyzing->nondeterministic.diff
Last active August 29, 2015 13:58
changes needed to turn the analyzing interpreter of SICP 4.1 to the nondeterministic interpreter of SICP 4.3
diff --git a/nondeterministic.scm b/nondeterministic.scm
index 1fab899..52e5b73 100644
--- a/nondeterministic.scm
+++ b/nondeterministic.scm
@@ -1,5 +1,5 @@
-(define (eval exp env)
- ((analyze exp) env))
+(define (eval exp env succeed fail)
+ ((analyze exp) env succeed fail))
@rgm
rgm / nondeterministic.scm
Created April 4, 2014 02:37
Nondeterministic interpreter of SICP 4.3
(define (eval exp env succeed fail)
((analyze exp) env succeed fail))
(define (analyze exp)
(cond ((self-evaluating? exp) (analyze-self-evaluating exp))
((variable? exp) (analyze-variable exp))
((quoted? exp) (analyze-quoted exp))
((assignment? exp) (analyze-assignment exp))
((definition? exp) (analyze-definition exp))
((if? exp) (analyze-if exp))
@rgm
rgm / lazy.diff
Last active August 29, 2015 13:57
diff --git a/metacircular.scm b/metacircular.scm
index 20542d1..97632e5 100644
--- a/metacircular.scm
+++ b/metacircular.scm
@@ -10,26 +10,43 @@
env))
((begin? exp) (eval-sequence (begin-actions exp) env))
((cond? exp) (eval (cond->if exp) env))
- ((application? exp) (apply (eval (operator exp) env)
- (list-of-values (operands exp) env)))
(define (eval exp env)
(cond ((self-evaluating? exp) exp)
((variable? exp) (lookup-variable-value exp env))
((quoted? exp) (text-of-quotation exp))
((assignment? exp) (eval-assignment exp env))
((definition? exp) (eval-definition exp env))
((if? exp) (eval-if exp env))
((lambda? exp) (make-procedure (lambda-parameters exp)
(lambda-body exp)
env))
;; SICP 3.3.4 - Constraint system
(define (for-each-except exception procedure list)
(define (loop items)
(cond ((null? items) 'done)
((eq? (car items) exception) (loop (cdr items)))
(else (procedure (car items))
(loop (cdr items)))))
(loop list))
@rgm
rgm / gist:8627828
Created January 26, 2014 03:12
SICP lecture 5B 55m00s -> end
;; pure smoke immutable cons
(define cons
(lambda (x y)
(lambda (m) (m x y))))
(define car
(lambda (x)
(x (lambda (a d) a))))
@rgm
rgm / gist:8627518
Last active January 4, 2016 13:19
Floyd's algorithm for SICP exercise 3.19
(define (has-cycle? ls)
(define (has-cycle-h tortoise hare)
(cond ((null? hare) #f)
((null? (cdr hare)) #f)
((eq? (car tortoise) (car hare)) #t)
(else
(has-cycle-h (cdr tortoise) (cddr hare)))))
(cond ((null? ls) #f)
(else (has-cycle-h ls (cdr ls)))))