diff options
author | elioat <elioat@tilde.institute> | 2023-08-23 07:52:19 -0400 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2023-08-23 07:52:19 -0400 |
commit | 562a9a52d599d9a05f871404050968a5fd282640 (patch) | |
tree | 7d3305c1252c043bfe246ccc7deff0056aa6b5ab /js/games/nluqo.github.io/~bh/61a-pages/Lib | |
parent | 5d012c6c011a9dedf7d0a098e456206244eb5a0f (diff) | |
download | tour-562a9a52d599d9a05f871404050968a5fd282640.tar.gz |
*
Diffstat (limited to 'js/games/nluqo.github.io/~bh/61a-pages/Lib')
51 files changed, 13228 insertions, 0 deletions
diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/adv-world.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/adv-world.scm new file mode 100644 index 0000000..bc4eb8c --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/adv-world.scm @@ -0,0 +1,83 @@ +;;; Data for adventure game. This file is adv-world.scm + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; setting up the world +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(define Soda (instantiate place 'Soda)) +(define BH-Office (instantiate place 'BH-Office)) +(define MJC-Office (instantiate place 'MJC-Office)) +(define art-gallery (instantiate place 'art-gallery)) +(define Pimentel (instantiate place 'Pimentel)) +(define 61A-Lab (instantiate place '61A-Lab)) +(define Sproul-Plaza (instantiate place 'Sproul-Plaza)) +(define Telegraph-Ave (instantiate place 'Telegraph-Ave)) +(define Noahs (instantiate place 'Noahs)) +(define Intermezzo (instantiate place 'Intermezzo)) +(define s-h (instantiate place 'sproul-hall)) + + +(can-go Soda 'up art-gallery) +(can-go art-gallery 'down Soda) +(can-go art-gallery 'west BH-Office) +(can-go BH-Office 'east art-gallery) +(can-go art-gallery 'east MJC-Office) +(can-go MJC-office 'west art-gallery) +(can-go Soda 'south Pimentel) +(can-go Pimentel 'north Soda) +(can-go Pimentel 'south 61A-Lab) +(can-go 61A-Lab 'north Pimentel) +(can-go 61A-Lab 'west s-h) +(can-go s-h 'east 61A-Lab) +(can-go Sproul-Plaza 'east s-h) +(can-go s-h 'west Sproul-Plaza) +(can-go Sproul-Plaza 'north Pimentel) +(can-go Sproul-Plaza 'south Telegraph-Ave) +(can-go Telegraph-Ave 'north Sproul-Plaza) +(can-go Telegraph-Ave 'south Noahs) +(can-go Noahs 'north Telegraph-Ave) +(can-go Noahs 'south Intermezzo) +(can-go Intermezzo 'north Noahs) + +;; Some people. +; MOVED above the add-entry-procedure stuff, to avoid the "The computers +; seem to be down" message that would occur when hacker enters 61a-lab +; -- Ryan Stejskal + +(define Brian (instantiate person 'Brian BH-Office)) +(define hacker (instantiate person 'hacker 61A-lab)) +(define nasty (instantiate thief 'nasty sproul-plaza)) + +(define (sproul-hall-exit) + (error "You can check out any time you'd like, but you can never leave")) + +(define (bh-office-exit) + (print "What's your favorite programming language?") + (let ((answer (read))) + (if (eq? answer 'scheme) + (print "Good answer, but my favorite is Logo!") + (begin (newline) (bh-office-exit))))) + + +(ask s-h 'add-entry-procedure + (lambda () (print "Miles and miles of students are waiting in line..."))) +(ask s-h 'add-exit-procedure sproul-hall-exit) +(ask BH-Office 'add-exit-procedure bh-office-exit) +(ask Noahs 'add-entry-procedure + (lambda () (print "Would you like lox with it?"))) +(ask Noahs 'add-exit-procedure + (lambda () (print "How about a cinnamon raisin bagel for dessert?"))) +(ask Telegraph-Ave 'add-entry-procedure + (lambda () (print "There are tie-dyed shirts as far as you can see..."))) +(ask 61A-Lab 'add-entry-procedure + (lambda () (print "The computers seem to be down"))) +(ask 61A-Lab 'add-exit-procedure + (lambda () (print "The workstations come back to life just in time."))) + +;; Some things. + +(define bagel (instantiate thing 'bagel)) +(ask Noahs 'appear bagel) + +(define coffee (instantiate thing 'coffee)) +(ask Intermezzo 'appear coffee) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/adv.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/adv.scm new file mode 100644 index 0000000..f4a94cb --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/adv.scm @@ -0,0 +1,235 @@ +;; ADV.SCM +;; This file contains the definitions for the objects in the adventure +;; game and some utility procedures. + +(define-class (place name) + (instance-vars + (directions-and-neighbors '()) + (things '()) + (people '()) + (entry-procs '()) + (exit-procs '())) + (method (type) 'place) + (method (neighbors) (map cdr directions-and-neighbors)) + (method (exits) (map car directions-and-neighbors)) + (method (look-in direction) + (let ((pair (assoc direction directions-and-neighbors))) + (if (not pair) + '() ;; nothing in that direction + (cdr pair)))) ;; return the place object + (method (appear new-thing) + (if (memq new-thing things) + (error "Thing already in this place" (list name new-thing))) + (set! things (cons new-thing things)) + 'appeared) + (method (enter new-person) + (if (memq new-person people) + (error "Person already in this place" (list name new-person))) + (set! people (cons new-person people)) + (for-each (lambda (proc) (proc)) entry-procs) + 'appeared) + (method (gone thing) + (if (not (memq thing things)) + (error "Disappearing thing not here" (list name thing))) + (set! things (delete thing things)) + 'disappeared) + (method (exit person) + (for-each (lambda (proc) (proc)) exit-procs) + (if (not (memq person people)) + (error "Disappearing person not here" (list name person))) + (set! people (delete person people)) + 'disappeared) + + (method (new-neighbor direction neighbor) + (if (assoc direction directions-and-neighbors) + (error "Direction already assigned a neighbor" (list name direction))) + (set! directions-and-neighbors + (cons (cons direction neighbor) directions-and-neighbors)) + 'connected) + + (method (add-entry-procedure proc) + (set! entry-procs (cons proc entry-procs))) + (method (add-exit-procedure proc) + (set! exit-procs (cons proc exit-procs))) + (method (remove-entry-procedure proc) + (set! entry-procs (delete proc entry-procs))) + (method (remove-exit-procedure proc) + (set! exit-procs (delete proc exit-procs))) + (method (clear-all-procs) + (set! exit-procs '()) + (set! entry-procs '()) + 'cleared) ) + +(define-class (person name place) + (instance-vars + (possessions '()) + (saying "")) + (initialize + (ask place 'enter self)) + (method (type) 'person) + (method (look-around) + (map (lambda (obj) (ask obj 'name)) + (filter (lambda (thing) (not (eq? thing self))) + (append (ask place 'things) (ask place 'people))))) + (method (take thing) + (cond ((not (thing? thing)) (error "Not a thing" thing)) + ((not (memq thing (ask place 'things))) + (error "Thing taken not at this place" + (list (ask place 'name) thing))) + ((memq thing possessions) (error "You already have it!")) + (else + (announce-take name thing) + (set! possessions (cons thing possessions)) + + ;; If somebody already has this object... + (for-each + (lambda (pers) + (if (and (not (eq? pers self)) ; ignore myself + (memq thing (ask pers 'possessions))) + (begin + (ask pers 'lose thing) + (have-fit pers)))) + (ask place 'people)) + + (ask thing 'change-possessor self) + 'taken))) + + (method (lose thing) + (set! possessions (delete thing possessions)) + (ask thing 'change-possessor 'no-one) + 'lost) + (method (talk) (print saying)) + (method (set-talk string) (set! saying string)) + (method (exits) (ask place 'exits)) + (method (notice person) (ask self 'talk)) + (method (go direction) + (let ((new-place (ask place 'look-in direction))) + (cond ((null? new-place) + (error "Can't go" direction)) + (else + (ask place 'exit self) + (announce-move name place new-place) + (for-each + (lambda (p) + (ask place 'gone p) + (ask new-place 'appear p)) + possessions) + (set! place new-place) + (ask new-place 'enter self))))) ) + +(define thing + (let () + (lambda (class-message) + (cond + ((eq? class-message 'instantiate) + (lambda (name) + (let ((self '()) (possessor 'no-one)) + (define (dispatch message) + (cond + ((eq? message 'initialize) + (lambda (value-for-self) + (set! self value-for-self))) + ((eq? message 'send-usual-to-parent) + (error "Can't use USUAL without a parent." 'thing)) + ((eq? message 'name) (lambda () name)) + ((eq? message 'possessor) (lambda () possessor)) + ((eq? message 'type) (lambda () 'thing)) + ((eq? message 'change-possessor) + (lambda (new-possessor) + (set! possessor new-possessor))) + (else (no-method 'thing)))) + dispatch))) + (else (error "Bad message to class" class-message)))))) + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Implementation of thieves for part two +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(define *foods* '(pizza potstickers coffee)) + +(define (edible? thing) + (member? (ask thing 'name) *foods*)) + +(define-class (thief name initial-place) + (parent (person name initial-place)) + (instance-vars + (behavior 'steal)) + (method (type) 'thief) + + (method (notice person) + (if (eq? behavior 'run) + (ask self 'go (pick-random (ask (usual 'place) 'exits))) + (let ((food-things + (filter (lambda (thing) + (and (edible? thing) + (not (eq? (ask thing 'possessor) self)))) + (ask (usual 'place) 'things)))) + (if (not (null? food-things)) + (begin + (ask self 'take (car food-things)) + (set! behavior 'run) + (ask self 'notice person)) )))) ) + +;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Utility procedures +;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;;; this next procedure is useful for moving around + +(define (move-loop who) + (newline) + (print (ask who 'exits)) + (display "? > ") + (let ((dir (read))) + (if (equal? dir 'stop) + (newline) + (begin (ask who 'go dir) + (move-loop who))))) + + +;; One-way paths connect individual places. + +(define (can-go from direction to) + (ask from 'new-neighbor direction to)) + + +(define (announce-take name thing) + (newline) + (display name) + (display " took ") + (display (ask thing 'name)) + (newline)) + +(define (announce-move name old-place new-place) + (newline) + (newline) + (display name) + (display " moved from ") + (display (ask old-place 'name)) + (display " to ") + (display (ask new-place 'name)) + (newline)) + +(define (have-fit p) + (newline) + (display "Yaaah! ") + (display (ask p 'name)) + (display " is upset!") + (newline)) + + +(define (pick-random set) + (nth (random (length set)) set)) + +(define (delete thing stuff) + (cond ((null? stuff) '()) + ((eq? thing (car stuff)) (cdr stuff)) + (else (cons (car stuff) (delete thing (cdr stuff)))) )) + +(define (person? obj) + (and (procedure? obj) + (member? (ask obj 'type) '(person police thief)))) + +(define (thing? obj) + (and (procedure? obj) + (eq? (ask obj 'type) 'thing))) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/ambdiff b/js/games/nluqo.github.io/~bh/61a-pages/Lib/ambdiff new file mode 100644 index 0000000..4d76cda --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/ambdiff @@ -0,0 +1,309 @@ +1,2c1,2 +< ;;;;METACIRCULAR EVALUATOR THAT SEPARATES ANALYSIS FROM EXECUTION +< ;;;; FROM SECTION 4.1.7 OF STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS +--- +> ;;;;AMB EVALUATOR FROM SECTION 4.3 OF +> ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS +4c4,7 +< ;;;;Matches code in ch4.scm +--- +> ;;;;Matches code in ch4.scm. +> ;;;; To run the sample programs and exercises, code below also includes +> ;;;; -- enlarged primitive-procedures list +> ;;;; -- support for Let (as noted in footnote 56, p.428) +18c21 +< ;;Note: It is loaded first so that the section 4.1.7 definition +--- +> ;;Note: It is loaded first so that the section 4.2 definition +20c23 +< (load "61a/lib/mceval.scm") +--- +> (load "~/61a/lib/mceval.scm") +22d24 +< ;;;SECTION 4.1.7 +24,25d25 +< (define (mc-eval exp env) +< ((analyze exp) env)) +26a27,33 +> ;;;Code from SECTION 4.3.3, modified as needed to run it +> +> (define (amb? exp) (tagged-list? exp 'amb)) +> (define (amb-choices exp) (cdr exp)) +> +> ;; analyze from 4.1.6, with clause from 4.3.3 added +> ;; and also support for Let +37a45,46 +> ((let? exp) (analyze (let->combination exp))) ;** +> ((amb? exp) (analyze-amb exp)) ;** +41a51,55 +> (define (ambeval exp env succeed fail) +> ((analyze exp) env succeed fail)) +> +> ;;;Simple expressions +> +43c57,58 +< (lambda (env) exp)) +--- +> (lambda (env succeed fail) +> (succeed exp fail))) +47c62,63 +< (lambda (env) qval))) +--- +> (lambda (env succeed fail) +> (succeed qval fail)))) +50c66,68 +< (lambda (env) (lookup-variable-value exp env))) +--- +> (lambda (env succeed fail) +> (succeed (lookup-variable-value exp env) +> fail))) +52,57c70,75 +< (define (analyze-assignment exp) +< (let ((var (assignment-variable exp)) +< (vproc (analyze (assignment-value exp)))) +< (lambda (env) +< (set-variable-value! var (vproc env) env) +< 'ok))) +--- +> (define (analyze-lambda exp) +> (let ((vars (lambda-parameters exp)) +> (bproc (analyze-sequence (lambda-body exp)))) +> (lambda (env succeed fail) +> (succeed (make-procedure vars bproc env) +> fail)))) +59,64c77 +< (define (analyze-definition exp) +< (let ((var (definition-variable exp)) +< (vproc (analyze (definition-value exp)))) +< (lambda (env) +< (define-variable! var (vproc env) env) +< 'ok))) +--- +> ;;;Conditionals and sequences +70,73c83,92 +< (lambda (env) +< (if (true? (pproc env)) +< (cproc env) +< (aproc env))))) +--- +> (lambda (env succeed fail) +> (pproc env +> ;; success continuation for evaluating the predicate +> ;; to obtain pred-value +> (lambda (pred-value fail2) +> (if (true? pred-value) +> (cproc env succeed fail2) +> (aproc env succeed fail2))) +> ;; failure continuation for evaluating the predicate +> fail)))) +75,79d93 +< (define (analyze-lambda exp) +< (let ((vars (lambda-parameters exp)) +< (bproc (analyze-sequence (lambda-body exp)))) +< (lambda (env) (make-procedure vars bproc env)))) +< +81,82c95,102 +< (define (sequentially proc1 proc2) +< (lambda (env) (proc1 env) (proc2 env))) +--- +> (define (sequentially a b) +> (lambda (env succeed fail) +> (a env +> ;; success continuation for calling a +> (lambda (a-value fail2) +> (b env succeed fail2)) +> ;; failure continuation for calling a +> fail))) +92a113,143 +> ;;;Definitions and assignments +> +> (define (analyze-definition exp) +> (let ((var (definition-variable exp)) +> (vproc (analyze (definition-value exp)))) +> (lambda (env succeed fail) +> (vproc env +> (lambda (val fail2) +> (define-variable! var val env) +> (succeed 'ok fail2)) +> fail)))) +> +> (define (analyze-assignment exp) +> (let ((var (assignment-variable exp)) +> (vproc (analyze (assignment-value exp)))) +> (lambda (env succeed fail) +> (vproc env +> (lambda (val fail2) ; *1* +> (let ((old-value +> (lookup-variable-value var env))) +> (set-variable-value! var val env) +> (succeed 'ok +> (lambda () ; *2* +> (set-variable-value! var +> old-value +> env) +> (fail2))))) +> fail)))) +> +> ;;;Procedure applications +> +96,99c147,156 +< (lambda (env) +< (execute-application (fproc env) +< (map (lambda (aproc) (aproc env)) +< aprocs))))) +--- +> (lambda (env succeed fail) +> (fproc env +> (lambda (proc fail2) +> (get-args aprocs +> env +> (lambda (args fail3) +> (execute-application +> proc args succeed fail3)) +> fail2)) +> fail)))) +101c158,174 +< (define (execute-application proc args) +--- +> (define (get-args aprocs env succeed fail) +> (if (null? aprocs) +> (succeed '() fail) +> ((car aprocs) env +> ;; success continuation for this aproc +> (lambda (arg fail2) +> (get-args (cdr aprocs) +> env +> ;; success continuation for recursive +> ;; call to get-args +> (lambda (args fail3) +> (succeed (cons arg args) +> fail3)) +> fail2)) +> fail))) +> +> (define (execute-application proc args succeed fail) +103c176,177 +< (apply-primitive-procedure proc args)) +--- +> (succeed (apply-primitive-procedure proc args) +> fail)) +108c182,184 +< (procedure-environment proc)))) +--- +> (procedure-environment proc)) +> succeed +> fail)) +113a190,291 +> ;;;amb expressions +> +> (define (analyze-amb exp) +> (let ((cprocs (map analyze (amb-choices exp)))) +> (lambda (env succeed fail) +> (define (try-next choices) +> (if (null? choices) +> (fail) +> ((car choices) env +> succeed +> (lambda () +> (try-next (cdr choices)))))) +> (try-next cprocs)))) +> +> ;;;Driver loop +> +> (define input-prompt ";;; Amb-Eval input:") +> (define output-prompt ";;; Amb-Eval value:") +> +> (define (driver-loop) +> (define (internal-loop try-again) +> (prompt-for-input input-prompt) +> (let ((input (read))) +> (if (eq? input 'try-again) +> (try-again) +> (begin +> (newline) +> (display ";;; Starting a new problem ") +> (ambeval input +> the-global-environment +> ;; ambeval success +> (lambda (val next-alternative) +> (announce-output output-prompt) +> (user-print val) +> (internal-loop next-alternative)) +> ;; ambeval failure +> (lambda () +> (announce-output +> ";;; There are no more values of") +> (user-print input) +> (driver-loop))))))) +> (internal-loop +> (lambda () +> (newline) +> (display ";;; There is no current problem") +> (driver-loop)))) +> +> +> +> ;;; Support for Let (as noted in footnote 56, p.428) +> +> (define (let? exp) (tagged-list? exp 'let)) +> (define (let-bindings exp) (cadr exp)) +> (define (let-body exp) (cddr exp)) +> +> (define (let-var binding) (car binding)) +> (define (let-val binding) (cadr binding)) +> +> (define (make-combination operator operands) (cons operator operands)) +> +> (define (let->combination exp) +> ;;make-combination defined in earlier exercise +> (let ((bindings (let-bindings exp))) +> (make-combination (make-lambda (map let-var bindings) +> (let-body exp)) +> (map let-val bindings)))) +> +> +> +> ;; A longer list of primitives -- suitable for running everything in 4.3 +> ;; Overrides the list in ch4-mceval.scm +> ;; Has Not to support Require; various stuff for code in text (including +> ;; support for Prime?); integer? and sqrt for exercise code; +> ;; eq? for ex. solution +> +> (define primitive-procedures +> (list (list 'car car) +> (list 'cdr cdr) +> (list 'cons cons) +> (list 'null? null?) +> (list 'list list) +> (list 'append append) +> (list 'memq memq) +> (list 'member member) +> (list 'not not) +> (list '+ +) +> (list '- -) +> (list '* *) +> (list '= =) +> (list '> >) +> (list '>= >=) +> (list 'abs abs) +> (list 'remainder remainder) +> (list 'integer? integer?) +> (list 'sqrt sqrt) +> (list 'eq? eq?) +> (list 'equal? equal?) +> (list 'pair? pair?) +> ;; more primitives +> )) +> +> +116,117c294,300 +< (define input-prompt ";;; A-Eval input:") +< (define output-prompt ";;; A-Eval value:") +--- +> (define (mce) +> (set! the-global-environment (setup-environment)) +> (ambeval '(define (require p) (if (not p) (amb))) +> the-global-environment +> (lambda (a b) #t) +> (lambda () #t)) +> (driver-loop)) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/ambeval.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/ambeval.scm new file mode 100644 index 0000000..e9fbeb2 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/ambeval.scm @@ -0,0 +1,300 @@ +;;;;AMB EVALUATOR FROM SECTION 4.3 OF +;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS + +;;;;Matches code in ch4.scm. +;;;; To run the sample programs and exercises, code below also includes +;;;; -- enlarged primitive-procedures list +;;;; -- support for Let (as noted in footnote 56, p.428) + +;;;;This file can be loaded into Scheme as a whole. +;;;;**NOTE**This file loads the metacircular evaluator of +;;;; sections 4.1.1-4.1.4, since it uses the expression representation, +;;;; environment representation, etc. +;;;; You may need to change the (load ...) expression to work in your +;;;; version of Scheme. + +;;;;Then you can initialize and start the evaluator by evaluating +;;;; the expression (mce). + + +;;**implementation-dependent loading of evaluator file +;;Note: It is loaded first so that the section 4.2 definition +;; of eval overrides the definition from 4.1.1 +(load "~/61a/Lib/mceval.scm") + + + +;;;Code from SECTION 4.3.3, modified as needed to run it + +(define (amb? exp) (tagged-list? exp 'amb)) +(define (amb-choices exp) (cdr exp)) + +;; analyze from 4.1.6, with clause from 4.3.3 added +;; and also support for Let +(define (analyze exp) + (cond ((self-evaluating? exp) + (analyze-self-evaluating exp)) + ((quoted? exp) (analyze-quoted exp)) + ((variable? exp) (analyze-variable exp)) + ((assignment? exp) (analyze-assignment exp)) + ((definition? exp) (analyze-definition exp)) + ((if? exp) (analyze-if exp)) + ((lambda? exp) (analyze-lambda exp)) + ((begin? exp) (analyze-sequence (begin-actions exp))) + ((cond? exp) (analyze (cond->if exp))) + ((let? exp) (analyze (let->combination exp))) ;** + ((amb? exp) (analyze-amb exp)) ;** + ((application? exp) (analyze-application exp)) + (else + (error "Unknown expression type -- ANALYZE" exp)))) + +(define (ambeval exp env succeed fail) + ((analyze exp) env succeed fail)) + +;;;Simple expressions + +(define (analyze-self-evaluating exp) + (lambda (env succeed fail) + (succeed exp fail))) + +(define (analyze-quoted exp) + (let ((qval (text-of-quotation exp))) + (lambda (env succeed fail) + (succeed qval fail)))) + +(define (analyze-variable exp) + (lambda (env succeed fail) + (succeed (lookup-variable-value exp env) + fail))) + +(define (analyze-lambda exp) + (let ((vars (lambda-parameters exp)) + (bproc (analyze-sequence (lambda-body exp)))) + (lambda (env succeed fail) + (succeed (make-procedure vars bproc env) + fail)))) + +;;;Conditionals and sequences + +(define (analyze-if exp) + (let ((pproc (analyze (if-predicate exp))) + (cproc (analyze (if-consequent exp))) + (aproc (analyze (if-alternative exp)))) + (lambda (env succeed fail) + (pproc env + ;; success continuation for evaluating the predicate + ;; to obtain pred-value + (lambda (pred-value fail2) + (if (true? pred-value) + (cproc env succeed fail2) + (aproc env succeed fail2))) + ;; failure continuation for evaluating the predicate + fail)))) + +(define (analyze-sequence exps) + (define (sequentially a b) + (lambda (env succeed fail) + (a env + ;; success continuation for calling a + (lambda (a-value fail2) + (b env succeed fail2)) + ;; failure continuation for calling a + fail))) + (define (loop first-proc rest-procs) + (if (null? rest-procs) + first-proc + (loop (sequentially first-proc (car rest-procs)) + (cdr rest-procs)))) + (let ((procs (map analyze exps))) + (if (null? procs) + (error "Empty sequence -- ANALYZE")) + (loop (car procs) (cdr procs)))) + +;;;Definitions and assignments + +(define (analyze-definition exp) + (let ((var (definition-variable exp)) + (vproc (analyze (definition-value exp)))) + (lambda (env succeed fail) + (vproc env + (lambda (val fail2) + (define-variable! var val env) + (succeed 'ok fail2)) + fail)))) + +(define (analyze-assignment exp) + (let ((var (assignment-variable exp)) + (vproc (analyze (assignment-value exp)))) + (lambda (env succeed fail) + (vproc env + (lambda (val fail2) ; *1* + (let ((old-value + (lookup-variable-value var env))) + (set-variable-value! var val env) + (succeed 'ok + (lambda () ; *2* + (set-variable-value! var + old-value + env) + (fail2))))) + fail)))) + +;;;Procedure applications + +(define (analyze-application exp) + (let ((fproc (analyze (operator exp))) + (aprocs (map analyze (operands exp)))) + (lambda (env succeed fail) + (fproc env + (lambda (proc fail2) + (get-args aprocs + env + (lambda (args fail3) + (execute-application + proc args succeed fail3)) + fail2)) + fail)))) + +(define (get-args aprocs env succeed fail) + (if (null? aprocs) + (succeed '() fail) + ((car aprocs) env + ;; success continuation for this aproc + (lambda (arg fail2) + (get-args (cdr aprocs) + env + ;; success continuation for recursive + ;; call to get-args + (lambda (args fail3) + (succeed (cons arg args) + fail3)) + fail2)) + fail))) + +(define (execute-application proc args succeed fail) + (cond ((primitive-procedure? proc) + (succeed (apply-primitive-procedure proc args) + fail)) + ((compound-procedure? proc) + ((procedure-body proc) + (extend-environment (procedure-parameters proc) + args + (procedure-environment proc)) + succeed + fail)) + (else + (error + "Unknown procedure type -- EXECUTE-APPLICATION" + proc)))) + +;;;amb expressions + +(define (analyze-amb exp) + (let ((cprocs (map analyze (amb-choices exp)))) + (lambda (env succeed fail) + (define (try-next choices) + (if (null? choices) + (fail) + ((car choices) env + succeed + (lambda () + (try-next (cdr choices)))))) + (try-next cprocs)))) + +;;;Driver loop + +(define input-prompt ";;; Amb-Eval input:") +(define output-prompt ";;; Amb-Eval value:") + +(define (driver-loop) + (define (internal-loop try-again) + (prompt-for-input input-prompt) + (let ((input (read))) + (if (eq? input 'try-again) + (try-again) + (begin + (newline) + (display ";;; Starting a new problem ") + (ambeval input + the-global-environment + ;; ambeval success + (lambda (val next-alternative) + (announce-output output-prompt) + (user-print val) + (internal-loop next-alternative)) + ;; ambeval failure + (lambda () + (announce-output + ";;; There are no more values of") + (user-print input) + (driver-loop))))))) + (internal-loop + (lambda () + (newline) + (display ";;; There is no current problem") + (driver-loop)))) + + + +;;; Support for Let (as noted in footnote 56, p.428) + +(define (let? exp) (tagged-list? exp 'let)) +(define (let-bindings exp) (cadr exp)) +(define (let-body exp) (cddr exp)) + +(define (let-var binding) (car binding)) +(define (let-val binding) (cadr binding)) + +(define (make-combination operator operands) (cons operator operands)) + +(define (let->combination exp) + ;;make-combination defined in earlier exercise + (let ((bindings (let-bindings exp))) + (make-combination (make-lambda (map let-var bindings) + (let-body exp)) + (map let-val bindings)))) + + + +;; A longer list of primitives -- suitable for running everything in 4.3 +;; Overrides the list in ch4-mceval.scm +;; Has Not to support Require; various stuff for code in text (including +;; support for Prime?); integer? and sqrt for exercise code; +;; eq? for ex. solution + +(define primitive-procedures + (list (list 'car car) + (list 'cdr cdr) + (list 'cons cons) + (list 'null? null?) + (list 'list list) + (list 'append append) + (list 'memq memq) + (list 'member member) + (list 'not not) + (list '+ +) + (list '- -) + (list '* *) + (list '= =) + (list '> >) + (list '>= >=) + (list 'abs abs) + (list 'remainder remainder) + (list 'integer? integer?) + (list 'sqrt sqrt) + (list 'eq? eq?) + (list 'equal? equal?) + (list 'pair? pair?) +;; more primitives + )) + + +;;; Added at Berkeley: + +(define (mce) + (set! the-global-environment (setup-environment)) + (ambeval '(define (require p) (if (not p) (amb))) + the-global-environment + (lambda (a b) #t) + (lambda () #t)) + (driver-loop)) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/analyze.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/analyze.scm new file mode 100644 index 0000000..3fee1cc --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/analyze.scm @@ -0,0 +1,117 @@ +;;;;METACIRCULAR EVALUATOR THAT SEPARATES ANALYSIS FROM EXECUTION +;;;; FROM SECTION 4.1.7 OF STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS + +;;;;Matches code in ch4.scm + +;;;;This file can be loaded into Scheme as a whole. +;;;;**NOTE**This file loads the metacircular evaluator of +;;;; sections 4.1.1-4.1.4, since it uses the expression representation, +;;;; environment representation, etc. +;;;; You may need to change the (load ...) expression to work in your +;;;; version of Scheme. + +;;;;Then you can initialize and start the evaluator by evaluating +;;;; the expression (mce). + + +;;**implementation-dependent loading of evaluator file +;;Note: It is loaded first so that the section 4.1.7 definition +;; of eval overrides the definition from 4.1.1 +(load "61a/Lib/mceval.scm") + +;;;SECTION 4.1.7 + +(define (mc-eval exp env) + ((analyze exp) env)) + +(define (analyze exp) + (cond ((self-evaluating? exp) + (analyze-self-evaluating exp)) + ((quoted? exp) (analyze-quoted exp)) + ((variable? exp) (analyze-variable exp)) + ((assignment? exp) (analyze-assignment exp)) + ((definition? exp) (analyze-definition exp)) + ((if? exp) (analyze-if exp)) + ((lambda? exp) (analyze-lambda exp)) + ((begin? exp) (analyze-sequence (begin-actions exp))) + ((cond? exp) (analyze (cond->if exp))) + ((application? exp) (analyze-application exp)) + (else + (error "Unknown expression type -- ANALYZE" exp)))) + +(define (analyze-self-evaluating exp) + (lambda (env) exp)) + +(define (analyze-quoted exp) + (let ((qval (text-of-quotation exp))) + (lambda (env) qval))) + +(define (analyze-variable exp) + (lambda (env) (lookup-variable-value exp env))) + +(define (analyze-assignment exp) + (let ((var (assignment-variable exp)) + (vproc (analyze (assignment-value exp)))) + (lambda (env) + (set-variable-value! var (vproc env) env) + 'ok))) + +(define (analyze-definition exp) + (let ((var (definition-variable exp)) + (vproc (analyze (definition-value exp)))) + (lambda (env) + (define-variable! var (vproc env) env) + 'ok))) + +(define (analyze-if exp) + (let ((pproc (analyze (if-predicate exp))) + (cproc (analyze (if-consequent exp))) + (aproc (analyze (if-alternative exp)))) + (lambda (env) + (if (true? (pproc env)) + (cproc env) + (aproc env))))) + +(define (analyze-lambda exp) + (let ((vars (lambda-parameters exp)) + (bproc (analyze-sequence (lambda-body exp)))) + (lambda (env) (make-procedure vars bproc env)))) + +(define (analyze-sequence exps) + (define (sequentially proc1 proc2) + (lambda (env) (proc1 env) (proc2 env))) + (define (loop first-proc rest-procs) + (if (null? rest-procs) + first-proc + (loop (sequentially first-proc (car rest-procs)) + (cdr rest-procs)))) + (let ((procs (map analyze exps))) + (if (null? procs) + (error "Empty sequence -- ANALYZE")) + (loop (car procs) (cdr procs)))) + +(define (analyze-application exp) + (let ((fproc (analyze (operator exp))) + (aprocs (map analyze (operands exp)))) + (lambda (env) + (execute-application (fproc env) + (map (lambda (aproc) (aproc env)) + aprocs))))) + +(define (execute-application proc args) + (cond ((primitive-procedure? proc) + (apply-primitive-procedure proc args)) + ((compound-procedure? proc) + ((procedure-body proc) + (extend-environment (procedure-parameters proc) + args + (procedure-environment proc)))) + (else + (error + "Unknown procedure type -- EXECUTE-APPLICATION" + proc)))) + +;;; Added at Berkeley: + +(define input-prompt ";;; A-Eval input:") +(define output-prompt ";;; A-Eval value:") diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/animal.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/animal.scm new file mode 100644 index 0000000..424cacc --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/animal.scm @@ -0,0 +1,66 @@ +(define (animal node) + (define (type node) (car node)) + (define (question node) (cadr node)) + (define (yespart node) (caddr node)) + (define (nopart node) (cadddr node)) + (define (answer node) (cadr node)) + (define (leaf? node) (eq? (type node) 'leaf)) + (define (branch? node) (eq? (type node) 'branch)) + (define (set-yes! node x) + (set-car! (cddr node) x)) + (define (set-no! node x) + (set-car! (cdddr node) x)) + + (define (yorn) + (let ((yn (read))) + (cond ((eq? yn 'yes) #t) + ((eq? yn 'no) #f) + (else (display "Please type YES or NO") + (yorn))))) + + (display (question node)) + (display " ") + (let ((yn (yorn)) (correct #f) (newquest #f)) + (let ((next (if yn (yespart node) (nopart node)))) + (cond ((branch? next) (animal next)) + (else (display "Is it a ") + (display (answer next)) + (display "? ") + (cond ((yorn) "I win!") + (else (newline) + (display "I give up, what is it? ") + (set! correct (read)) + (newline) + (display "Please tell me a question whose answer ") + (display "is YES for a ") + (display correct) + (newline) + (display "and NO for a ") + (display (answer next)) + (display ".") + (newline) + (display "Enclose the question in quotation marks.") + (newline) + (set! newquest (read)) + (if yn + (set-yes! node (make-branch newquest + (make-leaf correct) + next)) + (set-no! node (make-branch newquest + (make-leaf correct) + next))) + "Thanks. Now I know better."))))))) + +(define (make-branch q y n) + (list 'branch q y n)) + +(define (make-leaf a) + (list 'leaf a)) + +(define animal-list + (make-branch "Does it have wings?" + (make-leaf 'parrot) + (make-leaf 'rabbit))) + + +(define (animal-game) (animal animal-list)) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/apl-meta.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/apl-meta.scm new file mode 100644 index 0000000..8147708 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/apl-meta.scm @@ -0,0 +1,260 @@ +;;; apl-meta.scm APL version of metacircular evaluator. + +;;; SETTING UP THE ENVIRONMENT + +;;; APL primitives aren't part of the environment. They are not subject +;;; to redefinition, for example. They are kept in a separate list. So +;;; the initial environment is empty. But define! only works if there is +;;; a non-empty environment, so we fake something. + +(define the-global-environment '()) + +;;; INITIALIZATION AND DRIVER LOOP + +;;; The following code initializes the machine and starts the APL +;;; system. You should not call it very often, because it will clobber +;;; the global environment, and you will lose any definitions you have +;;; accumulated. + +(define (initialize-apl) + (set! the-global-environment + (extend-environment '(no-name) '(no-value) '())) + (set! apl-operators + (list (make-scalar-op '+ (lambda (x) x) +) + (make-scalar-op '- - -) + (make-scalar-op '* + (lambda (x) (cond ((< x 0) -1) ((= x 0) 0) (else 1))) + *) + (make-scalar-op '% / /) + (make-scalar-op 'bar abs rem) + (make-scalar-op '= error (apl-pred2 =)) + (make-scalar-op '< error (apl-pred2 <)) + (make-scalar-op '> error (apl-pred2 >)) + (make-op '/ error compress) + (make-op 'iota iota error) + (make-op 'rho shape reshape) + (make-op 'comma ravel cat) + (make-op 'gets error 'set!))) + (apl-loop)) + +;;; APPLYING PRIMITIVE PROCEDURES + +;;; The mechanism for applying primitive procedures is somewhat +;;; different from the one given in the course notes. We can recognize +;;; primitive procedures (which are all inherited from Scheme) by asking +;;; Scheme if the object we have is a Scheme procedure. + +(define (primitive-procedure? p) + (applicable? p)) + +;;; To apply a primitive procedure, we ask the underlying Scheme system +;;; to perform the application. (Of course, an implementation on a +;;; low-level machine would perform the application in some other way.) + +(define (apply-primitive-procedure p args) + (apply p args)) + + +;;; Now for the code from the book!!! + + +;;; Section 4.1.1 + +(define (mini-eval exp env) + (cond ((self-evaluating? exp) exp) + ((variable? exp) (lookup-variable-value exp env)) + ((assignment? exp) (eval-assignment exp env)) + ((application? exp) + (mini-apply (mini-eval (operator exp) env) + (list-of-values (operands exp) env) + env)) + (else (error "Unknown expression type -- MINI-EVAL" exp)))) + +(define (mini-apply procedure arguments env) + (cond ((primitive-procedure? procedure) + (apply-primitive-procedure procedure arguments)) + ((compound-procedure? procedure) + (eval-sequence (procedure-body procedure) + (extend-environment + (parameters procedure) + arguments + env))) + (else + (error "Unknown procedure type -- MINI-APPLY" procedure)))) + +(define (list-of-values exps env) + (cond ((no-operands? exps) '()) + (else (cons (mini-eval (first-operand exps) env) + (list-of-values (rest-operands exps) + env))))) + +(define (eval-sequence exps env) + (cond ((last-exp? exps) (mini-eval (first-exp exps) env)) + (else (mini-eval (first-exp exps) env) + (eval-sequence (rest-exps exps) env)))) + +(define (eval-assignment exp env) + (let ((new-value (mini-eval (assignment-value exp) env))) + (set-variable-value! (assignment-variable exp) + new-value + env) + new-value)) + +;;; Section 4.1.2 -- Representing expressions + +;;; numbers + +(define (self-evaluating? exp) (number? exp)) + +;;; variables + +(define (variable? exp) (symbol? exp)) + +;;; assignment + +(define (assignment? exp) + (if (not (pair? exp)) + #f + (eq? (car exp) 'set!))) + +(define (assignment-variable exp) (cadr exp)) + +(define (assignment-value exp) (caddr exp)) + +;;; sequences + +(define (last-exp? seq) (null? (cdr seq))) + +(define (first-exp seq) (car seq)) + +(define (rest-exps seq) (cdr seq)) + +;;; procedure applications + +(define (application? exp) + (if (not (pair? exp)) + #f + (procedure? (car exp)))) + +(define (procedure? exp) + (or (applicable? exp) (compound-procedure? exp))) + +(define (operator app) (car app)) + +(define (operands app) (cdr app)) + +(define (no-operands? args) (null? args)) + +(define (first-operand args) (car args)) + +(define (rest-operands args) (cdr args)) + +;;; Representation of procedure objects + +(define (make-procedure lambda-exp env) + (list 'procedure lambda-exp env)) + +(define (compound-procedure? proc) + (if (not (pair? proc)) + #f + (eq? (car proc) 'procedure))) + +(define (parameters proc) (cadr (cadr proc))) + +(define (procedure-body proc) (cddr (cadr proc))) + +(define (procedure-environment proc) (caddr proc)) + +;;; Section 4.1.3 + +;;; Operations on environments + +(define (lookup-variable-value var env) + (if (assq var apl-operators) + var + (let ((b (binding-in-env var env))) + (if (found-binding? b) + (binding-value b) + (error "Unbound variable" var))))) + +(define (binding-in-env var env) + (if (no-more-frames? env) + no-binding + (let ((b (binding-in-frame var (first-frame env)))) + (if (found-binding? b) + b + (binding-in-env var (rest-frames env)))))) + +(define (extend-environment variables values base-env) + (adjoin-frame (make-frame variables values) base-env)) + +(define (set-variable-value! var val env) + (let ((b (binding-in-env var env))) + (if (found-binding? b) + (set-binding-value! b val) + (error "Unbound variable" var)))) + +(define (define-variable! var val env) + (let ((b (binding-in-frame var (first-frame env)))) + (if (found-binding? b) + (set-binding-value! b val) + (set-first-frame! + env + (adjoin-binding (make-binding var val) + (first-frame env)))))) + +;;; Representing environments + +(define (first-frame env) (car env)) + +(define (rest-frames env) (cdr env)) + +(define (no-more-frames? env) (null? env)) + +(define (adjoin-frame frame env) (cons frame env)) + +(define (set-first-frame! env new-frame) + (set-car! env new-frame)) + +;;; Representing frames + +(define (make-frame variables values) + (cond ((and (null? variables) (null? values)) '()) + ((null? variables) + (error "Too many values supplied" values)) + ((null? values) + (error "Too few values supplied" variables)) + (else + (cons (make-binding (car variables) (car values)) + (make-frame (cdr variables) (cdr values)))))) + +(define (adjoin-binding binding frame) + (cons binding frame)) + +(define (assq key bindings) + (cond ((null? bindings) no-binding) + ((eq? key (binding-variable (car bindings))) + (car bindings)) + (else (assq key (cdr bindings))))) + +(define (binding-in-frame var frame) + (assq var frame)) + +(define (found-binding? b) + (not (eq? b no-binding))) + +(define no-binding '()) + +;;; Representing bindings + +(define (make-binding variable value) + (cons variable value)) + +(define (binding-variable binding) + (car binding)) + +(define (binding-value binding) + (cdr binding)) + +(define (set-binding-value! binding value) + (set-cdr! binding value)) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/apl.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/apl.scm new file mode 100644 index 0000000..3af4acf --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/apl.scm @@ -0,0 +1,254 @@ +;;; APL interpreter project apl.scm + +(define (----YOU-FILL-THIS-IN----) '()) ; just so file will load + +;;; Step 1: convert scalar procedures to array procedures + +(define (single x) ; reduce order of single-element list + (cond ((not (pair? x)) x) + ((null? (cdr x)) (single (car x))) + (else x))) + +(define (apl-dyadic op) ; turn dyadic scalar function into APL array fn + (define (newop x y) + (let ((xx (single x)) + (yy (single y))) + (cond ((and (number? xx) (number? yy)) + (----YOU-FILL-THIS-IN----)) + ((number? xx) (map (----YOU-FILL-THIS-IN----) yy)) + ((number? yy) (map (----YOU-FILL-THIS-IN----) xx)) + (else (map newop xx yy))))) + newop) + +;;; Step 2: APL primitive operations + +(define (iota n) ; monadic iota + (define (iter x n) + (if (> x n) + '() + (cons x (iter (1+ x) n)))) + (iter 1 (single n))) + +(define (reshape shape l) ; dyadic rho + (define (circular l) + (define (c1 pair) + (if (null? (cdr pair)) + (set-cdr! pair l) + (c1 (cdr pair)))) + (c1 l) + l) + (define token + (let ((source (circular (ravel l)))) + (lambda () + (let ((out (car source))) + (set! source (cdr source)) + out)))) + (define (string n shape) + (if (= n 0) + '() + (let ((top (re1 shape))) + (cons top (string (-1+ n) shape))))) + (define (re1 shape) + (if (null? shape) + (token) + (string (car shape) (cdr shape)))) + (re1 shape)) + +(define (cat a b) ; dyadic comma + (define (depth l) + (if (not (pair? l)) + 0 + (1+ (depth (car l))))) + (define (max x y) + (if (> x y) x y)) + (define (shapeup l dims) + (if (= dims (depth l)) + l + (shapeup (cons l '()) dims))) + (let ((dim (max (depth a) (depth b)))) + (append (shapeup a dim) (shapeup b dim)))) + +(define (ravel l) ; monadic comma + (define (r1 this rest) + (cond ((null? this) (ravel rest)) + ((not (pair? this)) (cons this (ravel rest))) + (else (r1 (car this) (cons (cdr this) rest))))) + (cond ((null? l) '()) + ((not (pair? l)) (cons l '())) + (else (r1 (car l) (cdr l))))) + +(define (abs x) ; monadic bar + (if (< x 0) (- x) x)) + +(define (rem x y) ; dyadic bar + (remainder y x)) + +;;; data abstraction for APL operators + +(define (tri-op name nil mon dy) + (list name nil mon dy)) + +(define (apl-operator? op) + (assq op apl-operators)) + +(define (niladic op) + (cadr (assq op apl-operators))) + +(define (monadic op) + (caddr (assq op apl-operators))) + +(define (dyadic op) + (cadddr (assq op apl-operators))) + +(define (make-niladic name body) + (set! apl-operators (cons (tri-op name body error error) apl-operators))) + +(define (make-monadic name body) + (set! apl-operators (cons (tri-op name error body error) apl-operators))) + +(define (make-dyadic name body) + (set! apl-operators (cons (tri-op name error error body) apl-operators))) + +(define (make-op op mon dy) ; abbreviation for primitives, never niladic + (tri-op op error mon dy)) + +(define (make-scalar-op op mon dy) + (make-op op (apl-monadic mon) (apl-dyadic dy))) + +;;; Table of operations + +(define (apl-pred2 op) ; turn Lisp predicate (t/f) into APL (0/1) + (lambda (x y) + (if (op x y) 1 0))) + +(define apl-operators + (list (make-scalar-op '+ (lambda (x) x) +) + (make-scalar-op '- - -) + (make-scalar-op '* + (lambda (x) (cond ((< x 0) -1) + ((= x 0) 0) + (else 1))) + *) + (make-scalar-op '% / /) + (make-scalar-op 'bar abs rem) + (make-scalar-op '= error (apl-pred2 =)) + (make-scalar-op '< error (apl-pred2 <)) + (make-scalar-op '> error (apl-pred2 >)) + (make-op '/ error compress) + (make-op 'iota iota error) + (make-op 'rho shape reshape) + (make-op 'comma ravel cat))) + +;;; APL higher-order operations + +(define (reduce op l) ; higher-order / + (if (null? (cdr l)) + (car l) + ((dyadic op) (car l) (reduce op (cdr l))))) + +(define (apl-hof? op) + (assq op apl-hofs)) + +(define (hof op) + (cdr (assq op apl-hofs))) + +(define apl-hofs (list (cons '/ reduce))) + +;;; Step 3: Syntax conversion, infix to prefix + +(define (get-operand l) + (cond ((null? l) '()) + ((list? (car l)) + (get-dyad (get-operand (car l)) (cdr l))) + ((number? (car l)) (get-vector l)) + ((apl-operator? (car l)) + (cond ((null? (cdr l)) (error "dangling operator" (car l))) + ((apl-hof? (cadr l)) + (list (hof (cadr l)) (car l) (get-operand (cddr l)))) + (else + (list (monadic (car l)) (get-operand (cdr l)))))) + (else (get-dyad (car l) (cdr l))))) + +(define (get-dyad left l) + (cond ((null? l) left) + ((apl-operator? (car l)) + (----YOU PUT SOMETHING HERE----)) + (else + (error "operand where operator expected" (car l))))) + +(define (get-vector l) + (define (gv vect l) + (cond ((null? l) vect) + ((number? (car l)) + (----YOU PUT SOMETHING HERE----)) + (else (get-dyad vect l)))) + (gv '() l)) + +;;; mini-evaluator + +(define (apl-loop) + (define (maybe-display val) + (if (eq? val 'no-value) '() (display val))) + (newline) + (display "APL> ") + (maybe-display (apl-eval (get-operand (read)))) + (apl-loop)) + +(define (apl-eval l) + (cond ((not (pair? l)) l) + ((procedure? (car l)) (apply (car l) (map apl-eval (cdr l)))) + (else l))) + +(define *keyboard-interrupt-handler* reset) + +;;; Step 11: Procedure definition + +(define (convert-syntax l) + (cond ((not (pair? l)) l) + ((eq? (car l) 'del) (proc-definition (cdr l)) 'no-name) + (else (get-operand l)))) + +(define (proc-definition l) + (cond ((null? (cdr l)) + (make-niladic (car l) (make-procedure '() '() '() (proc-body)))) + ((eq? (cadr l) 'gets) + (proc-result (car l) (cddr l))) + (else (----YOU-FILL-THIS-IN----)))) + +(define (proc-result outvar l) + (define (count-to-locals l) + (cond ((null? l) 0) + ((eq? (car l) ':) 0) + (else (1+ (count-to-locals (cdr l)))))) + (define (locals l) + (cond ((null? l) '()) + ((eq? (car l) ':) + (cons (cadr l) (locals (cddr l)))) + (else (error "bad format in locals" l)))) + (let ((adic (count-to-locals l))) + (cond ((= adic 1) + (make-niladic (car l) + (make-procedure outvar + '() + (locals (cdr l)) + (proc-body)))) + ((= adic 2) + (make-monadic (car l) + (make-procedure outvar + (list (cadr l)) + (locals (cddr l)) + (proc-body)))) + ((= adic 3) + (----YOU-FILL-THIS-IN----)) + (else (error "too many args in function definition" l))))) + +(define (proc-body) + (define (proc-body-loop lineno) + (display "[") + (display lineno) + (display "] ") + (let ((next (read))) + (if (eq? next 'del) + '() + (cons next (proc-body-loop (1+ lineno)))))) + (proc-body-loop 1)) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/berkeley.scmm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/berkeley.scmm new file mode 100644 index 0000000..9dec781 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/berkeley.scmm @@ -0,0 +1,1918 @@ +;;; berkeley.scm 3.14 9/23/98 +;;; This version purports to work on Unix SCM, PC SCM, and Mac Gambit +;;; all without the least little version skew! + +;; 3.1 add uniform graphics interface +;; 3.2 fix scm untrace messing up butlast +;; 3.3 fix setheading in scm +;; 3.4 number->string doesn't blow up if given string (for trace) +;; 3.5 fix (/) without breaking (/ 3) +;; 3.6 fix number->string in MIT Scheme (don't set! it) +;; 3.7 big rewrite for SICP second edition changes +;; 3.8 SICP concurrency features added +;; 3.9 define-handler hacked for obscure scm bug +;; 3.10 fix scm parallel-execute to start the timer! +;; 3.11 not enough primitives protected in redefinition of define in scm +;; 3.12 stream-map with multiple streams +;; 3.13 number->string extra args +;; 3.14 protect define against redefining map + +;;; This file makes SCM 4e1 and Gambit 2.2 compatible with both +;;; Structure and Interpretation of Computer Programs (Abelson, +;;; Sussman, and Sussman) and Simply Scheme (Harvey and Wright). +;;; This should be sufficient to make Scheme fully compatible with +;;; Harvey and Wright, and compatible with SICP with the exception of +;;; first-class environments and pre-R4RS stuff like false empty lists. +;;; (It should be fully compatible with SICP second edition.) + +(if (equal? 'foo (symbol->string 'foo)) + (error "Berkeley.scm already loaded!!") + #f) + +(define scm? (not (exact? (/ 1 3)))) +;;; Notice that *after* loading this file, (/ 1 3) is never exact, +;;; so we have to check first thing. Naked Gambit has exact rationals, +;;; but naked SCM doesn't. + +;;; Let's not have any random messages please. +(if scm? + (begin + (eval '(define *dev-null* (make-soft-port (vector (lambda (x) #f) + (lambda (x) #f) + #f #f #f) + OPEN_WRITE))) + (set-current-error-port *dev-null*))) + +(define nil '()) +(define true #t) +(define false #f) + +(if scm? + (eval '(define (runtime) + (/ (get-internal-run-time) internal-time-units-per-second)))) + +;; crude timing program (time (foo..)) +(if scm? + (eval '(define time (procedure->macro + (lambda(x env) + `(let*((start (runtime)) + (result ,(cadr x)) + (end (- (runtime) start))) + (write end)(display " seconds")(newline) + result))))) + (eval '(define-macro (time . args) `(let*((start (runtime)) + (result ,(car args)) + (end (- (runtime) start))) + (write end) + (display " seconds") + (newline) + result)))) + +;; Originally from Jolly Chen. Modified by Justin Gibbs. + +(if (and scm? (eq? (software-type) 'unix)) + (begin + (eval '(define (expand-name st) + ;; given a string like "~cs60a/lib" expand it to + ;; "home/po/k/classes../cs60a/lib" + (let ((file (tmpnam)) + (res '())) + (system (string-append "/bin/csh -cf \"glob " st " > " file "\"")) + ;; Why read-line won't work here, I don't know + (let ((port (open-io-file file))) + (set! res (read-string port)) + (system (string-append "/bin/rm " file)) + (close-io-port port) + res)))) + ;;; Load. + ;;; Original Code from default SCM Init.scm. Modified by Justin Gibbs. + ;;; This load is identical to the load in Init.scm save that we use + ;;; csh to glob our file names for us. This leaves open the option + ;;; of using wild cards and "~" in the argument to load. Load does + ;;; not understand multifile arguments -- a feature that would be nice + ;;; to add later, since we already can glob on '*'s and '?'s. + (eval '(define (load file) + ;;; Only change is the addition of the following line. + (define filesuf (expand-name file)) + (define cep (current-error-port)) + (set! file filesuf) + (cond ((> (verbose) 1) + (display ";loading " cep) (write file cep) (newline cep))) + (force-output cep) + (or (try-load file) + ;;HERE is where the suffix gets specified + (begin (set! filesuf (string-append file (scheme-file-suffix))) + (try-load filesuf)) + (and (procedure? could-not-open) (could-not-open) #f) + (error "LOAD couldn't find file " file)) + (errno 0) + (cond ((> (verbose) 1) + (display ";done loading " cep) + (write filesuf cep) + (newline cep) + (force-output cep))))))) + + +;;; SICP stuff: + +(define (print x) + (display x) + (newline)) + +;; Define tagged data ADT: + +(define (attach-tag type-tag contents) + (cons type-tag contents)) + +(define (type-tag datum) + (if (pair? datum) + (car datum) + (error "Bad tagged datum -- TYPE-TAG" datum))) + +(define (contents datum) + (if (pair? datum) + (cdr datum) + (error "Bad tagged datum -- CONTENTS" datum))) + +;;For Section 3.1.2 -- written as suggested in footnote, +;; though the values of a, b, m may not be very "appropriately chosen" +(define (rand-update x) + (let ((a 27) (b 26) (m 127)) + (modulo (+ (* a x) b) m))) + +;;For Section 3.3.4, used by and-gate +;;Note: logical-and should test for valid signals, as logical-not does +(define (logical-and x y) + (if (and (= x 1) (= y 1)) + 1 + 0)) + +;; concurrency stuff + +(if scm? + (eval '(define test-and-set! + (let ((arb (make-arbiter 'scratchnsniff))) + (lambda (cell) + (if (try-arbiter arb) + (begin (process:schedule!) + (test-and-set! cell)) + (let ((result (car cell))) + (set-car! cell #t) + (release-arbiter arb) + result)))))) + (eval '(define test-and-set! + (let ((sem (make-semaphore))) + (lambda (cell) + (semaphore-wait sem) + (let ((result (car cell))) + (set-car! cell #t) + (semaphore-signal sem) + result)))))) + +(if scm? (eval '(require 'process))) + +(if scm? + (eval '(define (parallel-execute . thunks) + (for-each (lambda (thunk) (add-process! (lambda (foo) (thunk)))) + thunks) + (alarm-interrupt) + (process:schedule!))) + (eval '(define (parallel-execute . thunks) + (for-each (lambda (thunk) (future (thunk))) thunks)))) + +(if scm? + (eval '(define (stop) (alarm 0) (set! process:queue (make-queue))))) + +;;For Section 3.5.2, to check power series (exercises 3.59-3.62) +;;Evaluate and accumulate n terms of the series s at the given x +;;Uses horner-eval from ex 2.34 +(define (eval-power-series s x n) + (horner-eval x (first-n-of-series s n))) +(define (first-n-of-series s n) + (if (= n 0) + '() + (cons (stream-car s) (first-n-of-series (stream-cdr s) (- n 1))))) + +;; Streams: + +;; Reimplement delay so that promises are procedures + +(define (memo-proc proc) + (let ((already-run? #f) (result #f)) + (lambda () + (if (not already-run?) + (begin (set! result (proc)) + (set! already-run? #t) + result) + result)))) + +(if scm? + (eval '(define delay (procedure->macro + (lambda (x env) + `(memo-proc (lambda () ,(cadr x))))))) + (eval '(define-macro (delay . args) + `(memo-proc (lambda () ,(car args)))))) + +(define (force delayed-object) + (delayed-object)) + +(if scm? + (eval '(define cons-stream + (procedure->macro + (lambda(x env)`(cons ,(cadr x) (delay ,(caddr x))))))) + (eval '(define-macro (cons-stream . args) + `(cons ,(car args) (delay ,(cadr args)))))) + +(define (stream-car stream) (car stream)) + +(define (stream-cdr st) + (force (cdr st))) + +(define the-empty-stream '()) + +(define (stream-null? stream) (eq? stream the-empty-stream)) + +(define (stream? obj) + (or (stream-null? obj) + (and (pair? obj) (procedure? (cdr obj))))) + +(define (stream-accumulate combiner initial-value stream) + (if (stream-null? stream) + initial-value + (combiner (stream-car stream) + (stream-accumulate combiner + initial-value + (stream-cdr stream))))) + +(define (accumulate-delayed combiner initial-value stream) + (if (stream-null? stream) + initial-value + (combiner (stream-car stream) + (delay + (accumulate-delayed combiner + initial-value + (stream-cdr stream)))))) + +(define (interleave s1 s2) + (if (stream-null? s1) + s2 + (cons-stream (stream-car s1) + (interleave s2 (stream-cdr s1))))) + +(define (interleave-delayed s1 delayed-s2) + (if (stream-null? s1) + (force delayed-s2) + (cons-stream (stream-car s1) + (interleave-delayed (force delayed-s2) + (delay (stream-cdr s1)))))) + +(define (stream-flatten stream) + (accumulate-delayed interleave-delayed + the-empty-stream + stream)) + +(define (stream-ref s n) + (if (= n 0) + (stream-car s) + (stream-ref (stream-cdr s) (- n 1)))) + +(define (stream-map proc . s) + (if (stream-null? (car s)) + the-empty-stream + (cons-stream (apply proc (map stream-car s)) + (apply stream-map proc (map stream-cdr s))))) + +(define (stream-for-each proc s) + (if (stream-null? s) + 'done + (begin + (proc (stream-car s)) + (stream-for-each proc (stream-cdr s))))) + +(define (display-stream s) + (stream-for-each + (lambda (element) (newline) (display element)) + s)) + +(define (stream-flatmap f s) + (stream-flatten (stream-map f s))) + +(define (stream-append s1 s2) + (if (stream-null? s1) + s2 + (cons-stream (stream-car s1) + (stream-append (stream-cdr s1) s2)))) + +(define (list->stream l) + (if (null? l) + the-empty-stream + (cons-stream (car l) (list->stream (cdr l))) )) + +(define (make-stream . elements) + (list->stream elements)) + +(define (enumerate-interval low high) + (if (> low high) + '() + (cons low (enumerate-interval (+ low 1) high)))) + +(define (flatmap proc seq) + (accumulate append '() (map proc seq))) + +(define (stream-enumerate-interval low high) + (if (> low high) + the-empty-stream + (cons-stream low (stream-enumerate-interval (+ low 1) high)))) + +(define range stream-enumerate-interval) + +(define (stream-filter pred stream) + (cond ((stream-null? stream) the-empty-stream) + ((pred (stream-car stream)) + (cons-stream (stream-car stream) + (stream-filter pred (stream-cdr stream)))) + (else (stream-filter pred (stream-cdr stream))))) + +(define (show-stream strm . args) + (if (null? args) + (ss1 strm 10 10) + (ss1 strm (car args) (car args)))) + +(define ss show-stream) + +(define (ss1 strm this all) + (cond ((null? strm) '()) + ((= this 0) '(...)) + ((and (pair? strm) (procedure? (cdr strm))) + (cons (ss1 (stream-car strm) all all) + (ss1 (stream-cdr strm) (- this 1) all))) + (else strm))) + +(define div quotient) + +(define / + (let ((old/ /)) + (lambda args + (let ((quo (apply old/ args))) + (if (integer? quo) + quo + (exact->inexact quo)))))) + +(define 1+ + (let ((+ +)) + (lambda (x) (+ x 1)))) + +(define (-1+ x) (- x 1)) + +(define (nth n l) (list-ref l n)) + +(define (load-noisily file-name) + (define (iter port) + (let ((the-expression (read port))) + (cond ((eof-object? the-expression) #t) + (else + (display (eval the-expression)) + (newline) + (iter port))))) + (let ((port (open-input-file file-name))) + (iter port) + (close-input-port port) + 'ok)) + + +;;; Get and put for section 2.3 + +(define (get key1 key2) + (let ((subtable (assoc key1 (cdr the-get/put-table)))) + (if (not subtable) + #f + (let ((record (assoc key2 (cdr subtable)))) + (if (not record) + #f + (cdr record)))))) + +(define (put key1 key2 value) + (let ((subtable (assoc key1 (cdr the-get/put-table)))) + (if (not subtable) + (set-cdr! the-get/put-table + (cons (list key1 + (cons key2 value)) + (cdr the-get/put-table))) + (let ((record (assoc key2 (cdr subtable)))) + (if (not record) + (set-cdr! subtable + (cons (cons key2 value) + (cdr subtable))) + (set-cdr! record value))))) + 'ok) + +(define the-get/put-table (list '*table*)) + + +;;; simply.scm version 3.6 (4/13/94) + +;;; This file uses Scheme features we don't talk about in _Simply_Scheme_. +;;; Read at your own risk. + +;; Make number->string remove leading "+" if necessary + +(if (char=? #\+ (string-ref (number->string 1.0) 0)) + (let ((old-ns number->string) + (char=? char=?) + (string-ref string-ref) + (substring substring) + (string-length string-length)) + (set! number->string + (lambda args + (let ((result (apply old-ns args))) + (if (char=? #\+ (string-ref result 0)) + (substring result 1 (string-length result)) + result))))) + 'no-problem) + +(define number->string + (let ((old-ns number->string) + (string? string?)) + (lambda args + (if (string? (car args)) + (car args) + (apply old-ns args))))) + +;; Get strings in error messages to print nicely (especially "") + +(define whoops + (let ((string? string?) + (string-append string-append) + (error error) + (cons cons) + (map map) + (apply apply)) + (define (error-printform x) + (if (string? x) + (string-append "\"" x "\"") + x)) + (lambda (string . args) + (apply error (cons string (map error-printform args)))))) + + +;; ROUND returns an inexact integer if its argument is inexact, +;; but we think it should always return an exact integer. +;; (It matters because some Schemes print inexact integers as "+1.0".) +;; The (exact 1) test is for PC Scheme, in which nothing is exact. +(if (and (inexact? (round (sqrt 2))) (exact? 1)) + (let ((old-round round) + (inexact->exact inexact->exact)) + (set! round + (lambda (number) + (inexact->exact (old-round number))))) + 'no-problem) + +;; Remainder and quotient blow up if their argument isn't an integer. +;; Unfortunately, in SCM, (* 365.25 24 60 60) *isn't* an integer. + +(if (inexact? (* .25 4)) + (let ((rem remainder) + (quo quotient) + (inexact->exact inexact->exact) + (integer? integer?)) + (set! remainder + (lambda (x y) + (rem (if (integer? x) (inexact->exact x) x) + (if (integer? y) (inexact->exact y) y)))) + (set! quotient + (lambda (x y) + (quo (if (integer? x) (inexact->exact x) x) + (if (integer? y) (inexact->exact y) y))))) + 'done) + + +;; Random +;; If your version of Scheme has RANDOM, you should take this out. +;; (It gives the same sequence of random numbers every time.) + +(define random + (let ((*seed* 1) (quotient quotient) (modulo modulo) (+ +) (- -) (* *) (> >)) + (lambda (x) + (let* ((hi (quotient *seed* 127773)) + (low (modulo *seed* 127773)) + (test (- (* 16807 low) (* 2836 hi)))) + (if (> test 0) + (set! *seed* test) + (set! *seed* (+ test 2147483647)))) + (modulo *seed* x)))) + + +;;; Logo-style word/sentence implementation + +(define word? + (let ((number? number?) + (symbol? symbol?) + (string? string?)) + (lambda (x) + (or (symbol? x) (number? x) (string? x))))) + +(define sentence? + (let ((null? null?) + (pair? pair?) + (word? word?) + (car car) + (cdr cdr)) + (define (list-of-words? l) + (cond ((null? l) #t) + ((pair? l) + (and (word? (car l)) (list-of-words? (cdr l)))) + (else #f))) + list-of-words?)) + +(define empty? + (let ((null? null?) + (string? string?) + (string=? string=?)) + (lambda (x) + (or (null? x) + (and (string? x) (string=? x "")))))) + + +(define char-rank + ;; 0 Letter in good case or special initial + ;; 1 ., + or - + ;; 2 Digit + ;; 3 Letter in bad case or weird character + (let ((*the-char-ranks* (make-vector 256 3)) + (= =) + (+ +) + (string-ref string-ref) + (string-length string-length) + (vector-set! vector-set!) + (char->integer char->integer) + (symbol->string symbol->string) + (vector-ref vector-ref)) + (define (rank-string str rank) + (define (helper i len) + (if (= i len) + 'done + (begin (vector-set! *the-char-ranks* + (char->integer (string-ref str i)) + rank) + (helper (+ i 1) len)))) + (helper 0 (string-length str))) + (rank-string (symbol->string 'abcdefghijklmnopqrstuvwxyz) 0) + (rank-string "!$%&*/:<=>?~_^" 0) + (rank-string "+-." 1) + (rank-string "0123456789" 2) + (lambda (char) ;; value of char-rank + (vector-ref *the-char-ranks* (char->integer char))))) + +(define string->word + (let ((= =) (<= <=) (+ +) (- -) + (char-rank char-rank) + (string-ref string-ref) + (string-length string-length) + (string=? string=?) + (not not) + (char=? char=?) + (string->number string->number) + (string->symbol string->symbol)) + (lambda (string) + (define (subsequents? string i length) + (cond ((= i length) #t) + ((<= (char-rank (string-ref string i)) 2) + (subsequents? string (+ i 1) length)) + (else #f))) + (define (special-id? string) + (or (string=? string "+") + (string=? string "-") + (string=? string "..."))) + (define (ok-symbol? string) + (if (string=? string "") + #f + (let ((rank1 (char-rank (string-ref string 0)))) + (cond ((= rank1 0) (subsequents? string 1 (string-length string))) + ((= rank1 1) (special-id? string)) + (else #f))))) + (define (nn-helper string i len seen-point?) + (cond ((= i len) + (if seen-point? + (not (char=? (string-ref string (- len 1)) #\0)) + #t)) + ((char=? #\. (string-ref string i)) + (cond (seen-point? #f) + ((= (+ i 2) len) #t) ; Accepts "23.0" + (else (nn-helper string (+ i 1) len #t)))) + ((= 2 (char-rank (string-ref string i))) + (nn-helper string (+ i 1) len seen-point?)) + (else #f))) + (define (narrow-number? string) + (if (string=? string "") + #f + (let* ((c0 (string-ref string 0)) + (start 0) + (len (string-length string)) + (cn (string-ref string (- len 1)))) + (if (and (char=? c0 #\-) (not (= len 1))) + (begin + (set! start 1) + (set! c0 (string-ref string 1))) + #f) + (cond ((not (= (char-rank cn) 2)) #f) ; Rejects "-" among others + ((char=? c0 #\.) #f) + ((char=? c0 #\0) + (cond ((= len 1) #t) ; Accepts "0" but not "-0" + ((= len 2) #f) ; Rejects "-0" and "03" + ((char=? (string-ref string (+ start 1)) #\.) + (nn-helper string (+ start 2) len #t)) + (else #f))) + (else (nn-helper string start len #f)))))) + + ;; The body of string->word: + (cond ((narrow-number? string) (string->number string)) + ((ok-symbol? string) (string->symbol string)) + (else string))))) + +(define char->word + (let ((= =) + (char-rank char-rank) + (make-string make-string) + (string->symbol string->symbol) + (string->number string->number) + (char=? char=?)) + (lambda (char) + (let ((rank (char-rank char)) + (string (make-string 1 char))) + (cond ((= rank 0) (string->symbol string)) + ((= rank 2) (string->number string)) + ((char=? char #\+) '+) + ((char=? char #\-) '-) + (else string)))))) + +(define word->string + (let ((number? number?) + (string? string?) + (number->string number->string) + (symbol->string symbol->string)) + (lambda (wd) + (cond ((string? wd) wd) + ((number? wd) (number->string wd)) + (else (symbol->string wd)))))) + +(define count + (let ((word? word?) + (string-length string-length) + (word->string word->string) + (length length)) + (lambda (stuff) + (if (word? stuff) + (string-length (word->string stuff)) + (length stuff))))) + +(define word + (let ((string->word string->word) + (apply apply) + (string-append string-append) + (map map) + (word? word?) + (word->string word->string) + (whoops whoops)) + (lambda x + (string->word + (apply string-append + (map (lambda (arg) + (if (word? arg) + (word->string arg) + (whoops "Invalid argument to WORD: " arg))) + x)))))) + +(define se + (let ((pair? pair?) + (null? null?) + (word? word?) + (car car) + (cons cons) + (cdr cdr) + (whoops whoops)) + (define (paranoid-append a original-a b) + (cond ((null? a) b) + ((word? (car a)) + (cons (car a) (paranoid-append (cdr a) original-a b))) + (else (whoops "Argument to SENTENCE not a word or sentence" + original-a )))) + (define (combine-two a b) ;; Note: b is always a list + (cond ((pair? a) (paranoid-append a a b)) + ((null? a) b) + ((word? a) (cons a b)) + (else (whoops "Argument to SENTENCE not a word or sentence:" a)))) + ;; Helper function so recursive calls don't show up in TRACE + (define (real-se args) + (if (null? args) + '() + (combine-two (car args) (real-se (cdr args))))) + (lambda args + (real-se args)))) + +(define sentence se) + +(define first + (let ((pair? pair?) + (char->word char->word) + (string-ref string-ref) + (word->string word->string) + (car car) + (empty? empty?) + (whoops whoops) + (word? word?)) + (define (word-first wd) + (char->word (string-ref (word->string wd) 0))) + (lambda (x) + (cond ((pair? x) (car x)) + ((empty? x) (whoops "Invalid argument to FIRST: " x)) + ((word? x) (word-first x)) + (else (whoops "Invalid argument to FIRST: " x)))))) + +(define last + (let ((pair? pair?) + (- -) + (word->string word->string) + (char->word char->word) + (string-ref string-ref) + (string-length string-length) + (empty? empty?) + (cdr cdr) + (car car) + (whoops whoops) + (word? word?)) + (define (word-last wd) + (let ((s (word->string wd))) + (char->word (string-ref s (- (string-length s) 1))))) + (define (list-last lst) + (if (empty? (cdr lst)) + (car lst) + (list-last (cdr lst)))) + (lambda (x) + (cond ((pair? x) (list-last x)) + ((empty? x) (whoops "Invalid argument to LAST: " x)) + ((word? x) (word-last x)) + (else (whoops "Invalid argument to LAST: " x)))))) + +(define bf + (let ((pair? pair?) + (substring substring) + (string-length string-length) + (string->word string->word) + (word->string word->string) + (cdr cdr) + (empty? empty?) + (whoops whoops) + (word? word?)) + (define string-bf + (lambda (s) + (substring s 1 (string-length s)))) + (define (word-bf wd) + (string->word (string-bf (word->string wd)))) + (lambda (x) + (cond ((pair? x) (cdr x)) + ((empty? x) (whoops "Invalid argument to BUTFIRST: " x)) + ((word? x) (word-bf x)) + (else (whoops "Invalid argument to BUTFIRST: " x)))))) + +(define butfirst bf) + +(define bl + (let ((pair? pair?) (- -) + (cdr cdr) + (cons cons) + (car car) + (substring substring) + (string-length string-length) + (string->word string->word) + (word->string word->string) + (empty? empty?) + (whoops whoops) + (word? word?)) + (define (list-bl list) + (if (null? (cdr list)) + '() + (cons (car list) (list-bl (cdr list))))) + (define (string-bl s) + (substring s 0 (- (string-length s) 1))) + (define (word-bl wd) + (string->word (string-bl (word->string wd)))) + (lambda (x) + (cond ((pair? x) (list-bl x)) + ((empty? x) (whoops "Invalid argument to BUTLAST: " x)) + ((word? x) (word-bl x)) + (else (whoops "Invalid argument to BUTLAST: " x)))))) + +(define butlast bl) + +(define item + (let ((> >) (- -) (< <) (integer? integer?) (list-ref list-ref) + (char->word char->word) + (string-ref string-ref) + (word->string word->string) + (not not) + (whoops whoops) + (count count) + (word? word?) + (list? list?)) + (define (word-item n wd) + (char->word (string-ref (word->string wd) (- n 1)))) + (lambda (n stuff) + (cond ((not (integer? n)) + (whoops "Invalid first argument to ITEM (must be an integer): " + n)) + ((< n 1) + (whoops "Invalid first argument to ITEM (must be positive): " + n)) + ((> n (count stuff)) + (whoops "No such item: " n stuff)) + ((word? stuff) (word-item n stuff)) + ((list? stuff) (list-ref stuff (- n 1))) + (else (whoops "Invalid second argument to ITEM: " stuff)))))) + +(define equal? + ;; Note that EQUAL? assumes strings are numbers. + ;; (strings-are-numbers #f) doesn't change this behavior. + (let ((vector-length vector-length) + (= =) + (vector-ref vector-ref) + (+ +) + (string? string?) + (symbol? symbol?) + (null? null?) + (pair? pair?) + (car car) + (cdr cdr) + (eq? eq?) + (string=? string=?) + (symbol->string symbol->string) + (number? number?) + (string->word string->word) + (vector? vector?) + (eqv? eqv?)) + (define (vector-equal? v1 v2) + (let ((len1 (vector-length v1)) + (len2 (vector-length v2))) + (define (helper i) + (if (= i len1) + #t + (and (equal? (vector-ref v1 i) (vector-ref v2 i)) + (helper (+ i 1))))) + (if (= len1 len2) + (helper 0) + #f))) + (lambda (x y) + (cond ((null? x) (null? y)) + ((null? y) #f) + ((pair? x) + (and (pair? y) + (equal? (car x) (car y)) + (equal? (cdr x) (cdr y)))) + ((pair? y) #f) + ((symbol? x) + (or (and (symbol? y) (eq? x y)) + (and (string? y) (string=? (symbol->string x) y)))) + ((symbol? y) + (and (string? x) (string=? x (symbol->string y)))) + ((number? x) + (or (and (number? y) (= x y)) + (and (string? y) + (let ((possible-num (string->word y))) + (and (number? possible-num) + (= x possible-num)))))) + ((number? y) + (and (string? x) + (let ((possible-num (string->word x))) + (and (number? possible-num) + (= possible-num y))))) + ((string? x) (and (string? y) (string=? x y))) + ((string? y) #f) + ((vector? x) (and (vector? y) (vector-equal? x y))) + ((vector? y) #f) + (else (eqv? x y)))))) + +(define member? + (let ((> >) (- -) (< <) + (null? null?) + (symbol? symbol?) + (eq? eq?) + (car car) + (not not) + (symbol->string symbol->string) + (string=? string=?) + (cdr cdr) + (equal? equal?) + (word->string word->string) + (string-length string-length) + (whoops whoops) + (string-ref string-ref) + (char=? char=?) + (list? list?) + (number? number?) + (empty? empty?) + (word? word?) + (string? string?)) + (define (symbol-in-list? symbol string lst) + (cond ((null? lst) #f) + ((and (symbol? (car lst)) + (eq? symbol (car lst)))) + ((string? (car lst)) + (cond ((not string) + (symbol-in-list? symbol (symbol->string symbol) lst)) + ((string=? string (car lst)) #t) + (else (symbol-in-list? symbol string (cdr lst))))) + (else (symbol-in-list? symbol string (cdr lst))))) + (define (word-in-list? wd lst) + (cond ((null? lst) #f) + ((equal? wd (car lst)) #t) + (else (word-in-list? wd (cdr lst))))) + (define (word-in-word? small big) + (let ((one-letter-str (word->string small))) + (if (> (string-length one-letter-str) 1) + (whoops "Invalid arguments to MEMBER?: " small big) + (let ((big-str (word->string big))) + (char-in-string? (string-ref one-letter-str 0) + big-str + (- (string-length big-str) 1)))))) + (define (char-in-string? char string i) + (cond ((< i 0) #f) + ((char=? char (string-ref string i)) #t) + (else (char-in-string? char string (- i 1))))) + (lambda (x stuff) + (cond ((empty? stuff) #f) + ((word? stuff) (word-in-word? x stuff)) + ((not (list? stuff)) + (whoops "Invalid second argument to MEMBER?: " stuff)) + ((symbol? x) (symbol-in-list? x #f stuff)) + ((or (number? x) (string? x)) + (word-in-list? x stuff)) + (else (whoops "Invalid first argument to MEMBER?: " x)))))) + +(define before? + (let ((not not) + (word? word?) + (whoops whoops) + (string<? string<?) + (word->string word->string)) + (lambda (wd1 wd2) + (cond ((not (word? wd1)) + (whoops "Invalid first argument to BEFORE? (not a word): " wd1)) + ((not (word? wd2)) + (whoops "Invalid second argument to BEFORE? (not a word): " wd2)) + (else (string<? (word->string wd1) (word->string wd2))))))) + + +;;; Higher Order Functions + +(define filter + (let ((null? null?) + (car car) + (cons cons) + (cdr cdr) + (not not) + (procedure? procedure?) + (whoops whoops) + (list? list?)) + (lambda (pred l) + ;; Helper function so recursive calls don't show up in TRACE + (define (real-filter l) + (cond ((null? l) '()) + ((pred (car l)) + (cons (car l) (real-filter (cdr l)))) + (else (real-filter (cdr l))))) + (cond ((not (procedure? pred)) + (whoops "Invalid first argument to FILTER (not a procedure): " + pred)) + ((not (list? l)) + (whoops "Invalid second argument to FILTER (not a list): " l)) + (else (real-filter l)))))) + +(define keep + (let ((+ +) (= =) (pair? pair?) + (substring substring) + (char->word char->word) + (string-ref string-ref) + (string-set! string-set!) + (word->string word->string) + (string-length string-length) + (string->word string->word) + (make-string make-string) + (procedure? procedure?) + (whoops whoops) + (word? word?) + (null? null?)) + (lambda (pred w-or-s) + (define (keep-string in i out out-len len) + (cond ((= i len) (substring out 0 out-len)) + ((pred (char->word (string-ref in i))) + (string-set! out out-len (string-ref in i)) + (keep-string in (+ i 1) out (+ out-len 1) len)) + (else (keep-string in (+ i 1) out out-len len)))) + (define (keep-word wd) + (let* ((string (word->string wd)) + (len (string-length string))) + (string->word + (keep-string string 0 (make-string len) 0 len)))) + (cond ((not (procedure? pred)) + (whoops "Invalid first argument to KEEP (not a procedure): " + pred)) + ((pair? w-or-s) (filter pred w-or-s)) + ((word? w-or-s) (keep-word w-or-s)) + ((null? w-or-s) '()) + (else + (whoops "Bad second argument to KEEP (not a word or sentence): " + w-or-s)))))) + +(define appearances + (let ((count count) + (keep keep) + (equal? equal?)) + (lambda (item aggregate) + (count (keep (lambda (element) (equal? item element)) aggregate))))) + +(define every + (let ((= =) (+ +) + (se se) + (char->word char->word) + (string-ref string-ref) + (empty? empty?) + (first first) + (bf bf) + (not not) + (procedure? procedure?) + (whoops whoops) + (word? word?) + (word->string word->string) + (string-length string-length)) + (lambda (fn stuff) + (define (string-every string i length) + (if (= i length) + '() + (se (fn (char->word (string-ref string i))) + (string-every string (+ i 1) length)))) + (define (sent-every sent) + ;; This proc. can't be optimized or else it will break the + ;; exercise where we ask them to reimplement sentences as + ;; vectors and then see if every still works. + (if (empty? sent) + sent ; Can't be '() or exercise breaks. + (se (fn (first sent)) + (sent-every (bf sent))))) + (cond ((not (procedure? fn)) + (whoops "Invalid first argument to EVERY (not a procedure):" + fn)) + ((word? stuff) + (let ((string (word->string stuff))) + (string-every string 0 (string-length string)))) + (else (sent-every stuff)))))) + +;; In _Simply Scheme_, accumulate works on words and sentences, and takes +;; two arguments. In SICP, accumulate works on lists, and takes three +;; arguments. This version does both. Sorry. + +(define accumulate + (let ((not not) + (empty? empty?) + (bf bf) + (first first) + (procedure? procedure?) + (whoops whoops) + (member member) + (list list)) + (lambda (combiner stuff . extra) + (define (real-accumulate stuff) + (if (empty? (bf stuff)) + (first stuff) + (combiner (first stuff) (real-accumulate (bf stuff))))) + (define (sicp-accumulate initial stuff) + (if (null? stuff) + initial + (combiner (car stuff) (sicp-accumulate initial (cdr stuff))))) + (cond ((not (procedure? combiner)) + (whoops "Invalid first argument to ACCUMULATE (not a procedure):" + combiner)) + ((null? extra) ; Simply Scheme version + (cond ((not (empty? stuff)) (real-accumulate stuff)) + ((member combiner (list + * word se)) (combiner)) + (else + (whoops "Can't accumulate empty input with that combiner")))) + ((not (null? (cdr extra))) + (whoops "Too many arguments to accumulate")) + (else (sicp-accumulate stuff (car extra))))))) + +(define reduce + (let ((null? null?) + (cdr cdr) + (car car) + (not not) + (procedure? procedure?) + (whoops whoops) + (member member) + (list list)) + (lambda (combiner stuff) + (define (real-reduce stuff) + (if (null? (cdr stuff)) + (car stuff) + (combiner (car stuff) (real-reduce (cdr stuff))))) + (cond ((not (procedure? combiner)) + (whoops "Invalid first argument to REDUCE (not a procedure):" + combiner)) + ((not (null? stuff)) (real-reduce stuff)) + ((member combiner (list + * word se append)) (combiner)) + (else (whoops "Can't reduce empty input with that combiner")))))) + +(define repeated + (let ((= =) (- -)) + (lambda (fn number) + (if (= number 0) + (lambda (x) x) + (lambda (x) + ((repeated fn (- number 1)) (fn x))))))) + + +;; Tree stuff +(define make-node cons) +(define datum car) +(define children cdr) + + +;; I/O + +(define show + (let ((= =) + (length length) + (display display) + (car car) + (newline newline) + (not not) + (output-port? output-port?) + (apply apply) + (whoops whoops)) + (lambda args + (cond + ((= (length args) 1) + (display (car args)) + (newline)) + ((= (length args) 2) + (if (not (output-port? (car (cdr args)))) + (whoops "Invalid second argument to SHOW (not an output port): " + (car (cdr args)))) + (apply display args) + (newline (car (cdr args)))) + (else (whoops "Incorrect number of arguments to procedure SHOW")))))) + +(define show-line + (let ((>= >=) + (length length) + (whoops whoops) + (null? null?) + (current-output-port current-output-port) + (car car) + (not not) + (list? list?) + (display display) + (for-each for-each) + (cdr cdr) + (newline newline)) + (lambda (line . args) + (if (>= (length args) 2) + (whoops "Too many arguments to show-line") + (let ((port (if (null? args) (current-output-port) (car args)))) + (cond ((not (list? line)) + (whoops "Invalid argument to SHOW-LINE (not a list):" line)) + ((null? line) #f) + (else + (display (car line) port) + (for-each (lambda (wd) (display " " port) (display wd port)) + (cdr line)))) + (newline port)))))) + +(define read-string + (let ((read-char read-char) + (eqv? eqv?) + (apply apply) + (string-append string-append) + (substring substring) + (reverse reverse) + (cons cons) + (>= >=) (+ +) + (string-set! string-set!) + (length length) + (whoops whoops) + (null? null?) + (current-input-port current-input-port) + (car car) + (cdr cdr) + (eof-object? eof-object?) + (list list) + (make-string make-string) + (peek-char peek-char)) + (define (read-string-helper chars all-length chunk-length port) + (let ((char (read-char port)) + (string (car chars))) + (cond ((or (eof-object? char) (eqv? char #\newline)) + (apply string-append + (reverse + (cons + (substring (car chars) 0 chunk-length) + (cdr chars))))) + ((>= chunk-length 80) + (let ((newstring (make-string 80))) + (string-set! newstring 0 char) + (read-string-helper (cons newstring chars) + (+ all-length 1) + 1 + port))) + (else + (string-set! string chunk-length char) + (read-string-helper chars + (+ all-length 1) + (+ chunk-length 1) + port))))) + (lambda args + (if (>= (length args) 2) + (whoops "Too many arguments to read-string") + (let ((port (if (null? args) (current-input-port) (car args)))) + (if (eof-object? (peek-char port)) + (read-char port) + (read-string-helper (list (make-string 80)) 0 0 port))))))) + +(define read-line + (let ((= =) + (list list) + (string->word string->word) + (substring substring) + (char-whitespace? char-whitespace?) + (string-ref string-ref) + (+ +) + (string-length string-length) + (apply apply) + (read-string read-string)) + (lambda args + (define (tokenize string) + (define (helper i start len) + (cond ((= i len) + (if (= i start) + '() + (list (string->word (substring string start i))))) + ((char-whitespace? (string-ref string i)) + (if (= i start) + (helper (+ i 1) (+ i 1) len) + (cons (string->word (substring string start i)) + (helper (+ i 1) (+ i 1) len)))) + (else (helper (+ i 1) start len)))) + (if (eof-object? string) + string + (helper 0 0 (string-length string)))) + (tokenize (apply read-string args))))) + +(define *the-open-inports* '()) +(define *the-open-outports* '()) + +(define align + (let ((< <) (abs abs) (* *) (expt expt) (>= >=) (- -) (+ +) (= =) + (null? null?) + (car car) + (round round) + (number->string number->string) + (string-length string-length) + (string-append string-append) + (make-string make-string) + (substring substring) + (string-set! string-set!) + (number? number?) + (word->string word->string)) + (lambda (obj width . rest) + (define (align-number obj width rest) + (let* ((sign (< obj 0)) + (num (abs obj)) + (prec (if (null? rest) 0 (car rest))) + (big (round (* num (expt 10 prec)))) + (cvt0 (number->string big)) + (cvt (if (< num 1) (string-append "0" cvt0) cvt0)) + (pos-str (if (>= (string-length cvt0) prec) + cvt + (string-append + (make-string (- prec (string-length cvt0)) #\0) + cvt))) + (string (if sign (string-append "-" pos-str) pos-str)) + (length (+ (string-length string) + (if (= prec 0) 0 1))) + (left (- length (+ 1 prec))) + (result (if (= prec 0) + string + (string-append + (substring string 0 left) + "." + (substring string left (- length 1)))))) + (cond ((= length width) result) + ((< length width) + (string-append (make-string (- width length) #\space) result)) + (else (let ((new (substring result 0 width))) + (string-set! new (- width 1) #\+) + new))))) + (define (align-word string) + (let ((length (string-length string))) + (cond ((= length width) string) + ((< length width) + (string-append string (make-string (- width length) #\space))) + (else (let ((new (substring string 0 width))) + (string-set! new (- width 1) #\+) + new))))) + (if (number? obj) + (align-number obj width rest) + (align-word (word->string obj)))))) + +(define open-output-file + (let ((oof open-output-file) + (cons cons)) + (lambda (filename) + (let ((port (oof filename))) + (set! *the-open-outports* (cons port *the-open-outports*)) + port)))) + +(define open-input-file + (let ((oif open-input-file) + (cons cons)) + (lambda (filename) + (let ((port (oif filename))) + (set! *the-open-inports* (cons port *the-open-inports*)) + port)))) + +(define remove! + (let ((null? null?) + (cdr cdr) + (eq? eq?) + (set-cdr! set-cdr!) + (car car)) + (lambda (thing lst) + (define (r! prev) + (cond ((null? (cdr prev)) lst) + ((eq? thing (car (cdr prev))) + (set-cdr! prev (cdr (cdr prev))) + lst) + (else (r! (cdr prev))))) + (cond ((null? lst) lst) + ((eq? thing (car lst)) (cdr lst)) + (else (r! lst)))))) + +(define close-input-port + (let ((cip close-input-port) + (remove! remove!)) + (lambda (port) + (set! *the-open-inports* (remove! port *the-open-inports*)) + (cip port)))) + +(define close-output-port + (let ((cop close-output-port) + (remove! remove!)) + (lambda (port) + (set! *the-open-outports* (remove! port *the-open-outports*)) + (cop port)))) + +(define close-all-ports + (let ((for-each for-each) + (close-input-port close-input-port) + (close-output-port close-output-port)) + (lambda () + (for-each close-input-port *the-open-inports*) + (for-each close-output-port *the-open-outports*) + 'closed))) + +;; Make arithmetic work on numbers in string form: +(define maybe-num + (let ((string? string?) + (string->number string->number)) + (lambda (arg) + (if (string? arg) + (let ((num (string->number arg))) + (if num num arg)) + arg)))) + +(define logoize + (let ((apply apply) + (map map) + (maybe-num maybe-num)) + (lambda (fn) + (lambda args + (apply fn (map maybe-num args)))))) + +;; special case versions of logoize, since (lambda args ...) is expensive +(define logoize-1 + (let ((maybe-num maybe-num)) + (lambda (fn) + (lambda (x) (fn (maybe-num x)))))) + +(define logoize-2 + (let ((maybe-num maybe-num)) + (lambda (fn) + (lambda (x y) (fn (maybe-num x) (maybe-num y)))))) + +(define strings-are-numbers + (let ((are-they? #f) + (real-* *) + (real-+ +) + (real-- -) + (real-/ /) + (real-< <) + (real-<= <=) + (real-= =) + (real-> >) + (real->= >=) + (real-abs abs) + (real-acos acos) + (real-asin asin) + (real-atan atan) + (real-ceiling ceiling) + (real-cos cos) + (real-even? even?) + (real-exp exp) + (real-expt expt) + (real-floor floor) + (real-align align) + (real-gcd gcd) + (real-integer? integer?) + (real-item item) + (real-lcm lcm) + (real-list-ref list-ref) + (real-log log) + (real-make-vector make-vector) + (real-max max) + (real-min min) + (real-modulo modulo) + (real-negative? negative?) + (real-number? number?) + (real-odd? odd?) + (real-positive? positive?) + (real-quotient quotient) + (real-random random) + (real-remainder remainder) + (real-repeated repeated) + (real-round round) + (real-sin sin) + (real-sqrt sqrt) + (real-tan tan) + (real-truncate truncate) + (real-vector-ref vector-ref) + (real-vector-set! vector-set!) + (real-zero? zero?) + (maybe-num maybe-num) + (number->string number->string) + (cons cons) + (car car) + (cdr cdr) + (eq? eq?) + (show show) + (logoize logoize) + (logoize-1 logoize-1) + (logoize-2 logoize-2) + (not not) + (whoops whoops)) + + (lambda (yesno) + (cond ((and are-they? (eq? yesno #t)) + (show "Strings are already numbers")) + ((eq? yesno #t) + (set! are-they? #t) + (set! * (logoize real-*)) + (set! + (logoize real-+)) + (set! - (logoize real--)) + (set! / (logoize real-/)) + (set! < (logoize real-<)) + (set! <= (logoize real-<=)) + (set! = (logoize real-=)) + (set! > (logoize real->)) + (set! >= (logoize real->=)) + (set! abs (logoize-1 real-abs)) + (set! acos (logoize-1 real-acos)) + (set! asin (logoize-1 real-asin)) + (set! atan (logoize real-atan)) + (set! ceiling (logoize-1 real-ceiling)) + (set! cos (logoize-1 real-cos)) + (set! even? (logoize-1 real-even?)) + (set! exp (logoize-1 real-exp)) + (set! expt (logoize-2 real-expt)) + (set! floor (logoize-1 real-floor)) + (set! align (logoize align)) + (set! gcd (logoize real-gcd)) + (set! integer? (logoize-1 real-integer?)) + (set! item (lambda (n stuff) + (real-item (maybe-num n) stuff))) + (set! lcm (logoize real-lcm)) + (set! list-ref (lambda (lst k) + (real-list-ref lst (maybe-num k)))) + (set! log (logoize-1 real-log)) + (set! max (logoize real-max)) + (set! min (logoize real-min)) + (set! modulo (logoize-2 real-modulo)) + (set! negative? (logoize-1 real-negative?)) + (set! number? (logoize-1 real-number?)) + (set! odd? (logoize-1 real-odd?)) + (set! positive? (logoize-1 real-positive?)) + (set! quotient (logoize-2 real-quotient)) + (set! random (logoize real-random)) + (set! remainder (logoize-2 real-remainder)) + (set! round (logoize-1 real-round)) + (set! sin (logoize-1 real-sin)) + (set! sqrt (logoize-1 real-sqrt)) + + (set! tan (logoize-1 real-tan)) + (set! truncate (logoize-1 real-truncate)) + (set! zero? (logoize-1 real-zero?)) + (set! vector-ref + (lambda (vec i) (real-vector-ref vec (maybe-num i)))) + (set! vector-set! + (lambda (vec i val) + (real-vector-set! vec (maybe-num i) val))) + (set! make-vector + (lambda (num . args) + (apply real-make-vector (cons (maybe-num num) + args)))) + (set! list-ref + (lambda (lst i) (real-list-ref lst (maybe-num i)))) + (set! repeated + (lambda (fn n) (real-repeated fn (maybe-num n))))) + ((and (not are-they?) (not yesno)) + (show "Strings are already not numbers")) + ((not yesno) + (set! are-they? #f) (set! * real-*) (set! + real-+) + (set! - real--) (set! / real-/) (set! < real-<) + (set! <= real-<=) (set! = real-=) (set! > real->) + (set! >= real->=) (set! abs real-abs) (set! acos real-acos) + (set! asin real-asin) (set! atan real-atan) + (set! ceiling real-ceiling) (set! cos real-cos) + (set! even? real-even?) + (set! exp real-exp) (set! expt real-expt) + (set! floor real-floor) (set! align real-align) + (set! gcd real-gcd) (set! integer? real-integer?) + (set! item real-item) + (set! lcm real-lcm) (set! list-ref real-list-ref) + (set! log real-log) (set! max real-max) (set! min real-min) + (set! modulo real-modulo) (set! odd? real-odd?) + (set! quotient real-quotient) (set! random real-random) + (set! remainder real-remainder) (set! round real-round) + (set! sin real-sin) (set! sqrt real-sqrt) (set! tan real-tan) + (set! truncate real-truncate) (set! zero? real-zero?) + (set! positive? real-positive?) (set! negative? real-negative?) + (set! number? real-number?) (set! vector-ref real-vector-ref) + (set! vector-set! real-vector-set!) + (set! make-vector real-make-vector) + (set! list-ref real-list-ref) (set! item real-item) + (set! repeated real-repeated)) + (else (whoops "Strings-are-numbers: give a #t or a #f"))) + are-they?))) + + +;; By default, strings are numbers: +(strings-are-numbers #t) + +(if scm? + (begin + (eval '(define (trace:untracef fun sym) + (cond ((memq sym *traced-procedures*) + (set! *traced-procedures* + (remove! sym *traced-procedures*)) + (untracef fun)) + (else + (display "WARNING: not traced " (current-error-port)) + (display sym (current-error-port)) + (newline (current-error-port)) + fun)))) + (eval '(define (edit file) + (ed file) + (load file))) + (eval '(define read + (let ((old-read read)) + (lambda args + (let* ((result (apply old-read args)) + (char (apply peek-char args))) + (if (end-of-line-char? char) + (apply read-char args) + '()) + result))))) + (eval `(define (end-of-line-char? char) + (eq? char ,(integer->char 10)))) + + ;; Don't get confusing "unspecified", and don't allow (define ((f x) y)..) + (eval '(define base:define define)) + (eval '(define define-fixup + (let ((pair? pair?) + (map map) + (eq? eq?) + (car car) + (list list) + (cdr cdr) + (cadr cadr) + (caadr caadr) + (cons cons) + (cdadr cdadr) + (cddr cddr)) + (lambda (exps) + (map (lambda (exp) + (if (and (pair? exp) + (eq? (car exp) 'define) + (pair? (cdr exp)) + (pair? (cadr exp))) + (list 'define + (caadr exp) + (cons 'lambda + (cons (cdadr exp) + (cddr exp)))) + exp)) + exps))))) + (eval '(define define-handler + (let ((cons cons) + (null? null?) + (car car) + (cdr cdr) + (remove! remove!) + (cddr cddr) + (pair? pair?) + (cadr cadr) + (list list) + (member member) + (caadr caadr)) + (lambda (exp env) + (cond ((or (null? (cdr exp)) (null? (cddr exp))) + (error "Badly formed DEFINE")) + ((not (pair? (cadr exp))) + (cond ((not (null? env)) (cons 'base:define (cdr exp))) + ((member (cadr exp) + '(define quote set! if cond else lambda + and or let cons-stream delay + begin quasiquote)) + (error "Can't redefine special form" (cadr exp))) + (else (eval (cons 'base:define (cdr exp))) + (set! *traced-procedures* + (remove! (cadr exp) + *traced-procedures*)) + (list 'quote (cadr exp))))) + ((pair? (caadr exp)) + (error "Badly formed DEFINE")) + (else + (cond ((not (null? env)) + (cons 'base:define (cdr exp))) + ((member (caadr exp) + '(define quote set! if cond else lambda + and or let cons-stream delay + begin quasiquote)) + (error "Can't redefine special form" (caadr exp))) + (else (eval (cons 'base:define + (define-fixup (cdr exp)))) + (set! *traced-procedures* + (remove! (caadr exp) + *traced-procedures*)) + (list 'quote (caadr exp)))))))))) + (eval '(define define (procedure->macro define-handler))) + (eval '(define fix-message + (let ((cons cons) + (car car) + (cdr cdr) + (procedure? procedure?) + (eval eval) + (set! set!)) + (procedure->macro + (lambda (exp env) + (let ((old (string->symbol + (string-append "base:" + (symbol->string (cadr exp)))))) + (eval `(define ,old ,(cadr exp))) + (if (procedure? (eval (cadr exp))) + `(set! ,(cadr exp) + (lambda args + (apply ,old args) + 'okay)) + `(set! ,(cadr exp) + (procedure->macro + (lambda (exp env) + `(begin ,(cons ',old (cdr exp)) + 'okay))))))))))) + (fix-message load) + (fix-message vector-set!) + (fix-message display) + (fix-message write) + (fix-message newline) + (fix-message close-input-port) + (fix-message close-output-port) + (fix-message for-each) + (fix-message set-car!) + (fix-message set-cdr!) + (fix-message transcript-on) + (fix-message transcript-off) + ;; (fix-message set!) + (eval '(define base:set! set!)) + (eval '(set! set! + (procedure->macro + (lambda (exp env) + (if (member (cadr exp) + '(define quote set! if cond else lambda and or + let cons-stream delay + begin quasiquote)) + (error "Can't redefine special form" (cadr exp)) + `(begin (base:set! ,(cadr exp) ,(caddr exp)) + 'okay)))))) + (set-current-error-port (current-output-port)) + + (verbose 1))) + +(if scm? + (begin + (eval '(define scm-xcenter 0)) + (eval '(define scm-ycenter 0)) + (eval '(define pen-color 7)) + (eval '(define bg-color 0)) + (eval '(define color-makers '#((set-color! 0) + (set-color! 9) + (set-color! 10) + (set-color! 11) + (set-color! 12) + (set-color! 13) + (set-color! 14) + (set-color! 15)))) + (eval '(define (clearscreen) + (graphics-mode!) + (clear-graphics!) + (goto-center!) + (set! scm-xcenter (where-x)) + (set! scm-ycenter (where-y)) + (turn-to! -90) + (setpc pen-color) + (if turtle-shown (show-turtle #t)))) + (eval '(define (internal-fd dist) + (if (pendown?) + (draw dist) + (move dist)))) + (eval '(define (internal-rt turn) + (turn-left turn))) + (eval '(define (internal-setxy newx newy) + ((if (pendown?) draw-to! move-to!) + (+ newx scm-xcenter) + (- scm-ycenter newy)))) + (eval '(define (internal-setheading newh) + (turn-to! (+ -90 newh)))) + (eval '(define (xcor) + (- (where-x) scm-xcenter))) + (eval '(define (ycor) + (- scm-ycenter (where-y)))) + (eval '(define (heading) + (- 90 (what-direction))))) + (begin + (eval '(define gambit-xcor 0)) + (eval '(define gambit-ycor 0)) + (eval '(define gambit-heading 0)) + (eval '(define pen-color 0)) + (eval '(define bg-color 7)) + (eval '(define color-makers '#((set-rgb-color 0 0 0) + (set-rgb-color 0 0 1) + (set-rgb-color 0 1 0) + (set-rgb-color 0 1 1) + (set-rgb-color 1 0 0) + (set-rgb-color 1 0 1) + (set-rgb-color 1 1 0) + (set-rgb-color 1 1 1)))) + (eval '(define (clearscreen) + (clear-graphics) + (position-pen 0 0) + (set! gambit-xcor 0) + (set! gambit-ycor 0) + (set! gambit-heading 0) + (if turtle-shown (show-turtle #t)))) + (eval '(define (internal-fd dist) + (set! gambit-xcor (+ gambit-xcor + (* dist (degree-sin gambit-heading)))) + (set! gambit-ycor (+ gambit-ycor + (* dist (degree-cos gambit-heading)))) + ((if (pendown?) draw-line-to position-pen) + gambit-xcor gambit-ycor))) + (eval '(define (internal-rt turn) + (set! gambit-heading (+ gambit-heading turn)) + (while (lambda () (< gambit-heading 0)) + (lambda () (set! gambit-heading (+ gambit-heading 360)))) + (while (lambda () (>= gambit-heading 360)) + (lambda () (set! gambit-heading (- gambit-heading 360)))))) + (eval '(define (while condition action) + (if (condition) (begin (action) (while condition action))))) + (eval '(define (degree-sin angle) + (sin (/ (* angle 3.141592654) 180)))) + (eval '(define (degree-cos angle) + (cos (/ (* angle 3.141592654) 180)))) + (eval '(define (internal-setxy newx newy) + (set! gambit-xcor newx) + (set! gambit-ycor newy) + ((if (pendown?) draw-line-to position-pen) + gambit-xcor gambit-ycor))) + (eval '(define (internal-setheading newh) + (set! gambit-heading newh) + (while (lambda () (< gambit-heading 0)) + (lambda () (set! gambit-heading (+ gambit-heading 360)))) + (while (lambda () (>= gambit-heading 360)) + (lambda () (set! gambit-heading (- gambit-heading 360)))))) + (eval '(define (xcor) gambit-xcor)) + (eval '(define (ycor) gambit-ycor)) + (eval '(define (heading) gambit-heading)))) + +(define turtle-shown #t) +(define (showturtle) + (if (not turtle-shown) (show-turtle #t)) + (set! turtle-shown #t)) +(define st showturtle) +(define (hideturtle) + (if turtle-shown (show-turtle #f)) + (set! turtle-shown #f)) +(define ht hideturtle) +(define (shown?) turtle-shown) + +(define (forward dist) + (if turtle-shown (show-turtle #f)) + (internal-fd dist) + (if turtle-shown (show-turtle #t))) +(define (right angle) + (if turtle-shown (show-turtle #f)) + (internal-rt angle) + (if turtle-shown (show-turtle #t))) +(define (setxy newx newy) + (if turtle-shown (show-turtle #f)) + (internal-setxy newx newy) + (if turtle-shown (show-turtle #t))) +(define (setheading newh) + (if turtle-shown (show-turtle #f)) + (internal-setheading newh) + (if turtle-shown (show-turtle #t))) + +(define (back dist) + (forward (- dist))) +(define fd forward) +(define bk back) +(define (left turn) + (right (- turn))) +(define lt left) +(define rt right) +(define (setx newx) + (setxy newx (ycor))) +(define (sety newy) + (setxy (xcor) newy)) +(define pendown-flag #t) +(define penerase-flag #f) +(define (pendown?) pendown-flag) +(define (pendown) + (set! pendown-flag #t) + (set! penerase-flag #f) + (set! true-pen-color pen-color) + (eval (vector-ref color-makers true-pen-color))) +(define pd pendown) +(define (penup) + (set! pendown-flag #f)) +(define pu penup) +(define (home) (setxy 0 0)) +(define cs clearscreen) +(define (pos) (list (xcor) (ycor))) +(define (setpencolor newc) + (eval (vector-ref color-makers newc)) + (set! pen-color newc) + (if turtle-shown (show-turtle #t)) + (if penerase-flag + (eval (vector-ref color-makers bg-color)) + (set! true-pen-color newc))) +(define setpc setpencolor) +(define (pencolor) pen-color) +(define pc pencolor) + +(define true-pen-color pen-color) +(define (penerase) + (set! true-pen-color bg-color) + (set! pendown-flag #t) + (set! penerase-flag #t) + (eval (vector-ref color-makers true-pen-color))) +(define pe penerase) + +(define turtle-base-angle (/ (* (acos (/ 1 3)) 180) 3.141592654)) +(define (show-turtle show-flag) + (let ((olderase penerase-flag) + (olddown pendown-flag)) + (if show-flag (pendown) (penerase)) + (internal-rt -90) + (internal-fd 5) + (internal-rt (- 180 turtle-base-angle)) + (internal-fd 15) + (internal-rt (* 2 turtle-base-angle)) + (internal-fd 15) + (internal-rt (- 180 turtle-base-angle)) + (internal-fd 5) + (internal-rt 90) + (if olddown + (if olderase (penerase) (pendown)) + (penup)))) + +(if scm? + (eval '(define repeat (procedure->macro + (lambda(x env) + `(repeat-helper ,(cadr x) (lambda () . ,(cddr x))))))) + (eval '(define-macro (repeat . args) + `(repeat-helper ,(car args) (lambda () . ,(cdr args)))))) + +(define (repeat-helper num thunk) + (if (<= num 0) + 'done + (begin (thunk) (repeat-helper (- num 1) thunk)))) + +(if scm? + (eval '(define call/cc call-with-current-continuation))) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/bst.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/bst.scm new file mode 100644 index 0000000..5815eae --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/bst.scm @@ -0,0 +1,28 @@ +(define (entry tree) (car tree)) + +(define (left-branch tree) (cadr tree)) + +(define (right-branch tree) (caddr tree)) + +(define (make-tree entry left right) + (list entry left right)) + +(define (element-of-set? x set) + (cond ((null? set) #f) + ((= x (entry set)) #t) + ((< x (entry set)) + (element-of-set? x (left-branch set))) + ((> x (entry set)) + (element-of-set? x (right-branch set))))) + +(define (adjoin-set x set) + (cond ((null? set) (make-tree x '() '())) + ((= x (entry set)) set) + ((< x (entry set)) + (make-tree (entry set) + (adjoin-set x (left-branch set)) + (right-branch set))) + ((> x (entry set)) + (make-tree (entry set) + (left-branch set) + (adjoin-set x (right-branch set)))))) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/calc.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/calc.scm new file mode 100644 index 0000000..7d667dd --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/calc.scm @@ -0,0 +1,29 @@ +;; Scheme calculator -- evaluate simple expressions + +; The read-eval-print loop: + +(define (calc) + (display "calc: ") + (flush) + (print (calc-eval (read))) + (calc)) + +; Evaluate an expression: + +(define (calc-eval exp) + (cond ((number? exp) exp) + ((list? exp) (calc-apply (car exp) (map calc-eval (cdr exp)))) + (else (error "Calc: bad expression:" exp)))) + +; Apply a function to arguments: + +(define (calc-apply fn args) + (cond ((eq? fn '+) (accumulate + 0 args)) + ((eq? fn '-) (cond ((null? args) (error "Calc: no args to -")) + ((= (length args) 1) (- (car args))) + (else (- (car args) (accumulate + 0 (cdr args)))))) + ((eq? fn '*) (accumulate * 1 args)) + ((eq? fn '/) (cond ((null? args) (error "Calc: no args to /")) + ((= (length args) 1) (/ (car args))) + (else (/ (car args) (accumulate * 1 (cdr args)))))) + (else (error "Calc: bad operator:" fn)))) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/chapter1.code b/js/games/nluqo.github.io/~bh/61a-pages/Lib/chapter1.code new file mode 100644 index 0000000..438f49f --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/chapter1.code @@ -0,0 +1,805 @@ +;;;;CODE FROM CHAPTER 1 OF STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS + +;;; Examples from the book are commented out with ;: so that they +;;; are easy to find and so that they will be omitted if you evaluate a +;;; chunk of the file (programs with intervening examples) in Scheme. + +;;; BEWARE: Although the whole file can be loaded into Scheme, +;;; don't expect the programs to work if you do so. For example, +;;; the redefinition of + in exercise 1.9 wreaks havoc with the +;;; last version of square defined here. + + +;;;SECTION 1.1.1 + +;; interpreter examples + +;: 486 + +;: (+ 137 349) +;: (- 1000 334) +;: (* 5 99) +;: (/ 10 5) +;: (+ 2.7 10) + +;: (+ 21 35 12 7) +;: (* 25 4 12) + +;: (+ (* 3 5) (- 10 6)) + +;: (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) + +;: (+ (* 3 +;: (+ (* 2 4) +;: (+ 3 5))) +;: (+ (- 10 7) +;: 6)) + + +;;;SECTION 1.1.2 + +;: (define size 2) +;: size +;: (* 5 size) + +;: (define pi 3.14159) +;: (define radius 10) +;: (* pi (* radius radius)) +;: (define circumference (* 2 pi radius)) +;: circumference + + +;;;SECTION 1.1.3 + +;: (* (+ 2 (* 4 6)) +;: (+ 3 5 7)) + + +;;;SECTION 1.1.4 + +(define (square x) (* x x)) + +;: (square 21) +;: (square (+ 2 5)) +;: (square (square 3)) + +(define (sum-of-squares x y) + (+ (square x) (square y))) + +;: (sum-of-squares 3 4) + +(define (f a) + (sum-of-squares (+ a 1) (* a 2))) + +;: (f 5) + + +;;;SECTION 1.1.5 + +;: (f 5) +;: (sum-of-squares (+ 5 1) (* 5 2)) +;: (+ (square 6) (square 10)) +;: (+ (* 6 6) (* 10 10)) +;: (+ 36 100) + +;: (f 5) +;: (sum-of-squares (+ 5 1) (* 5 2)) +;: (+ (square (+ 5 1)) (square (* 5 2)) ) +;: (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2))) +;: (+ (* 6 6) (* 10 10)) +;: (+ 36 100) +;: 136 + + +;;;SECTION 1.1.6 + +(define (abs x) + (cond ((> x 0) x) + ((= x 0) 0) + ((< x 0) (- x)))) + +(define (abs x) + (cond ((< x 0) (- x)) + (else x))) + +(define (abs x) + (if (< x 0) + (- x) + x)) + +;: (and (> x 5) (< x 10)) + +(define (>= x y) + (or (> x y) (= x y))) + +(define (>= x y) + (not (< x y))) + + +;;EXERCISE 1.1 +;: 10 + +;: (+ 5 3 4) + +;: (- 9 1) + +;: (/ 6 2) + +;: (+ (* 2 4) (- 4 6)) + +;: (define a 3) + +;: (define b (+ a 1)) + +;: (+ a b (* a b)) + +;: (= a b) + +;: (if (and (> b a) (< b (* a b))) +;: b +;: a) + +;: (cond ((= a 4) 6) +;: ((= b 4) (+ 6 7 a)) +;: (else 25)) + +;: (+ 2 (if (> b a) b a)) + +;: (* (cond ((> a b) a) +;: ((< a b) b) +;: (else -1)) +;: (+ a 1)) + +;;EXERCISE 1.4 +(define (a-plus-abs-b a b) + ((if (> b 0) + -) a b)) + +;;EXERCISE 1.5 +(define (p) (p)) + +(define (test x y) + (if (= x 0) + 0 + y)) + +;: (test 0 (p)) + + +;;;SECTION 1.1.7 + +(define (sqrt-iter guess x) + (if (good-enough? guess x) + guess + (sqrt-iter (improve guess x) + x))) + +(define (improve guess x) + (average guess (/ x guess))) + +(define (average x y) + (/ (+ x y) 2)) + +(define (good-enough? guess x) + (< (abs (- (square guess) x)) 0.001)) + +(define (sqrt x) + (sqrt-iter 1.0 x)) + + +;: (sqrt 9) +;: (sqrt (+ 100 37)) +;: (sqrt (+ (sqrt 2) (sqrt 3))) +;: (square (sqrt 1000)) + + +;;EXERCISE 1.6 +(define (new-if predicate then-clause else-clause) + (cond (predicate then-clause) + (else else-clause))) + +;: (new-if (= 2 3) 0 5) + +;: (new-if (= 1 1) 0 5) + +(define (sqrt-iter guess x) + (new-if (good-enough? guess x) + guess + (sqrt-iter (improve guess x) + x))) + + +;;;SECTION 1.1.8 + +(define (square x) (* x x)) + +(define (square x) + (exp (double (log x)))) + +(define (double x) (+ x x)) + + +;; As in 1.1.7 +(define (sqrt x) + (sqrt-iter 1.0 x)) + +(define (sqrt-iter guess x) + (if (good-enough? guess x) + guess + (sqrt-iter (improve guess x) x))) + +(define (good-enough? guess x) + (< (abs (- (square guess) x)) 0.001)) + +(define (improve guess x) + (average guess (/ x guess))) + + +;; Block-structured +(define (sqrt x) + (define (good-enough? guess x) + (< (abs (- (square guess) x)) 0.001)) + (define (improve guess x) + (average guess (/ x guess))) + (define (sqrt-iter guess x) + (if (good-enough? guess x) + guess + (sqrt-iter (improve guess x) x))) + (sqrt-iter 1.0 x)) + +;; Taking advantage of lexical scoping +(define (sqrt x) + (define (good-enough? guess) + (< (abs (- (square guess) x)) 0.001)) + (define (improve guess) + (average guess (/ x guess))) + (define (sqrt-iter guess) + (if (good-enough? guess) + guess + (sqrt-iter (improve guess)))) + (sqrt-iter 1.0)) + +;;;SECTION 1.2.1 + +;; Recursive + +(define (factorial n) + (if (= n 1) + 1 + (* n (factorial (- n 1))))) + + +;; Iterative + +(define (factorial n) + (fact-iter 1 1 n)) + +(define (fact-iter product counter max-count) + (if (> counter max-count) + product + (fact-iter (* counter product) + (+ counter 1) + max-count))) + +;; Iterative, block-structured (from footnote) +(define (factorial n) + (define (iter product counter) + (if (> counter n) + product + (iter (* counter product) + (+ counter 1)))) + (iter 1 1)) + + +;;EXERCISE 1.9 +(define (+ a b) + (if (= a 0) + b + (inc (+ (dec a) b)))) + +(define (+ a b) + (if (= a 0) + b + (+ (dec a) (inc b)))) + +;;EXERCISE 1.10 +(define (A x y) + (cond ((= y 0) 0) + ((= x 0) (* 2 y)) + ((= y 1) 2) + (else (A (- x 1) + (A x (- y 1)))))) + +;: (A 1 10) + +;: (A 2 4) + +;: (A 3 3) + +(define (f n) (A 0 n)) + +(define (g n) (A 1 n)) + +(define (h n) (A 2 n)) + +(define (k n) (* 5 n n)) + + +;;;SECTION 1.2.2 + +;; Recursive + +(define (fib n) + (cond ((= n 0) 0) + ((= n 1) 1) + (else (+ (fib (- n 1)) + (fib (- n 2)))))) + +;; Iterative + +(define (fib n) + (fib-iter 1 0 n)) + +(define (fib-iter a b count) + (if (= count 0) + b + (fib-iter (+ a b) a (- count 1)))) + + +;; Counting change + +(define (count-change amount) + (cc amount 5)) + +(define (cc amount kinds-of-coins) + (cond ((= amount 0) 1) + ((or (< amount 0) (= kinds-of-coins 0)) 0) + (else (+ (cc amount + (- kinds-of-coins 1)) + (cc (- amount + (first-denomination kinds-of-coins)) + kinds-of-coins))))) + +(define (first-denomination kinds-of-coins) + (cond ((= kinds-of-coins 1) 1) + ((= kinds-of-coins 2) 5) + ((= kinds-of-coins 3) 10) + ((= kinds-of-coins 4) 25) + ((= kinds-of-coins 5) 50))) + +;: (count-change 100) + + +;;;SECTION 1.2.3 + +;;EXERCISE 1.15 +(define (cube x) (* x x x)) + +(define (p x) (- (* 3 x) (* 4 (cube x)))) + +(define (sine angle) + (if (not (> (abs angle) 0.1)) + angle + (p (sine (/ angle 3.0))))) + + +;;;SECTION 1.2.4 + +;; Linear recursion +(define (expt b n) + (if (= n 0) + 1 + (* b (expt b (- n 1))))) + + +;; Linear iteration +(define (expt b n) + (expt-iter b n 1)) + +(define (expt-iter b counter product) + (if (= counter 0) + product + (expt-iter b + (- counter 1) + (* b product)))) + +;; Logarithmic iteration +(define (fast-expt b n) + (cond ((= n 0) 1) + ((even? n) (square (fast-expt b (/ n 2)))) + (else (* b (fast-expt b (- n 1)))))) + +(define (even? n) + (= (remainder n 2) 0)) + + +;;EXERCISE 1.17 +(define (* a b) + (if (= b 0) + 0 + (+ a (* a (- b 1))))) + +;;EXERCISE 1.19 +(define (fib n) + (fib-iter 1 0 0 1 n)) + +(define (fib-iter a b p q count) + (cond ((= count 0) b) + ((even? count) + (fib-iter a + b + ;;?? compute p + ;;?? compute q + (/ count 2))) + (else (fib-iter (+ (* b q) (* a q) (* a p)) + (+ (* b p) (* a q)) + p + q + (- count 1))))) + + +;;;SECTION 1.2.5 + +(define (gcd a b) + (if (= b 0) + a + (gcd b (remainder a b)))) + + +;;;SECTION 1.2.6 + +;; prime? + +(define (smallest-divisor n) + (find-divisor n 2)) + +(define (find-divisor n test-divisor) + (cond ((> (square test-divisor) n) n) + ((divides? test-divisor n) test-divisor) + (else (find-divisor n (+ test-divisor 1))))) + +(define (divides? a b) + (= (remainder b a) 0)) + +(define (prime? n) + (= n (smallest-divisor n))) + + +;; fast-prime? + +(define (expmod base exp m) + (cond ((= exp 0) 1) + ((even? exp) + (remainder (square (expmod base (/ exp 2) m)) + m)) + (else + (remainder (* base (expmod base (- exp 1) m)) + m)))) + +(define (fermat-test n) + (define (try-it a) + (= (expmod a n n) a)) + (try-it (+ 1 (random (- n 1))))) + +(define (fast-prime? n times) + (cond ((= times 0) true) + ((fermat-test n) (fast-prime? n (- times 1))) + (else false))) + + +;;EXERCISE 1.22 +(define (timed-prime-test n) + (newline) + (display n) + (start-prime-test n (runtime))) + +(define (start-prime-test n start-time) + (if (prime? n) + (report-prime (- (runtime) start-time)))) + +(define (report-prime elapsed-time) + (display " *** ") + (display elapsed-time)) + +;;EXERCISE 1.25 +(define (expmod base exp m) + (remainder (fast-expt base exp) m)) + +;;EXERCISE 1.26 +(define (expmod base exp m) + (cond ((= exp 0) 1) + ((even? exp) + (remainder (* (expmod base (/ exp 2) m) + (expmod base (/ exp 2) m)) + m)) + (else + (remainder (* base (expmod base (- exp 1) m)) + m)))) + +;;;SECTION 1.3 + +(define (cube x) (* x x x)) + +;;;SECTION 1.3.1 + +(define (sum-integers a b) + (if (> a b) + 0 + (+ a (sum-integers (+ a 1) b)))) + +(define (sum-cubes a b) + (if (> a b) + 0 + (+ (cube a) (sum-cubes (+ a 1) b)))) + +(define (pi-sum a b) + (if (> a b) + 0 + (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b)))) + +(define (sum term a next b) + (if (> a b) + 0 + (+ (term a) + (sum term (next a) next b)))) + + +;; Using sum + +(define (inc n) (+ n 1)) + +(define (sum-cubes a b) + (sum cube a inc b)) + +;: (sum-cubes 1 10) + + +(define (identity x) x) + +(define (sum-integers a b) + (sum identity a inc b)) + +;: (sum-integers 1 10) + + +(define (pi-sum a b) + (define (pi-term x) + (/ 1.0 (* x (+ x 2)))) + (define (pi-next x) + (+ x 4)) + (sum pi-term a pi-next b)) + +;: (* 8 (pi-sum 1 1000)) + + +(define (integral f a b dx) + (define (add-dx x) (+ x dx)) + (* (sum f (+ a (/ dx 2)) add-dx b) + dx)) + +;: (integral cube 0 1 0.01) + +;: (integral cube 0 1 0.001) + + +;;EXERCISE 1.32 +;: (accumulate combiner null-value term a next b) + +;;;SECTION 1.3.2 + +(define (pi-sum a b) + (sum (lambda (x) (/ 1.0 (* x (+ x 2)))) + a + (lambda (x) (+ x 4)) + b)) + +(define (integral f a b dx) + (* (sum f + (+ a (/ dx 2.0)) + (lambda (x) (+ x dx)) + b) + dx)) + +(define (plus4 x) (+ x 4)) + +(define plus4 (lambda (x) (+ x 4))) + +;: ((lambda (x y z) (+ x y (square z))) 1 2 3) + + +;; Using let + +(define (f x y) + (define (f-helper a b) + (+ (* x (square a)) + (* y b) + (* a b))) + (f-helper (+ 1 (* x y)) + (- 1 y))) + +(define (f x y) + ((lambda (a b) + (+ (* x (square a)) + (* y b) + (* a b))) + (+ 1 (* x y)) + (- 1 y))) + +(define (f x y) + (let ((a (+ 1 (* x y))) + (b (- 1 y))) + (+ (* x (square a)) + (* y b) + (* a b)))) + +;: (+ (let ((x 3)) +;: (+ x (* x 10))) +;: x) + +;: (let ((x 3) +;: (y (+ x 2))) +;: (* x y)) + +(define (f x y) + (define a (+ 1 (* x y))) + (define b (- 1 y)) + (+ (* x (square a)) + (* y b) + (* a b))) + + +;;EXERCISE 1.34 +(define (f g) + (g 2)) + +;: (f square) + +;: (f (lambda (z) (* z (+ z 1)))) + + +;;;SECTION 1.3.3 + +;; Half-interval method + +(define (search f neg-point pos-point) + (let ((midpoint (average neg-point pos-point))) + (if (close-enough? neg-point pos-point) + midpoint + (let ((test-value (f midpoint))) + (cond ((positive? test-value) + (search f neg-point midpoint)) + ((negative? test-value) + (search f midpoint pos-point)) + (else midpoint)))))) + +(define (close-enough? x y) + (< (abs (- x y)) 0.001)) + +(define (half-interval-method f a b) + (let ((a-value (f a)) + (b-value (f b))) + (cond ((and (negative? a-value) (positive? b-value)) + (search f a b)) + ((and (negative? b-value) (positive? a-value)) + (search f b a)) + (else + (error "Values are not of opposite sign" a b))))) + + +;: (half-interval-method sin 2.0 4.0) + +;: (half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3)) +;: 1.0 +;: 2.0) + + +;; Fixed points + +(define tolerance 0.00001) + +(define (fixed-point f first-guess) + (define (close-enough? v1 v2) + (< (abs (- v1 v2)) tolerance)) + (define (try guess) + (let ((next (f guess))) + (if (close-enough? guess next) + next + (try next)))) + (try first-guess)) + + +;: (fixed-point cos 1.0) + +;: (fixed-point (lambda (y) (+ (sin y) (cos y))) +;: 1.0) + + +(define (sqrt x) + (fixed-point (lambda (y) (/ x y)) + 1.0)) + +(define (sqrt x) + (fixed-point (lambda (y) (average y (/ x y))) + 1.0)) + + +;;EXERCISE 1.37 +;: (cont-frac (lambda (i) 1.0) +;: (lambda (i) 1.0) +;: k) + + +;;;SECTION 1.3.4 + +(define (average-damp f) + (lambda (x) (average x (f x)))) + +;: ((average-damp square) 10) + +(define (sqrt x) + (fixed-point (average-damp (lambda (y) (/ x y))) + 1.0)) + +(define (cube-root x) + (fixed-point (average-damp (lambda (y) (/ x (square y)))) + 1.0)) + + +;; Newton's method + +(define (deriv g) + (lambda (x) + (/ (- (g (+ x dx)) (g x)) + dx))) +(define dx 0.00001) + + +(define (cube x) (* x x x)) + +;: ((deriv cube) 5) + +(define (newton-transform g) + (lambda (x) + (- x (/ (g x) ((deriv g) x))))) + +(define (newtons-method g guess) + (fixed-point (newton-transform g) guess)) + + +(define (sqrt x) + (newtons-method (lambda (y) (- (square y) x)) + 1.0)) + + +;; Fixed point of transformed function + +(define (fixed-point-of-transform g transform guess) + (fixed-point (transform g) guess)) + +(define (sqrt x) + (fixed-point-of-transform (lambda (y) (/ x y)) + average-damp + 1.0)) + +(define (sqrt x) + (fixed-point-of-transform (lambda (y) (- (square y) x)) + newton-transform + 1.0)) + + +;;EXERCISE 1.40 +;: (newtons-method (cubic a b c) 1) + + +;;EXERCISE 1.41 +;: (((double (double double)) inc) 5) + + +;;EXERCISE 1.42 +;: ((compose square inc) 6) + + +;;EXERCISE 1.43 +;: ((repeated square 2) 5) + + + diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/chapter2.code b/js/games/nluqo.github.io/~bh/61a-pages/Lib/chapter2.code new file mode 100644 index 0000000..78dcccd --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/chapter2.code @@ -0,0 +1,1966 @@ +; -----***CH2.SCM*** +;;;;CODE FROM CHAPTER 2 OF STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS + +;;; Examples from the book are commented out with ;: so that they +;;; are easy to find and so that they will be omitted if you evaluate a +;;; chunk of the file (programs with intervening examples) in Scheme. + +;;; BEWARE: Although the whole file can be loaded into Scheme, +;;; you won't want to do so. For example, you generally do +;;; not want to use the procedural representation of pairs +;;; (cons, car, cdr as defined in section 2.1.3) instead of +;;; Scheme's primitive pairs. + +;;; Some things require code from other chapters -- see ch2support.scm + + +(define (linear-combination a b x y) + (+ (* a x) (* b y))) + +(define (linear-combination a b x y) + (add (mul a x) (mul b y))) + + +;;;SECTION 2.1.1 + +(define (add-rat x y) + (make-rat (+ (* (numer x) (denom y)) + (* (numer y) (denom x))) + (* (denom x) (denom y)))) + +(define (sub-rat x y) + (make-rat (- (* (numer x) (denom y)) + (* (numer y) (denom x))) + (* (denom x) (denom y)))) + +(define (mul-rat x y) + (make-rat (* (numer x) (numer y)) + (* (denom x) (denom y)))) + +(define (div-rat x y) + (make-rat (* (numer x) (denom y)) + (* (denom x) (numer y)))) + +(define (equal-rat? x y) + (= (* (numer x) (denom y)) + (* (numer y) (denom x)))) + +;: (define x (cons 1 2)) +;: +;: (car x) +;: (cdr x) + +;: (define x (cons 1 2)) +;: (define y (cons 3 4)) +;: (define z (cons x y)) +;: (car (car z)) +;: (car (cdr z)) + +(define (make-rat n d) (cons n d)) + +(define (numer x) (car x)) + +(define (denom x) (cdr x)) + +;;footnote -- alternative definitions +(define make-rat cons) +(define numer car) +(define denom cdr) + +(define (print-rat x) + (newline) + (display (numer x)) + (display "/") + (display (denom x))) + + +;: (define one-half (make-rat 1 2)) +;: +;: (print-rat one-half) +;: +;: (define one-third (make-rat 1 3)) +;: +;: (print-rat (add-rat one-half one-third)) +;: (print-rat (mul-rat one-half one-third)) +;: (print-rat (add-rat one-third one-third)) + + +;; reducing to lowest terms in constructor +;; (uses gcd from 1.2.5 -- see ch2support.scm) + +(define (make-rat n d) + (let ((g (gcd n d))) + (cons (/ n g) (/ d g)))) + + +;: (print-rat (add-rat one-third one-third)) + + +;;;SECTION 2.1.2 + +;; reducing to lowest terms in selectors +;; (uses gcd from 1.2.5 -- see ch2support.scm) + +(define (make-rat n d) + (cons n d)) + +(define (numer x) + (let ((g (gcd (car x) (cdr x)))) + (/ (car x) g))) + +(define (denom x) + (let ((g (gcd (car x) (cdr x)))) + (/ (cdr x) g))) + + +;; EXERCISE 2.2 +(define (print-point p) + (newline) + (display "(") + (display (x-point p)) + (display ",") + (display (y-point p)) + (display ")")) + + +;;;SECTION 2.1.3 +(define (cons x y) + (define (dispatch m) + (cond ((= m 0) x) + ((= m 1) y) + (else (error "Argument not 0 or 1 -- CONS" m)))) + dispatch) + +(define (car z) (z 0)) + +(define (cdr z) (z 1)) + + +;; EXERCISE 2.4 + +(define (cons x y) + (lambda (m) (m x y))) + +(define (car z) + (z (lambda (p q) p))) + + +;; EXERCISE 2.6 +(define zero (lambda (f) (lambda (x) x))) + +(define (add-1 n) + (lambda (f) (lambda (x) (f ((n f) x))))) + + +;;;SECTION 2.1.4 + +(define (add-interval x y) + (make-interval (+ (lower-bound x) (lower-bound y)) + (+ (upper-bound x) (upper-bound y)))) + +(define (mul-interval x y) + (let ((p1 (* (lower-bound x) (lower-bound y))) + (p2 (* (lower-bound x) (upper-bound y))) + (p3 (* (upper-bound x) (lower-bound y))) + (p4 (* (upper-bound x) (upper-bound y)))) + (make-interval (min p1 p2 p3 p4) + (max p1 p2 p3 p4)))) + +(define (div-interval x y) + (mul-interval x + (make-interval (/ 1.0 (upper-bound y)) + (/ 1.0 (lower-bound y))))) + +;; EXERCISE 2.7 + +(define (make-interval a b) (cons a b)) + + +;;;SECTION 2.1.4 again + +(define (make-center-width c w) + (make-interval (- c w) (+ c w))) + +(define (center i) + (/ (+ (lower-bound i) (upper-bound i)) 2)) + +(define (width i) + (/ (- (upper-bound i) (lower-bound i)) 2)) + +;; parallel resistors + +(define (par1 r1 r2) + (div-interval (mul-interval r1 r2) + (add-interval r1 r2))) + +(define (par2 r1 r2) + (let ((one (make-interval 1 1))) + (div-interval one + (add-interval (div-interval one r1) + (div-interval one r2))))) + +;;;SECTION 2.2.1 + +;: (cons 1 +;: (cons 2 +;: (cons 3 +;: (cons 4 nil)))) + + +;: (define one-through-four (list 1 2 3 4)) +;: +;: one-through-four +;: (car one-through-four) +;: (cdr one-through-four) +;: (car (cdr one-through-four)) +;: (cons 10 one-through-four) + +(define (list-ref items n) + (if (= n 0) + (car items) + (list-ref (cdr items) (- n 1)))) + +;: (define squares (list 1 4 9 16 25)) + +;: (list-ref squares 3) + +(define (length items) + (if (null? items) + 0 + (+ 1 (length (cdr items))))) + +;: (define odds (list 1 3 5 7)) + +;: (length odds) + +(define (length items) + (define (length-iter a count) + (if (null? a) + count + (length-iter (cdr a) (+ 1 count)))) + (length-iter items 0)) + +;: (append squares odds) +;: (append odds squares) + + +(define (append list1 list2) + (if (null? list1) + list2 + (cons (car list1) (append (cdr list1) list2)))) + + +;; EXERCISE 2.17 +;: (last-pair (list 23 72 149 34)) + + +;; EXERCISE 2.18 +;: (reverse (list 1 4 9 16 25)) + +;; EXERCISE 2.19 +(define us-coins (list 50 25 10 5 1)) + +(define uk-coins (list 100 50 20 10 5 2 1 0.5)) + +;: (cc 100 us-coins) + +(define (cc amount coin-values) + (cond ((= amount 0) 1) + ((or (< amount 0) (no-more? coin-values)) 0) + (else + (+ (cc amount + (except-first-denomination coin-values)) + (cc (- amount + (first-denomination coin-values)) + coin-values))))) + +;; EXERCISE 2.20 +;: (same-parity 1 2 3 4 5 6 7) +;: (same-parity 2 3 4 5 6 7) + + +;; Mapping over lists + +(define (scale-list items factor) + (if (null? items) + nil + (cons (* (car items) factor) + (scale-list (cdr items) factor)))) + +;: (scale-list (list 1 2 3 4 5) 10) + +;: (map + (list 1 2 3) (list 40 50 60) (list 700 800 900)) + +;: (map (lambda (x y) (+ x (* 2 y))) +;: (list 1 2 3) +;: (list 4 5 6)) + +(define (map proc items) + (if (null? items) + nil + (cons (proc (car items)) + (map proc (cdr items))))) + +;: (map abs (list -10 2.5 -11.6 17)) + +;: (map (lambda (x) (* x x)) +;: (list 1 2 3 4)) + +(define (scale-list items factor) + (map (lambda (x) (* x factor)) + items)) + + +;; EXERCISE 2.21 +;: (square-list (list 1 2 3 4)) + + +;; EXERCISE 2.22 +(define (square-list items) + (define (iter things answer) + (if (null? things) + answer + (iter (cdr things) + (cons (square (car things)) + answer)))) + (iter items nil)) + +(define (square-list items) + (define (iter things answer) + (if (null? things) + answer + (iter (cdr things) + (cons answer + (square (car things)))))) + (iter items nil)) + + +;; EXERCISE 2.23 + +;: (for-each (lambda (x) (newline) (display x)) +;: (list 57 321 88)) + + + +;;;SECTION 2.2.2 +;: (cons (list 1 2) (list 3 4)) +;: +;: (define x (cons (list 1 2) (list 3 4))) +;: (length x) +;: (count-leaves x) +;: +;: (list x x) +;: (length (list x x)) +;: (count-leaves (list x x)) + +(define (count-leaves x) + (cond ((null? x) 0) + ((not (pair? x)) 1) + (else (+ (count-leaves (car x)) + (count-leaves (cdr x)))))) + +;; EXERCISE 2.24 +;: (list 1 (list 2 (list 3 4))) + +;; EXERCISE 2.25 +;: (1 3 (5 7) 9) +;: ((7)) +;: (1 (2 (3 (4 (5 (6 7)))))) + +;; EXERCISE 2.26 +;: (define x (list 1 2 3)) +;: (define y (list 4 5 6)) +;: +;: (append x y) +;: (cons x y) +;: (list x y) + +;; EXERCISE 2.27 + +;: (define x (list (list 1 2) (list 3 4))) +;: x +;: (reverse x) +;: (deep-reverse x) + + +;; EXERCISE 2.28 + +;: (define x (list (list 1 2) (list 3 4))) +;: (fringe x) +;: (fringe (list x x)) + + +;; EXERCISE 2.29 +(define (make-mobile left right) + (list left right)) + +(define (make-branch length structure) + (list length structure)) + + +;; part d +(define (make-mobile left right) + (cons left right)) + +(define (make-branch length structure) + (cons length structure)) + + +;; Mapping over trees + +(define (scale-tree tree factor) + (cond ((null? tree) nil) + ((not (pair? tree)) (* tree factor)) + (else (cons (scale-tree (car tree) factor) + (scale-tree (cdr tree) factor))))) + + +;: (scale-tree (list 1 (list 2 (list 3 4) 5) (list 6 7)) +;: 10) + + +(define (scale-tree tree factor) + (map (lambda (sub-tree) + (if (pair? sub-tree) + (scale-tree sub-tree factor) + (* sub-tree factor))) + tree)) + + +;; EXERCISE 2.30 +;: (square-tree +;: (list 1 +;: (list 2 (list 3 4) 5) +;: (list 6 7))) + + +;; EXERCISE 2.31 +(define (square-tree tree) (tree-map square tree)) + + +;; EXERCISE 2.32 +(define (subsets s) + (if (null? s) + (list nil) + (let ((rest (subsets (cdr s)))) + (append rest (map ??FILL-THIS-IN?? rest))))) + + +;;;SECTION 2.2.3 + +(define (sum-odd-squares tree) + (cond ((null? tree) 0) + ((not (pair? tree)) + (if (odd? tree) (square tree) 0)) + (else (+ (sum-odd-squares (car tree)) + (sum-odd-squares (cdr tree)))))) + +(define (even-fibs n) + (define (next k) + (if (> k n) + nil + (let ((f (fib k))) + (if (even? f) + (cons f (next (+ k 1))) + (next (+ k 1)))))) + (next 0)) + + +;; Sequence operations + +;: (map square (list 1 2 3 4 5)) + +(define (filter predicate sequence) + (cond ((null? sequence) nil) + ((predicate (car sequence)) + (cons (car sequence) + (filter predicate (cdr sequence)))) + (else (filter predicate (cdr sequence))))) + +;: (filter odd? (list 1 2 3 4 5)) + +(define (accumulate op initial sequence) + (if (null? sequence) + initial + (op (car sequence) + (accumulate op initial (cdr sequence))))) + +;: (accumulate + 0 (list 1 2 3 4 5)) +;: (accumulate * 1 (list 1 2 3 4 5)) +;: (accumulate cons nil (list 1 2 3 4 5)) + +(define (enumerate-interval low high) + (if (> low high) + nil + (cons low (enumerate-interval (+ low 1) high)))) + +;: (enumerate-interval 2 7) + +(define (enumerate-tree tree) + (cond ((null? tree) nil) + ((not (pair? tree)) (list tree)) + (else (append (enumerate-tree (car tree)) + (enumerate-tree (cdr tree)))))) + +;: (enumerate-tree (list 1 (list 2 (list 3 4)) 5)) + +(define (sum-odd-squares tree) + (accumulate + + 0 + (map square + (filter odd? + (enumerate-tree tree))))) + +(define (even-fibs n) + (accumulate cons + nil + (filter even? + (map fib + (enumerate-interval 0 n))))) + +(define (list-fib-squares n) + (accumulate cons + nil + (map square + (map fib + (enumerate-interval 0 n))))) + +;: (list-fib-squares 10) + + +(define (product-of-squares-of-odd-elements sequence) + (accumulate * + 1 + (map square + (filter odd? sequence)))) + +;: (product-of-squares-of-odd-elements (list 1 2 3 4 5)) + +(define (salary-of-highest-paid-programmer records) + (accumulate max + 0 + (map salary + (filter programmer? records)))) + + +;; EXERCISE 2.34 +(define (horner-eval x coefficient-sequence) + (accumulate (lambda (this-coeff higher-terms) ??FILL-THIS-IN??) + 0 + coefficient-sequence)) + +;: (horner-eval 2 (list 1 3 0 5 0 1)) + +;; EXERCISE 2.36 +(define (accumulate-n op init seqs) + (if (null? (car seqs)) + nil + (cons (accumulate op init ??FILL-THIS-IN??) + (accumulate-n op init ??FILL-THIS-IN??)))) + +;: (accumulate-n + 0 s) + +;; EXERCISE 2.37 + +(define (dot-product v w) + (accumulate + 0 (map * v w))) + + +;; EXERCISE 2.38 + +(define (fold-left op initial sequence) + (define (iter result rest) + (if (null? rest) + result + (iter (op result (car rest)) + (cdr rest)))) + (iter initial sequence)) + +;: (fold-right / 1 (list 1 2 3)) +;: (fold-left / 1 (list 1 2 3)) +;: (fold-right list nil (list 1 2 3)) +;: (fold-left list nil (list 1 2 3)) + + +;;Nested mappings + +;: (accumulate append +;: nil +;: (map (lambda (i) +;: (map (lambda (j) (list i j)) +;: (enumerate-interval 1 (- i 1)))) +;: (enumerate-interval 1 n))) + +(define (flatmap proc seq) + (accumulate append nil (map proc seq))) + +(define (prime-sum? pair) + (prime? (+ (car pair) (cadr pair)))) + +(define (make-pair-sum pair) + (list (car pair) (cadr pair) (+ (car pair) (cadr pair)))) + +(define (prime-sum-pairs n) + (map make-pair-sum + (filter prime-sum? + (flatmap + (lambda (i) + (map (lambda (j) (list i j)) + (enumerate-interval 1 (- i 1)))) + (enumerate-interval 1 n))))) + + +(define (permutations s) + (if (null? s) ; empty set? + (list nil) ; sequence containing empty set + (flatmap (lambda (x) + (map (lambda (p) (cons x p)) + (permutations (remove x s)))) + s))) + +(define (remove item sequence) + (filter (lambda (x) (not (= x item))) + sequence)) + + +;; EXERCISE 2.42 +(define (queens board-size) + (define (queen-cols k) + (if (= k 0) + (list empty-board) + (filter + (lambda (positions) (safe? k positions)) + (flatmap + (lambda (rest-of-queens) + (map (lambda (new-row) + (adjoin-position new-row k rest-of-queens)) + (enumerate-interval 1 board-size))) + (queen-cols (- k 1)))))) + (queen-cols board-size)) + +;; EXERCISE 2.43 +;; Louis's version of queens +(define (queens board-size) + (define (queen-cols k) + (if (= k 0) + (list empty-board) + (filter + (lambda (positions) (safe? k positions)) + ;; next expression changed + (flatmap + (lambda (new-row) + (map (lambda (rest-of-queens) + (adjoin-position new-row k rest-of-queens)) + (queen-cols (- k 1)))) + (enumerate-interval 1 board-size))))) + (queen-cols board-size)) + +;;;SECTION 2.2.4 + +;: (define wave2 (beside wave (flip-vert wave))) +;: (define wave4 (below wave2 wave2)) + + +(define (flipped-pairs painter) + (let ((painter2 (beside painter (flip-vert painter)))) + (below painter2 painter2))) + + +;: (define wave4 (flipped-pairs wave)) + + +(define (right-split painter n) + (if (= n 0) + painter + (let ((smaller (right-split painter (- n 1)))) + (beside painter (below smaller smaller))))) + + +(define (corner-split painter n) + (if (= n 0) + painter + (let ((up (up-split painter (- n 1))) + (right (right-split painter (- n 1)))) + (let ((top-left (beside up up)) + (bottom-right (below right right)) + (corner (corner-split painter (- n 1)))) + (beside (below painter top-left) + (below bottom-right corner)))))) + + +(define (square-limit painter n) + (let ((quarter (corner-split painter n))) + (let ((half (beside (flip-horiz quarter) quarter))) + (below (flip-vert half) half)))) + + +;; Higher-order operations + +(define (square-of-four tl tr bl br) + (lambda (painter) + (let ((top (beside (tl painter) (tr painter))) + (bottom (beside (bl painter) (br painter)))) + (below bottom top)))) + + +(define (flipped-pairs painter) + (let ((combine4 (square-of-four identity flip-vert + identity flip-vert))) + (combine4 painter))) + +; footnote +;: (define flipped-pairs +;: (square-of-four identity flip-vert identity flip-vert)) + + +(define (square-limit painter n) + (let ((combine4 (square-of-four flip-horiz identity + rotate180 flip-vert))) + (combine4 (corner-split painter n)))) + + +;; EXERCISE 2.45 + +;: (define right-split (split beside below)) +;: (define up-split (split below beside)) + + +;; Frames + +(define (frame-coord-map frame) + (lambda (v) + (add-vect + (origin-frame frame) + (add-vect (scale-vect (xcor-vect v) + (edge1-frame frame)) + (scale-vect (ycor-vect v) + (edge2-frame frame)))))) + + +;: ((frame-coord-map a-frame) (make-vect 0 0)) + +;: (origin-frame a-frame) + + +;; EXERCISE 2.47 + +(define (make-frame origin edge1 edge2) + (list origin edge1 edge2)) + +(define (make-frame origin edge1 edge2) + (cons origin (cons edge1 edge2))) + + +;; Painters + +(define (segments->painter segment-list) + (lambda (frame) + (for-each + (lambda (segment) + (draw-line + ((frame-coord-map frame) (start-segment segment)) + ((frame-coord-map frame) (end-segment segment)))) + segment-list))) + + +(define (transform-painter painter origin corner1 corner2) + (lambda (frame) + (let ((m (frame-coord-map frame))) + (let ((new-origin (m origin))) + (painter + (make-frame new-origin + (sub-vect (m corner1) new-origin) + (sub-vect (m corner2) new-origin))))))) + + +(define (flip-vert painter) + (transform-painter painter + (make-vect 0.0 1.0) ; new origin + (make-vect 1.0 1.0) ; new end of edge1 + (make-vect 0.0 0.0))) ; new end of edge2 + + +(define (shrink-to-upper-right painter) + (transform-painter painter + (make-vect 0.5 0.5) + (make-vect 1.0 0.5) + (make-vect 0.5 1.0))) + + +(define (rotate90 painter) + (transform-painter painter + (make-vect 1.0 0.0) + (make-vect 1.0 1.0) + (make-vect 0.0 0.0))) + + +(define (squash-inwards painter) + (transform-painter painter + (make-vect 0.0 0.0) + (make-vect 0.65 0.35) + (make-vect 0.35 0.65))) + + +(define (beside painter1 painter2) + (let ((split-point (make-vect 0.5 0.0))) + (let ((paint-left + (transform-painter painter1 + (make-vect 0.0 0.0) + split-point + (make-vect 0.0 1.0))) + (paint-right + (transform-painter painter2 + split-point + (make-vect 1.0 0.0) + (make-vect 0.5 1.0)))) + (lambda (frame) + (paint-left frame) + (paint-right frame))))) + +;;;SECTION 2.3.1 + +;: (a b c d) +;: (23 45 17) +;: ((Norah 12) (Molly 9) (Anna 7) (Lauren 6) (Charlotte 3)) + +;: (* (+ 23 45) (+ x 9)) + +(define (fact n) (if (= n 1) 1 (* n (fact (- n 1))))) + + +;: (define a 1) +;: (define b 2) +;: (list a b) +;: (list 'a 'b) +;: (list 'a b) + +;: (car '(a b c)) +;: (cdr '(a b c)) + + +(define (memq item x) + (cond ((null? x) false) + ((eq? item (car x)) x) + (else (memq item (cdr x))))) + +;: (memq 'apple '(pear banana prune)) +;: (memq 'apple '(x (apple sauce) y apple pear)) + + +;; EXERCISE 2.53 +;: (list 'a 'b 'c) +;: +;: (list (list 'george)) +;: +;: (cdr '((x1 x2) (y1 y2))) +;: +;: (cadr '((x1 x2) (y1 y2))) +;: +;: (pair? (car '(a short list))) +;: +;: (memq 'red '((red shoes) (blue socks))) +;: +;: (memq 'red '(red shoes blue socks)) + + +;; EXERCISE 2.54 +;: (equal? '(this is a list) '(this is a list)) +;: (equal? '(this is a list) '(this (is a) list)) + +;; EXERCISE 2.55 +;: (car ''abracadabra) + + +;;;SECTION 2.3.2 + +(define (deriv exp var) + (cond ((number? exp) 0) + ((variable? exp) + (if (same-variable? exp var) 1 0)) + ((sum? exp) + (make-sum (deriv (addend exp) var) + (deriv (augend exp) var))) + ((product? exp) + (make-sum + (make-product (multiplier exp) + (deriv (multiplicand exp) var)) + (make-product (deriv (multiplier exp) var) + (multiplicand exp)))) + (else + (error "unknown expression type -- DERIV" exp)))) + +;; representing algebraic expressions + +(define (variable? x) (symbol? x)) + +(define (same-variable? v1 v2) + (and (variable? v1) (variable? v2) (eq? v1 v2))) + +(define (make-sum a1 a2) (list '+ a1 a2)) + +(define (make-product m1 m2) (list '* m1 m2)) + +(define (sum? x) + (and (pair? x) (eq? (car x) '+))) + +(define (addend s) (cadr s)) + +(define (augend s) (caddr s)) + +(define (product? x) + (and (pair? x) (eq? (car x) '*))) + +(define (multiplier p) (cadr p)) + +(define (multiplicand p) (caddr p)) + + +;: (deriv '(+ x 3) 'x) +;: (deriv '(* x y) 'x) +;: (deriv '(* (* x y) (+ x 3)) 'x) + + +;; With simplification + +(define (make-sum a1 a2) + (cond ((=number? a1 0) a2) + ((=number? a2 0) a1) + ((and (number? a1) (number? a2)) (+ a1 a2)) + (else (list '+ a1 a2)))) + +(define (=number? exp num) + (and (number? exp) (= exp num))) + +(define (make-product m1 m2) + (cond ((or (=number? m1 0) (=number? m2 0)) 0) + ((=number? m1 1) m2) + ((=number? m2 1) m1) + ((and (number? m1) (number? m2)) (* m1 m2)) + (else (list '* m1 m2)))) + + +;: (deriv '(+ x 3) 'x) +;: (deriv '(* x y) 'x) +;: (deriv '(* (* x y) (+ x 3)) 'x) + + +;; EXERCISE 2.57 +;: (deriv '(* x y (+ x 3)) 'x) + + +;;;SECTION 2.3.3 + +;; UNORDERED + +(define (element-of-set? x set) + (cond ((null? set) false) + ((equal? x (car set)) true) + (else (element-of-set? x (cdr set))))) + +(define (adjoin-set x set) + (if (element-of-set? x set) + set + (cons x set))) + +(define (intersection-set set1 set2) + (cond ((or (null? set1) (null? set2)) '()) + ((element-of-set? (car set1) set2) + (cons (car set1) + (intersection-set (cdr set1) set2))) + (else (intersection-set (cdr set1) set2)))) + + +;; ORDERED + +(define (element-of-set? x set) + (cond ((null? set) false) + ((= x (car set)) true) + ((< x (car set)) false) + (else (element-of-set? x (cdr set))))) + +(define (intersection-set set1 set2) + (if (or (null? set1) (null? set2)) + '() + (let ((x1 (car set1)) (x2 (car set2))) + (cond ((= x1 x2) + (cons x1 + (intersection-set (cdr set1) + (cdr set2)))) + ((< x1 x2) + (intersection-set (cdr set1) set2)) + ((< x2 x1) + (intersection-set set1 (cdr set2))))))) + +;; BINARY TREES +(define (entry tree) (car tree)) + +(define (left-branch tree) (cadr tree)) + +(define (right-branch tree) (caddr tree)) + +(define (make-tree entry left right) + (list entry left right)) + +(define (element-of-set? x set) + (cond ((null? set) false) + ((= x (entry set)) true) + ((< x (entry set)) + (element-of-set? x (left-branch set))) + ((> x (entry set)) + (element-of-set? x (right-branch set))))) + +(define (adjoin-set x set) + (cond ((null? set) (make-tree x '() '())) + ((= x (entry set)) set) + ((< x (entry set)) + (make-tree (entry set) + (adjoin-set x (left-branch set)) + (right-branch set))) + ((> x (entry set)) + (make-tree (entry set) + (left-branch set) + (adjoin-set x (right-branch set)))))) + + +;; EXERCISE 2.63 +(define (tree->list-1 tree) + (if (null? tree) + '() + (append (tree->list-1 (left-branch tree)) + (cons (entry tree) + (tree->list-1 (right-branch tree)))))) + +(define (tree->list-2 tree) + (define (copy-to-list tree result-list) + (if (null? tree) + result-list + (copy-to-list (left-branch tree) + (cons (entry tree) + (copy-to-list (right-branch tree) + result-list))))) + (copy-to-list tree '())) + + +;; EXERCISE 2.64 + +(define (list->tree elements) + (car (partial-tree elements (length elements)))) + +(define (partial-tree elts n) + (if (= n 0) + (cons '() elts) + (let ((left-size (quotient (- n 1) 2))) + (let ((left-result (partial-tree elts left-size))) + (let ((left-tree (car left-result)) + (non-left-elts (cdr left-result)) + (right-size (- n (+ left-size 1)))) + (let ((this-entry (car non-left-elts)) + (right-result (partial-tree (cdr non-left-elts) + right-size))) + (let ((right-tree (car right-result)) + (remaining-elts (cdr right-result))) + (cons (make-tree this-entry left-tree right-tree) + remaining-elts)))))))) + +;; INFORMATION RETRIEVAL + +(define (lookup given-key set-of-records) + (cond ((null? set-of-records) false) + ((equal? given-key (key (car set-of-records))) + (car set-of-records)) + (else (lookup given-key (cdr set-of-records))))) + + +;;;SECTION 2.3.3 + +;; representing + +(define (make-leaf symbol weight) + (list 'leaf symbol weight)) + +(define (leaf? object) + (eq? (car object) 'leaf)) + +(define (symbol-leaf x) (cadr x)) + +(define (weight-leaf x) (caddr x)) + +(define (make-code-tree left right) + (list left + right + (append (symbols left) (symbols right)) + (+ (weight left) (weight right)))) + +(define (left-branch tree) (car tree)) + +(define (right-branch tree) (cadr tree)) + +(define (symbols tree) + (if (leaf? tree) + (list (symbol-leaf tree)) + (caddr tree))) + +(define (weight tree) + (if (leaf? tree) + (weight-leaf tree) + (cadddr tree))) + +;; decoding +(define (decode bits tree) + (define (decode-1 bits current-branch) + (if (null? bits) + '() + (let ((next-branch + (choose-branch (car bits) current-branch))) + (if (leaf? next-branch) + (cons (symbol-leaf next-branch) + (decode-1 (cdr bits) tree)) + (decode-1 (cdr bits) next-branch))))) + (decode-1 bits tree)) + +(define (choose-branch bit branch) + (cond ((= bit 0) (left-branch branch)) + ((= bit 1) (right-branch branch)) + (else (error "bad bit -- CHOOSE-BRANCH" bit)))) + +;; sets + +(define (adjoin-set x set) + (cond ((null? set) (list x)) + ((< (weight x) (weight (car set))) (cons x set)) + (else (cons (car set) + (adjoin-set x (cdr set)))))) + +(define (make-leaf-set pairs) + (if (null? pairs) + '() + (let ((pair (car pairs))) + (adjoin-set (make-leaf (car pair) + (cadr pair)) + (make-leaf-set (cdr pairs)))))) + + +;; EXERCISE 2.67 + +;: (define sample-tree +;: (make-code-tree (make-leaf 'A 4) +;: (make-code-tree +;: (make-leaf 'B 2) +;: (make-code-tree (make-leaf 'D 1) +;: (make-leaf 'C 1))))) + +;: (define sample-message '(0 1 1 0 0 1 0 1 0 1 1 1 0)) + + +;; EXERCISE 2.68 + +(define (encode message tree) + (if (null? message) + '() + (append (encode-symbol (car message) tree) + (encode (cdr message) tree)))) + +;; EXERCISE 2.69 + +(define (generate-huffman-tree pairs) + (successive-merge (make-leaf-set pairs))) + +;;;SECTION 2.4.1 + +;: (make-from-real-imag (real-part z) (imag-part z)) + +;: (make-from-mag-ang (magnitude z) (angle z)) + +(define (add-complex z1 z2) + (make-from-real-imag (+ (real-part z1) (real-part z2)) + (+ (imag-part z1) (imag-part z2)))) + +(define (sub-complex z1 z2) + (make-from-real-imag (- (real-part z1) (real-part z2)) + (- (imag-part z1) (imag-part z2)))) + +(define (mul-complex z1 z2) + (make-from-mag-ang (* (magnitude z1) (magnitude z2)) + (+ (angle z1) (angle z2)))) + +(define (div-complex z1 z2) + (make-from-mag-ang (/ (magnitude z1) (magnitude z2)) + (- (angle z1) (angle z2)))) + + +;; Ben (rectangular) + +(define (real-part z) (car z)) + +(define (imag-part z) (cdr z)) + +(define (magnitude z) + (sqrt (+ (square (real-part z)) (square (imag-part z))))) + +(define (angle z) + (atan (imag-part z) (real-part z))) + +(define (make-from-real-imag x y) (cons x y)) + +(define (make-from-mag-ang r a) + (cons (* r (cos a)) (* r (sin a)))) + + +;; Alyssa (polar) + +(define (real-part z) + (* (magnitude z) (cos (angle z)))) + +(define (imag-part z) + (* (magnitude z) (sin (angle z)))) + +(define (magnitude z) (car z)) + +(define (angle z) (cdr z)) + +(define (make-from-real-imag x y) + (cons (sqrt (+ (square x) (square y))) + (atan y x))) + +(define (make-from-mag-ang r a) (cons r a)) + + +;;;SECTION 2.4.2 + +(define (attach-tag type-tag contents) + (cons type-tag contents)) + +(define (type-tag datum) + (if (pair? datum) + (car datum) + (error "Bad tagged datum -- TYPE-TAG" datum))) + +(define (contents datum) + (if (pair? datum) + (cdr datum) + (error "Bad tagged datum -- CONTENTS" datum))) + +(define (rectangular? z) + (eq? (type-tag z) 'rectangular)) + +(define (polar? z) + (eq? (type-tag z) 'polar)) + + +;; Ben (rectangular) + +(define (real-part-rectangular z) (car z)) + +(define (imag-part-rectangular z) (cdr z)) + +(define (magnitude-rectangular z) + (sqrt (+ (square (real-part-rectangular z)) + (square (imag-part-rectangular z))))) + +(define (angle-rectangular z) + (atan (imag-part-rectangular z) + (real-part-rectangular z))) + +(define (make-from-real-imag-rectangular x y) + (attach-tag 'rectangular (cons x y))) + +(define (make-from-mag-ang-rectangular r a) + (attach-tag 'rectangular + (cons (* r (cos a)) (* r (sin a))))) + +;; Alyssa (polar) + +(define (real-part-polar z) + (* (magnitude-polar z) (cos (angle-polar z)))) + +(define (imag-part-polar z) + (* (magnitude-polar z) (sin (angle-polar z)))) + +(define (magnitude-polar z) (car z)) + +(define (angle-polar z) (cdr z)) + +(define (make-from-real-imag-polar x y) + (attach-tag 'polar + (cons (sqrt (+ (square x) (square y))) + (atan y x)))) + +(define (make-from-mag-ang-polar r a) + (attach-tag 'polar (cons r a))) + + +;; Generic selectors + +(define (real-part z) + (cond ((rectangular? z) + (real-part-rectangular (contents z))) + ((polar? z) + (real-part-polar (contents z))) + (else (error "Unknown type -- REAL-PART" z)))) + +(define (imag-part z) + (cond ((rectangular? z) + (imag-part-rectangular (contents z))) + ((polar? z) + (imag-part-polar (contents z))) + (else (error "Unknown type -- IMAG-PART" z)))) + +(define (magnitude z) + (cond ((rectangular? z) + (magnitude-rectangular (contents z))) + ((polar? z) + (magnitude-polar (contents z))) + (else (error "Unknown type -- MAGNITUDE" z)))) + +(define (angle z) + (cond ((rectangular? z) + (angle-rectangular (contents z))) + ((polar? z) + (angle-polar (contents z))) + (else (error "Unknown type -- ANGLE" z)))) + +;; same as before +(define (add-complex z1 z2) + (make-from-real-imag (+ (real-part z1) (real-part z2)) + (+ (imag-part z1) (imag-part z2)))) + +;; Constructors for complex numbers + +(define (make-from-real-imag x y) + (make-from-real-imag-rectangular x y)) + +(define (make-from-mag-ang r a) + (make-from-mag-ang-polar r a)) + +;;;SECTION 2.4.3 + +;; uses get/put (from 3.3.3) -- see ch2support.scm + +(define (install-rectangular-package) + ;; internal procedures + (define (real-part z) (car z)) + (define (imag-part z) (cdr z)) + (define (make-from-real-imag x y) (cons x y)) + (define (magnitude z) + (sqrt (+ (square (real-part z)) + (square (imag-part z))))) + (define (angle z) + (atan (imag-part z) (real-part z))) + (define (make-from-mag-ang r a) + (cons (* r (cos a)) (* r (sin a)))) + + ;; interface to the rest of the system + (define (tag x) (attach-tag 'rectangular x)) + (put 'real-part '(rectangular) real-part) + (put 'imag-part '(rectangular) imag-part) + (put 'magnitude '(rectangular) magnitude) + (put 'angle '(rectangular) angle) + (put 'make-from-real-imag 'rectangular + (lambda (x y) (tag (make-from-real-imag x y)))) + (put 'make-from-mag-ang 'rectangular + (lambda (r a) (tag (make-from-mag-ang r a)))) + 'done) + +(define (install-polar-package) + ;; internal procedures + (define (magnitude z) (car z)) + (define (angle z) (cdr z)) + (define (make-from-mag-ang r a) (cons r a)) + (define (real-part z) + (* (magnitude z) (cos (angle z)))) + (define (imag-part z) + (* (magnitude z) (sin (angle z)))) + (define (make-from-real-imag x y) + (cons (sqrt (+ (square x) (square y))) + (atan y x))) + + ;; interface to the rest of the system + (define (tag x) (attach-tag 'polar x)) + (put 'real-part '(polar) real-part) + (put 'imag-part '(polar) imag-part) + (put 'magnitude '(polar) magnitude) + (put 'angle '(polar) angle) + (put 'make-from-real-imag 'polar + (lambda (x y) (tag (make-from-real-imag x y)))) + (put 'make-from-mag-ang 'polar + (lambda (r a) (tag (make-from-mag-ang r a)))) + 'done) + +;;footnote +;: (apply + (list 1 2 3 4)) + + +(define (apply-generic op . args) + (let ((type-tags (map type-tag args))) + (let ((proc (get op type-tags))) + (if proc + (apply proc (map contents args)) + (error + "No method for these types -- APPLY-GENERIC" + (list op type-tags)))))) + +;; Generic selectors + +(define (real-part z) (apply-generic 'real-part z)) +(define (imag-part z) (apply-generic 'imag-part z)) +(define (magnitude z) (apply-generic 'magnitude z)) +(define (angle z) (apply-generic 'angle z)) + + +;; Constructors for complex numbers + +(define (make-from-real-imag x y) + ((get 'make-from-real-imag 'rectangular) x y)) + +(define (make-from-mag-ang r a) + ((get 'make-from-mag-ang 'polar) r a)) + + + +;; EXERCISE 2.73 +(define (deriv exp var) + (cond ((number? exp) 0) + ((variable? exp) (if (same-variable? exp var) 1 0)) + ((sum? exp) + (make-sum (deriv (addend exp) var) + (deriv (augend exp) var))) + ((product? exp) + (make-sum + (make-product (multiplier exp) + (deriv (multiplicand exp) var)) + (make-product (deriv (multiplier exp) var) + (multiplicand exp)))) + (else (error "unknown expression type -- DERIV" exp)))) + + +(define (deriv exp var) + (cond ((number? exp) 0) + ((variable? exp) (if (same-variable? exp var) 1 0)) + (else ((get 'deriv (operator exp)) (operands exp) + var)))) + +(define (operator exp) (car exp)) + +(define (operands exp) (cdr exp)) + +;: ((get (operator exp) 'deriv) (operands exp) var) + + +;; Message passing +(define (make-from-real-imag x y) + (define (dispatch op) + (cond ((eq? op 'real-part) x) + ((eq? op 'imag-part) y) + ((eq? op 'magnitude) + (sqrt (+ (square x) (square y)))) + ((eq? op 'angle) (atan y x)) + (else + (error "Unknown op -- MAKE-FROM-REAL-IMAG" op)))) + dispatch) + +(define (apply-generic op arg) (arg op)) + +;;;SECTION 2.5.1 + +(define (add x y) (apply-generic 'add x y)) +(define (sub x y) (apply-generic 'sub x y)) +(define (mul x y) (apply-generic 'mul x y)) +(define (div x y) (apply-generic 'div x y)) + +(define (install-scheme-number-package) + (define (tag x) + (attach-tag 'scheme-number x)) + (put 'add '(scheme-number scheme-number) + (lambda (x y) (tag (+ x y)))) + (put 'sub '(scheme-number scheme-number) + (lambda (x y) (tag (- x y)))) + (put 'mul '(scheme-number scheme-number) + (lambda (x y) (tag (* x y)))) + (put 'div '(scheme-number scheme-number) + (lambda (x y) (tag (/ x y)))) + (put 'make 'scheme-number + (lambda (x) (tag x))) + 'done) + +(define (make-scheme-number n) + ((get 'make 'scheme-number) n)) + +(define (install-rational-package) + ;; internal procedures + (define (numer x) (car x)) + (define (denom x) (cdr x)) + (define (make-rat n d) + (let ((g (gcd n d))) + (cons (/ n g) (/ d g)))) + (define (add-rat x y) + (make-rat (+ (* (numer x) (denom y)) + (* (numer y) (denom x))) + (* (denom x) (denom y)))) + (define (sub-rat x y) + (make-rat (- (* (numer x) (denom y)) + (* (numer y) (denom x))) + (* (denom x) (denom y)))) + (define (mul-rat x y) + (make-rat (* (numer x) (numer y)) + (* (denom x) (denom y)))) + (define (div-rat x y) + (make-rat (* (numer x) (denom y)) + (* (denom x) (numer y)))) + ;; interface to rest of the system + (define (tag x) (attach-tag 'rational x)) + (put 'add '(rational rational) + (lambda (x y) (tag (add-rat x y)))) + (put 'sub '(rational rational) + (lambda (x y) (tag (sub-rat x y)))) + (put 'mul '(rational rational) + (lambda (x y) (tag (mul-rat x y)))) + (put 'div '(rational rational) + (lambda (x y) (tag (div-rat x y)))) + + (put 'make 'rational + (lambda (n d) (tag (make-rat n d)))) + 'done) + +(define (make-rational n d) + ((get 'make 'rational) n d)) + +(define (install-complex-package) + ;; imported procedures from rectangular and polar packages + (define (make-from-real-imag x y) + ((get 'make-from-real-imag 'rectangular) x y)) + (define (make-from-mag-ang r a) + ((get 'make-from-mag-ang 'polar) r a)) + ;; internal procedures + (define (add-complex z1 z2) + (make-from-real-imag (+ (real-part z1) (real-part z2)) + (+ (imag-part z1) (imag-part z2)))) + (define (sub-complex z1 z2) + (make-from-real-imag (- (real-part z1) (real-part z2)) + (- (imag-part z1) (imag-part z2)))) + (define (mul-complex z1 z2) + (make-from-mag-ang (* (magnitude z1) (magnitude z2)) + (+ (angle z1) (angle z2)))) + (define (div-complex z1 z2) + (make-from-mag-ang (/ (magnitude z1) (magnitude z2)) + (- (angle z1) (angle z2)))) + + ;; interface to rest of the system + (define (tag z) (attach-tag 'complex z)) + (put 'add '(complex complex) + (lambda (z1 z2) (tag (add-complex z1 z2)))) + (put 'sub '(complex complex) + (lambda (z1 z2) (tag (sub-complex z1 z2)))) + (put 'mul '(complex complex) + (lambda (z1 z2) (tag (mul-complex z1 z2)))) + (put 'div '(complex complex) + (lambda (z1 z2) (tag (div-complex z1 z2)))) + (put 'make-from-real-imag 'complex + (lambda (x y) (tag (make-from-real-imag x y)))) + (put 'make-from-mag-ang 'complex + (lambda (r a) (tag (make-from-mag-ang r a)))) + 'done) + +(define (make-complex-from-real-imag x y) + ((get 'make-from-real-imag 'complex) x y)) + +(define (make-complex-from-mag-ang r a) + ((get 'make-from-mag-ang 'complex) r a)) + + +;; EXERCISE 2.77 +;; to put in complex package + +;: (put 'real-part '(complex) real-part) +;: (put 'imag-part '(complex) imag-part) +;: (put 'magnitude '(complex) magnitude) +;: (put 'angle '(complex) angle) + + +;;;SECTION 2.5.2 + +;; to be included in the complex package +;: (define (add-complex-to-schemenum z x) +;: (make-from-real-imag (+ (real-part z) x) +;: (imag-part z))) +;: +;: (put 'add '(complex scheme-number) +;: (lambda (z x) (tag (add-complex-to-schemenum z x)))) + + +;; Coercion + +(define (scheme-number->complex n) + (make-complex-from-real-imag (contents n) 0)) + +;: (put-coercion 'scheme-number 'complex scheme-number->complex) + + +(define (apply-generic op . args) + (let ((type-tags (map type-tag args))) + (let ((proc (get op type-tags))) + (if proc + (apply proc (map contents args)) + (if (= (length args) 2) + (let ((type1 (car type-tags)) + (type2 (cadr type-tags)) + (a1 (car args)) + (a2 (cadr args))) + (let ((t1->t2 (get-coercion type1 type2)) + (t2->t1 (get-coercion type2 type1))) + (cond (t1->t2 + (apply-generic op (t1->t2 a1) a2)) + (t2->t1 + (apply-generic op a1 (t2->t1 a2))) + (else + (error "No method for these types" + (list op type-tags)))))) + (error "No method for these types" + (list op type-tags))))))) + +;; EXERCISE 2.81 + +(define (scheme-number->scheme-number n) n) +(define (complex->complex z) z) +;: (put-coercion 'scheme-number 'scheme-number +;: scheme-number->scheme-number) +;: (put-coercion 'complex 'complex complex->complex) + +(define (exp x y) (apply-generic 'exp x y)) +;: (put 'exp '(scheme-number scheme-number) +;: (lambda (x y) (tag (expt x y)))) + +;;;SECTION 2.5.3 + +;;; ALL procedures in 2.5.3 except make-polynomial +;;; should be inserted in install-polynomial-package, as indicated + +(define (add-poly p1 p2) + (if (same-variable? (variable p1) (variable p2)) + (make-poly (variable p1) + (add-terms (term-list p1) + (term-list p2))) + (error "Polys not in same var -- ADD-POLY" + (list p1 p2)))) + +(define (mul-poly p1 p2) + (if (same-variable? (variable p1) (variable p2)) + (make-poly (variable p1) + (mul-terms (term-list p1) + (term-list p2))) + (error "Polys not in same var -- MUL-POLY" + (list p1 p2)))) + +;; *incomplete* skeleton of package +(define (install-polynomial-package) + ;; internal procedures + ;; representation of poly + (define (make-poly variable term-list) + (cons variable term-list)) + (define (variable p) (car p)) + (define (term-list p) (cdr p)) + ;;[procedures same-variable? and variable? from section 2.3.2] + + ;; representation of terms and term lists + ;;[procedures adjoin-term ... coeff from text below] + + ;;(define (add-poly p1 p2) ... ) + ;;[procedures used by add-poly] + + ;;(define (mul-poly p1 p2) ... ) + ;;[procedures used by mul-poly] + + ;; interface to rest of the system + (define (tag p) (attach-tag 'polynomial p)) + (put 'add '(polynomial polynomial) + (lambda (p1 p2) (tag (add-poly p1 p2)))) + (put 'mul '(polynomial polynomial) + (lambda (p1 p2) (tag (mul-poly p1 p2)))) + + (put 'make 'polynomial + (lambda (var terms) (tag (make-poly var terms)))) + 'done) + +(define (add-terms L1 L2) + (cond ((empty-termlist? L1) L2) + ((empty-termlist? L2) L1) + (else + (let ((t1 (first-term L1)) (t2 (first-term L2))) + (cond ((> (order t1) (order t2)) + (adjoin-term + t1 (add-terms (rest-terms L1) L2))) + ((< (order t1) (order t2)) + (adjoin-term + t2 (add-terms L1 (rest-terms L2)))) + (else + (adjoin-term + (make-term (order t1) + (add (coeff t1) (coeff t2))) + (add-terms (rest-terms L1) + (rest-terms L2))))))))) + +(define (mul-terms L1 L2) + (if (empty-termlist? L1) + (the-empty-termlist) + (add-terms (mul-term-by-all-terms (first-term L1) L2) + (mul-terms (rest-terms L1) L2)))) + +(define (mul-term-by-all-terms t1 L) + (if (empty-termlist? L) + (the-empty-termlist) + (let ((t2 (first-term L))) + (adjoin-term + (make-term (+ (order t1) (order t2)) + (mul (coeff t1) (coeff t2))) + (mul-term-by-all-terms t1 (rest-terms L)))))) + + +;; Representing term lists + +(define (adjoin-term term term-list) + (if (=zero? (coeff term)) + term-list + (cons term term-list))) + +(define (the-empty-termlist) '()) +(define (first-term term-list) (car term-list)) +(define (rest-terms term-list) (cdr term-list)) +(define (empty-termlist? term-list) (null? term-list)) + +(define (make-term order coeff) (list order coeff)) +(define (order term) (car term)) +(define (coeff term) (cadr term)) + + +;; Constructor +(define (make-polynomial var terms) + ((get 'make 'polynomial) var terms)) + + +;; EXERCISE 2.91 + +(define (div-terms L1 L2) + (if (empty-termlist? L1) + (list (the-empty-termlist) (the-empty-termlist)) + (let ((t1 (first-term L1)) + (t2 (first-term L2))) + (if (> (order t2) (order t1)) + (list (the-empty-termlist) L1) + (let ((new-c (div (coeff t1) (coeff t2))) + (new-o (- (order t1) (order t2)))) + (let ((rest-of-result + ??FILL-THIS-IN?? ;compute rest of result recursively + )) + ??FILL-THIS-IN?? ;form complete result + )))))) + + +;; EXERCISE 2.93 +;: (define p1 (make-polynomial 'x '((2 1)(0 1)))) +;: (define p2 (make-polynomial 'x '((3 1)(0 1)))) +;: (define rf (make-rational p2 p1)) + + +;; Rational functions + +(define (gcd a b) + (if (= b 0) + a + (gcd b (remainder a b)))) + +(define (gcd-terms a b) + (if (empty-termlist? b) + a + (gcd-terms b (remainder-terms a b)))) + + +;; EXERCISE 2.94 +;: (define p1 (make-polynomial 'x '((4 1) (3 -1) (2 -2) (1 2)))) +;: (define p2 (make-polynomial 'x '((3 1) (1 -1)))) +;: (greatest-common-divisor p1 p2) + + +;; EXERCISE 2.97 + +(define (reduce-integers n d) + (let ((g (gcd n d))) + (list (/ n g) (/ d g)))) + +;: (define p1 (make-polynomial 'x '((1 1)(0 1)))) +;: (define p2 (make-polynomial 'x '((3 1)(0 -1)))) +;: (define p3 (make-polynomial 'x '((1 1)))) +;: (define p4 (make-polynomial 'x '((2 1)(0 -1)))) + +;: (define rf1 (make-rational p1 p2)) +;: (define rf2 (make-rational p3 p4)) + +;: (add rf1 rf2) + +; -----***CH2TESTS.SCM*** +;;; EXAMPLES OF TESTING CODE (IN MIT SCHEME) +;;; FROM CHAPTER 2 OF STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS + + +;;;SECTION 2.4.1 + +;; Ben's rectangular + +(define z1 (make-from-real-imag 1 1)) +;Value: z1 + +(real-part z1) +;Value: 1 +(imag-part z1) +;Value: 1 +(magnitude z1) +;Value: 1.4142135623730951 +(angle z1) +;Value: .7853981633974483 +(* 4 (angle z1)) +;Value: 3.141592653589793 +(define z2 (make-from-mag-ang 1.4142135623730951 .7853981633974483)) +;Value: z2 + +(real-part z2) +;Value: 1. +(imag-part z2) +;Value: 1. + +z1 +;Value 10: (1 . 1) + +z2 +;Value 14: (1. . 1.) + +(add-complex z1 z2) +;Value 16: (2. . 2.) + +(sub-complex z1 z2) +;Value 17: (0. . 0.) + + +;; Alyssa's polar + +(define z1 (make-from-real-imag 1 1)) +;Value: z1 + +(real-part z1) +;Value: 1. + +(imag-part z1) +;Value: 1. + +(magnitude z1) +;Value: 1.4142135623730951 + +(angle z1) +;Value: .7853981633974483 + +(* 4 (angle z1)) +;Value: 3.141592653589793 + +(define z2 (make-from-mag-ang 1.4142135623730951 .7853981633974483)) +;Value: z2 + +(real-part z2) +;Value: 1. + +(imag-part z2) +;Value: 1. + +z1 +;Value 12: (1.4142135623730951 . .7853981633974483) + +z2 +;Value 13: (1.4142135623730951 . .7853981633974483) + +(mul-complex z1 z2) +;Value 18: (2.0000000000000004 . 1.5707963267948966) + +(div-complex z1 z2) +;Value 19: (1. . 0.) + +;;;SECTION 2.4.2 + +(define z1 (make-from-real-imag 1 1)) +;Value: z1 + +z1 +;Value 20: (rectangular 1 . 1) +(real-part z1) +;Value: 1 +(imag-part z1) +;Value: 1 +(magnitude z1) +;Value: 1.4142135623730951 +(angle z1) +;Value: .7853981633974483 + +(define z2 (make-from-mag-ang 1.4142135623730951 .7853981633974483)) +;Value: z2 + +z2 +;Value 22: (polar 1.4142135623730951 . .7853981633974483) + +(magnitude z2) +;Value: 1.4142135623730951 +(angle z2) +;Value: .7853981633974483 +(real-part z2) +;Value: 1. +(imag-part z2) +;Value: 1. + +z1 +;Value 20: (rectangular 1 . 1) +z2 +;Value 22: (polar 1.4142135623730951 . .7853981633974483) + +(add-complex z1 z2) +;Value 23: (rectangular 2. . 2.) +(sub-complex z1 z2) +;Value 24: (rectangular 0. . 0.) +(mul-complex z1 z2) +;Value 25: (polar 2.0000000000000004 . 1.5707963267948966) +(div-complex z1 z2) +;Value 26: (polar 1. . 0.) + +;;;SECTION 2.5.2 + +(define z1 (make-complex-from-real-imag 1 1)) + +;; Before coercion mechanism + +(add z1 (make-scheme-number 3)) +;Value 1: (complex rectangular 4 . 1) + +(add (make-scheme-number 3) z1) +;No method for the given types (add (scheme-number complex)) + + +;; With coercion mechanism + +(add z1 (make-scheme-number 3)) +;Value 6: (complex rectangular 4 . 1) + +(add (make-scheme-number 3) z1) +;Value 7: (complex rectangular 4 . 1) + +;;;SECTION 2.5.3 + +(define a (make-polynomial 'x '((5 1) (4 2) (2 3) (1 -2) (0 -5)))) + +a +;Value 3: (polynomial x (5 1) (4 2) (2 3) (1 -2) (0 -5)) + +(add a a) +;Value 4: (polynomial x (5 2) (4 4) (2 6) (1 -4) (0 -10)) + +(define b (make-polynomial 'x '((100 1) (2 2) (0 1)))) + +b +;Value 5: (polynomial x (100 1) (2 2) (0 1)) + +(mul b b) +;Value 6: (polynomial x (200 1) (102 4) (100 2) (4 4) (2 4) (0 1)) + + diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/chapter3.code b/js/games/nluqo.github.io/~bh/61a-pages/Lib/chapter3.code new file mode 100644 index 0000000..656fff4 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/chapter3.code @@ -0,0 +1,1707 @@ +;;;;CODE FROM CHAPTER 3 OF STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS + +;;; Examples from the book are commented out with ;: so that they +;;; are easy to find and so that they will be omitted if you evaluate a +;;; chunk of the file (programs with intervening examples) in Scheme. + +;;; BEWARE: Although the whole file can be loaded into Scheme, +;;; you won't want to do so. For example, you generally do +;;; not want to use the procedural representation of pairs +;;; (cons, car, cdr as defined in section 3.3.1) instead of +;;; Scheme's primitive pairs. + +;;; Some things require code that is not in the book -- see ch3support.scm + + +;;;;SECTION 3.1 + +;;;SECTION 3.1.1 + +;: (withdraw 25) +;: (withdraw 25) +;: (withdraw 60) +;: (withdraw 15) + +(define balance 100) + +(define (withdraw amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")) + + +(define new-withdraw + (let ((balance 100)) + (lambda (amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")))) + + +(define (make-withdraw balance) + (lambda (amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds"))) + + +;: (define W1 (make-withdraw 100)) +;: (define W2 (make-withdraw 100)) +;: (W1 50) +;: (W2 70) +;: (W2 40) +;: (W1 40) + + +(define (make-account balance) + (define (withdraw amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")) + (define (deposit amount) + (set! balance (+ balance amount)) + balance) + (define (dispatch m) + (cond ((eq? m 'withdraw) withdraw) + ((eq? m 'deposit) deposit) + (else (error "Unknown request -- MAKE-ACCOUNT" + m)))) + dispatch) + +;: (define acc (make-account 100)) + +;: ((acc 'withdraw) 50) +;: ((acc 'withdraw) 60) +;: ((acc 'deposit) 40) +;: ((acc 'withdraw) 60) + +;: (define acc2 (make-account 100)) + + +;; EXERCISE 3.1 +;: (define A (make-accumulator 5)) +;: (A 10) +;: (A 10) + + +;; EXERCISE 3.2 +;: (define s (make-monitored sqrt)) +;: (s 100) +;: (s 'how-many-calls?) + + +;; EXERCISE 3.3 +;: (define acc (make-account 100 'secret-password)) +;: ((acc 'secret-password 'withdraw) 40) +;: ((acc 'some-other-password 'deposit) 50) + + +;;;SECTION 3.1.2 + +;; *following uses rand-update -- see ch3support.scm +;; *also must set random-init to some value +(define random-init 7) ;**not in book** +(define rand + (let ((x random-init)) + (lambda () + (set! x (rand-update x)) + x))) + + +(define (estimate-pi trials) + (sqrt (/ 6 (monte-carlo trials cesaro-test)))) + +(define (cesaro-test) + (= (gcd (rand) (rand)) 1)) + +(define (monte-carlo trials experiment) + (define (iter trials-remaining trials-passed) + (cond ((= trials-remaining 0) + (/ trials-passed trials)) + ((experiment) + (iter (- trials-remaining 1) (+ trials-passed 1))) + (else + (iter (- trials-remaining 1) trials-passed)))) + (iter trials 0)) + +;; second version (no assignment) +(define (estimate-pi trials) + (sqrt (/ 6 (random-gcd-test trials random-init)))) + +(define (random-gcd-test trials initial-x) + (define (iter trials-remaining trials-passed x) + (let ((x1 (rand-update x))) + (let ((x2 (rand-update x1))) + (cond ((= trials-remaining 0) + (/ trials-passed trials)) + ((= (gcd x1 x2) 1) + (iter (- trials-remaining 1) + (+ trials-passed 1) + x2)) + (else + (iter (- trials-remaining 1) + trials-passed + x2)))))) + (iter trials 0 initial-x)) + + +;; EXERCISE 3.5 +(define (random-in-range low high) + (let ((range (- high low))) + (+ low (random range)))) + + +;;;SECTION 3.1.3 + +(define (make-simplified-withdraw balance) + (lambda (amount) + (set! balance (- balance amount)) + balance)) + + +;: (define W (make-simplified-withdraw 25)) +;: (W 20) +;: (W 10) + + +(define (make-decrementer balance) + (lambda (amount) + (- balance amount))) + +;: (define D (make-decrementer 25)) +;: (D 20) +;: (D 10) + +;: ((make-decrementer 25) 20) +;: ((lambda (amount) (- 25 amount)) 20) +;: (- 25 20) + +;: ((make-simplified-withdraw 25) 20) + +;: ((lambda (amount) (set! balance (- 25 amount)) 25) 20) +;: (set! balance (- 25 20)) 25 + +;;;Sameness and change + +;: (define D1 (make-decrementer 25)) +;: (define D2 (make-decrementer 25)) +;: +;: (define W1 (make-simplified-withdraw 25)) +;: (define W2 (make-simplified-withdraw 25)) +;: +;: (W1 20) +;: (W1 20) +;: (W2 20) + +;: (define peter-acc (make-account 100)) +;: (define paul-acc (make-account 100)) +;: +;: (define peter-acc (make-account 100)) +;: (define paul-acc peter-acc) + +;;;Pitfalls of imperative programming + +(define (factorial n) + (define (iter product counter) + (if (> counter n) + product + (iter (* counter product) + (+ counter 1)))) + (iter 1 1)) + +(define (factorial n) + (let ((product 1) + (counter 1)) + (define (iter) + (if (> counter n) + product + (begin (set! product (* counter product)) + (set! counter (+ counter 1)) + (iter)))) + (iter))) + + +;; EXERCISE 3.7 +;: (define paul-acc +;: (make-joint peter-acc 'open-sesame 'rosebud)) + + +;;;;SECTION 3.2 + +;;;SECTION 3.2.1 + +(define (square x) + (* x x)) + +(define square + (lambda (x) (* x x))) + + +;;;SECTION 3.2.2 + +(define (square x) + (* x x)) + +(define (sum-of-squares x y) + (+ (square x) (square y))) + +(define (f a) + (sum-of-squares (+ a 1) (* a 2))) + +;: (sum-of-squares (+ a 1) (* a 2)) + + +;; EXERCISE 3.9 + +(define (factorial n) + (if (= n 1) + 1 + (* n (factorial (- n 1))))) + +(define (factorial n) + (fact-iter 1 1 n)) + +(define (fact-iter product counter max-count) + (if (> counter max-count) + product + (fact-iter (* counter product) + (+ counter 1) + max-count))) + + +;;;SECTION 3.2.3 + +(define (make-withdraw balance) + (lambda (amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds"))) + +;: (define W1 (make-withdraw 100)) +;: (W1 50) + +;: (define W2 (make-withdraw 100)) + + +;; EXERCISE 3.10 + +(define (make-withdraw initial-amount) + (let ((balance initial-amount)) + (lambda (amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")))) + + +;: (define W1 (make-withdraw 100)) +;: (W1 50) +;: (define W2 (make-withdraw 100)) + + +;;;SECTION 3.2.4 + +;; same as in section 1.1.8 +(define (sqrt x) + (define (good-enough? guess) + (< (abs (- (square guess) x)) 0.001)) + (define (improve guess) + (average guess (/ x guess))) + (define (sqrt-iter guess) + (if (good-enough? guess) + guess + (sqrt-iter (improve guess)))) + (sqrt-iter 1.0)) + + +;; EXERCISE 3.11 + +(define (make-account balance) + (define (withdraw amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")) + (define (deposit amount) + (set! balance (+ balance amount)) + balance) + (define (dispatch m) + (cond ((eq? m 'withdraw) withdraw) + ((eq? m 'deposit) deposit) + (else (error "Unknown request -- MAKE-ACCOUNT" + m)))) + dispatch) + +;: (define acc (make-account 50)) +;: +;: ((acc 'deposit) 40) +;: ((acc 'withdraw) 60) +;: +;: (define acc2 (make-account 100)) + + +;;;;SECTION 3.3 + +;;;SECTION 3.3.1 + +(define (cons x y) + (let ((new (get-new-pair))) + (set-car! new x) + (set-cdr! new y) + new)) + + +;; EXERCISE 3.12 +(define (append x y) + (if (null? x) + y + (cons (car x) (append (cdr x) y)))) + +(define (append! x y) + (set-cdr! (last-pair x) y) + x) + +(define (last-pair x) + (if (null? (cdr x)) + x + (last-pair (cdr x)))) + +;: (define x (list 'a 'b)) +;: (define y (list 'c 'd)) +;: (define z (append x y)) +;: z +;: (cdr x) +;: +;: (define w (append! x y)) +;: w +;: (cdr x) + + +;; EXERCISE 3.13 +(define (make-cycle x) + (set-cdr! (last-pair x) x) + x) + +;: (define z (make-cycle (list 'a 'b 'c))) + + +;; EXERCISE 3.14 +(define (mystery x) + (define (loop x y) + (if (null? x) + y + (let ((temp (cdr x))) + (set-cdr! x y) + (loop temp x)))) + (loop x '())) + + +;;; Sharing and identity + +;: (define x (list 'a 'b)) +;: (define z1 (cons x x)) +;: (define z2 (cons (list 'a 'b) (list 'a 'b))) + +(define (set-to-wow! x) + (set-car! (car x) 'wow) + x) + +;: z1 +;: (set-to-wow! z1) +;: z2 +;: (set-to-wow! z2) + + +;; EXERCISE 3.16 +(define (count-pairs x) + (if (not (pair? x)) + 0 + (+ (count-pairs (car x)) + (count-pairs (cdr x)) + 1))) + + +;;;Mutation as assignment + +(define (cons x y) + (define (dispatch m) + (cond ((eq? m 'car) x) + ((eq? m 'cdr) y) + (else (error "Undefined operation -- CONS" m)))) + dispatch) + +(define (car z) (z 'car)) +(define (cdr z) (z 'cdr)) + + +(define (cons x y) + (define (set-x! v) (set! x v)) + (define (set-y! v) (set! y v)) + (define (dispatch m) + (cond ((eq? m 'car) x) + ((eq? m 'cdr) y) + ((eq? m 'set-car!) set-x!) + ((eq? m 'set-cdr!) set-y!) + (else (error "Undefined operation -- CONS" m)))) + dispatch) + +(define (car z) (z 'car)) +(define (cdr z) (z 'cdr)) + +(define (set-car! z new-value) + ((z 'set-car!) new-value) + z) + +(define (set-cdr! z new-value) + ((z 'set-cdr!) new-value) + z) + + +;; EXERCISE 3.20 +;: (define x (cons 1 2)) +;: (define z (cons x x)) +;: (set-car! (cdr z) 17) +;: (car x) + + +;;;SECTION 3.3.2 + +(define (front-ptr queue) (car queue)) +(define (rear-ptr queue) (cdr queue)) +(define (set-front-ptr! queue item) (set-car! queue item)) +(define (set-rear-ptr! queue item) (set-cdr! queue item)) + +(define (empty-queue? queue) (null? (front-ptr queue))) +(define (make-queue) (cons '() '())) + +(define (front-queue queue) + (if (empty-queue? queue) + (error "FRONT called with an empty queue" queue) + (car (front-ptr queue)))) + +(define (insert-queue! queue item) + (let ((new-pair (cons item '()))) + (cond ((empty-queue? queue) + (set-front-ptr! queue new-pair) + (set-rear-ptr! queue new-pair) + queue) + (else + (set-cdr! (rear-ptr queue) new-pair) + (set-rear-ptr! queue new-pair) + queue)))) + +(define (delete-queue! queue) + (cond ((empty-queue? queue) + (error "DELETE! called with an empty queue" queue)) + (else + (set-front-ptr! queue (cdr (front-ptr queue))) + queue))) + + +;; EXERCISE 3.21 +;: (define q1 (make-queue)) +;: (insert-queue! q1 'a) +;: (insert-queue! q1 'b) +;: (delete-queue! q1) +;: (delete-queue! q1) + + +;;;SECTION 3.3.3 + +(define (lookup key table) + (let ((record (assoc key (cdr table)))) + (if record + (cdr record) + false))) + +(define (assoc key records) + (cond ((null? records) false) + ((equal? key (caar records)) (car records)) + (else (assoc key (cdr records))))) + +(define (insert! key value table) + (let ((record (assoc key (cdr table)))) + (if record + (set-cdr! record value) + (set-cdr! table + (cons (cons key value) (cdr table))))) + 'ok) + +(define (make-table) + (list '*table*)) + +;; two-dimensional +(define (lookup key-1 key-2 table) + (let ((subtable (assoc key-1 (cdr table)))) + (if subtable + (let ((record (assoc key-2 (cdr subtable)))) + (if record + (cdr record) + false)) + false))) + +(define (insert! key-1 key-2 value table) + (let ((subtable (assoc key-1 (cdr table)))) + (if subtable + (let ((record (assoc key-2 (cdr subtable)))) + (if record + (set-cdr! record value) + (set-cdr! subtable + (cons (cons key-2 value) + (cdr subtable))))) + (set-cdr! table + (cons (list key-1 + (cons key-2 value)) + (cdr table))))) + 'ok) + +;; local tables +(define (make-table) + (let ((local-table (list '*table*))) + (define (lookup key-1 key-2) + (let ((subtable (assoc key-1 (cdr local-table)))) + (if subtable + (let ((record (assoc key-2 (cdr subtable)))) + (if record + (cdr record) + false)) + false))) + (define (insert! key-1 key-2 value) + (let ((subtable (assoc key-1 (cdr local-table)))) + (if subtable + (let ((record (assoc key-2 (cdr subtable)))) + (if record + (set-cdr! record value) + (set-cdr! subtable + (cons (cons key-2 value) + (cdr subtable))))) + (set-cdr! local-table + (cons (list key-1 + (cons key-2 value)) + (cdr local-table))))) + 'ok) + (define (dispatch m) + (cond ((eq? m 'lookup-proc) lookup) + ((eq? m 'insert-proc!) insert!) + (else (error "Unknown operation -- TABLE" m)))) + dispatch)) + +(define operation-table (make-table)) +(define get (operation-table 'lookup-proc)) +(define put (operation-table 'insert-proc!)) + + +;; EXERCISE 3.27 +(define (fib n) + (cond ((= n 0) 0) + ((= n 1) 1) + (else (+ (fib (- n 1)) + (fib (- n 2)))))) + +(define (memoize f) + (let ((table (make-table))) + (lambda (x) + (let ((previously-computed-result (lookup x table))) + (or previously-computed-result + (let ((result (f x))) + (insert! x result table) + result)))))) + +(define memo-fib + (memoize (lambda (n) + (cond ((= n 0) 0) + ((= n 1) 1) + (else (+ (memo-fib (- n 1)) + (memo-fib (- n 2)))))))) + +;;;SECTION 3.3.4 + +;: (define a (make-wire)) +;: (define b (make-wire)) +;: (define c (make-wire)) +;: (define d (make-wire)) +;: (define e (make-wire)) +;: (define s (make-wire)) +;: +;: (or-gate a b d) +;: (and-gate a b c) +;: (inverter c e) +;: (and-gate d e s) + + +;;NB. To use half-adder, need or-gate from exercise 3.28 +(define (half-adder a b s c) + (let ((d (make-wire)) (e (make-wire))) + (or-gate a b d) + (and-gate a b c) + (inverter c e) + (and-gate d e s) + 'ok)) + +(define (full-adder a b c-in sum c-out) + (let ((s (make-wire)) + (c1 (make-wire)) + (c2 (make-wire))) + (half-adder b c-in s c1) + (half-adder a s sum c2) + (or-gate c1 c2 c-out) + 'ok)) + +(define (inverter input output) + (define (invert-input) + (let ((new-value (logical-not (get-signal input)))) + (after-delay inverter-delay + (lambda () + (set-signal! output new-value))))) + (add-action! input invert-input) + 'ok) + +(define (logical-not s) + (cond ((= s 0) 1) + ((= s 1) 0) + (else (error "Invalid signal" s)))) + +;; *following uses logical-and -- see ch3support.scm + +(define (and-gate a1 a2 output) + (define (and-action-procedure) + (let ((new-value + (logical-and (get-signal a1) (get-signal a2)))) + (after-delay and-gate-delay + (lambda () + (set-signal! output new-value))))) + (add-action! a1 and-action-procedure) + (add-action! a2 and-action-procedure) + 'ok) + + +(define (make-wire) + (let ((signal-value 0) (action-procedures '())) + (define (set-my-signal! new-value) + (if (not (= signal-value new-value)) + (begin (set! signal-value new-value) + (call-each action-procedures)) + 'done)) + (define (accept-action-procedure! proc) + (set! action-procedures (cons proc action-procedures)) + (proc)) + (define (dispatch m) + (cond ((eq? m 'get-signal) signal-value) + ((eq? m 'set-signal!) set-my-signal!) + ((eq? m 'add-action!) accept-action-procedure!) + (else (error "Unknown operation -- WIRE" m)))) + dispatch)) + +(define (call-each procedures) + (if (null? procedures) + 'done + (begin + ((car procedures)) + (call-each (cdr procedures))))) + +(define (get-signal wire) + (wire 'get-signal)) + +(define (set-signal! wire new-value) + ((wire 'set-signal!) new-value)) + +(define (add-action! wire action-procedure) + ((wire 'add-action!) action-procedure)) + +(define (after-delay delay action) + (add-to-agenda! (+ delay (current-time the-agenda)) + action + the-agenda)) + +(define (propagate) + (if (empty-agenda? the-agenda) + 'done + (let ((first-item (first-agenda-item the-agenda))) + (first-item) + (remove-first-agenda-item! the-agenda) + (propagate)))) + +(define (probe name wire) + (add-action! wire + (lambda () + (newline) + (display name) + (display " ") + (display (current-time the-agenda)) + (display " New-value = ") + (display (get-signal wire))))) + +;;; Sample simulation + +;: (define the-agenda (make-agenda)) +;: (define inverter-delay 2) +;: (define and-gate-delay 3) +;: (define or-gate-delay 5) +;: +;: (define input-1 (make-wire)) +;: (define input-2 (make-wire)) +;: (define sum (make-wire)) +;: (define carry (make-wire)) +;: +;: (probe 'sum sum) +;: (probe 'carry carry) +;: +;: (half-adder input-1 input-2 sum carry) +;: (set-signal! input-1 1) +;: (propagate) +;: +;: (set-signal! input-2 1) +;: (propagate) + + +;; EXERCISE 3.31 +;: (define (accept-action-procedure! proc) +;: (set! action-procedures (cons proc action-procedures))) + + +;;;Implementing agenda + +(define (make-time-segment time queue) + (cons time queue)) +(define (segment-time s) (car s)) +(define (segment-queue s) (cdr s)) + +(define (make-agenda) (list 0)) + +(define (current-time agenda) (car agenda)) +(define (set-current-time! agenda time) + (set-car! agenda time)) + +(define (segments agenda) (cdr agenda)) +(define (set-segments! agenda segments) + (set-cdr! agenda segments)) +(define (first-segment agenda) (car (segments agenda))) +(define (rest-segments agenda) (cdr (segments agenda))) + +(define (empty-agenda? agenda) + (null? (segments agenda))) + +(define (add-to-agenda! time action agenda) + (define (belongs-before? segments) + (or (null? segments) + (< time (segment-time (car segments))))) + (define (make-new-time-segment time action) + (let ((q (make-queue))) + (insert-queue! q action) + (make-time-segment time q))) + (define (add-to-segments! segments) + (if (= (segment-time (car segments)) time) + (insert-queue! (segment-queue (car segments)) + action) + (let ((rest (cdr segments))) + (if (belongs-before? rest) + (set-cdr! + segments + (cons (make-new-time-segment time action) + (cdr segments))) + (add-to-segments! rest))))) + (let ((segments (segments agenda))) + (if (belongs-before? segments) + (set-segments! + agenda + (cons (make-new-time-segment time action) + segments)) + (add-to-segments! segments)))) + +(define (remove-first-agenda-item! agenda) + (let ((q (segment-queue (first-segment agenda)))) + (delete-queue! q) + (if (empty-queue? q) + (set-segments! agenda (rest-segments agenda))))) + +(define (first-agenda-item agenda) + (if (empty-agenda? agenda) + (error "Agenda is empty -- FIRST-AGENDA-ITEM") + (let ((first-seg (first-segment agenda))) + (set-current-time! agenda (segment-time first-seg)) + (front-queue (segment-queue first-seg))))) + + +;;;SECTION 3.3.5 + +;: (define C (make-connector)) +;: (define F (make-connector)) +;: (celsius-fahrenheit-converter C F) + +(define (celsius-fahrenheit-converter c f) + (let ((u (make-connector)) + (v (make-connector)) + (w (make-connector)) + (x (make-connector)) + (y (make-connector))) + (multiplier c w u) + (multiplier v x u) + (adder v y f) + (constant 9 w) + (constant 5 x) + (constant 32 y) + 'ok)) + +;: (probe "Celsius temp" C) +;: (probe "Fahrenheit temp" F) +;: (set-value! C 25 'user) +;: (set-value! F 212 'user) +;: (forget-value! C 'user) +;: (set-value! F 212 'user) + + +(define (adder a1 a2 sum) + (define (process-new-value) + (cond ((and (has-value? a1) (has-value? a2)) + (set-value! sum + (+ (get-value a1) (get-value a2)) + me)) + ((and (has-value? a1) (has-value? sum)) + (set-value! a2 + (- (get-value sum) (get-value a1)) + me)) + ((and (has-value? a2) (has-value? sum)) + (set-value! a1 + (- (get-value sum) (get-value a2)) + me)))) + (define (process-forget-value) + (forget-value! sum me) + (forget-value! a1 me) + (forget-value! a2 me) + (process-new-value)) + (define (me request) + (cond ((eq? request 'I-have-a-value) + (process-new-value)) + ((eq? request 'I-lost-my-value) + (process-forget-value)) + (else + (error "Unknown request -- ADDER" request)))) + (connect a1 me) + (connect a2 me) + (connect sum me) + me) + +(define (inform-about-value constraint) + (constraint 'I-have-a-value)) + +(define (inform-about-no-value constraint) + (constraint 'I-lost-my-value)) + +(define (multiplier m1 m2 product) + (define (process-new-value) + (cond ((or (and (has-value? m1) (= (get-value m1) 0)) + (and (has-value? m2) (= (get-value m2) 0))) + (set-value! product 0 me)) + ((and (has-value? m1) (has-value? m2)) + (set-value! product + (* (get-value m1) (get-value m2)) + me)) + ((and (has-value? product) (has-value? m1)) + (set-value! m2 + (/ (get-value product) (get-value m1)) + me)) + ((and (has-value? product) (has-value? m2)) + (set-value! m1 + (/ (get-value product) (get-value m2)) + me)))) + (define (process-forget-value) + (forget-value! product me) + (forget-value! m1 me) + (forget-value! m2 me) + (process-new-value)) + (define (me request) + (cond ((eq? request 'I-have-a-value) + (process-new-value)) + ((eq? request 'I-lost-my-value) + (process-forget-value)) + (else + (error "Unknown request -- MULTIPLIER" request)))) + (connect m1 me) + (connect m2 me) + (connect product me) + me) + +(define (constant value connector) + (define (me request) + (error "Unknown request -- CONSTANT" request)) + (connect connector me) + (set-value! connector value me) + me) + +(define (probe name connector) + (define (print-probe value) + (newline) + (display "Probe: ") + (display name) + (display " = ") + (display value)) + (define (process-new-value) + (print-probe (get-value connector))) + (define (process-forget-value) + (print-probe "?")) + (define (me request) + (cond ((eq? request 'I-have-a-value) + (process-new-value)) + ((eq? request 'I-lost-my-value) + (process-forget-value)) + (else + (error "Unknown request -- PROBE" request)))) + (connect connector me) + me) + +(define (make-connector) + (let ((value false) (informant false) (constraints '())) + (define (set-my-value newval setter) + (cond ((not (has-value? me)) + (set! value newval) + (set! informant setter) + (for-each-except setter + inform-about-value + constraints)) + ((not (= value newval)) + (error "Contradiction" (list value newval))) + (else 'ignored))) + (define (forget-my-value retractor) + (if (eq? retractor informant) + (begin (set! informant false) + (for-each-except retractor + inform-about-no-value + constraints)) + 'ignored)) + (define (connect new-constraint) + (if (not (memq new-constraint constraints)) + (set! constraints + (cons new-constraint constraints))) + (if (has-value? me) + (inform-about-value new-constraint)) + 'done) + (define (me request) + (cond ((eq? request 'has-value?) + (if informant true false)) + ((eq? request 'value) value) + ((eq? request 'set-value!) set-my-value) + ((eq? request 'forget) forget-my-value) + ((eq? request 'connect) connect) + (else (error "Unknown operation -- CONNECTOR" + request)))) + me)) + +(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)) + +(define (has-value? connector) + (connector 'has-value?)) + +(define (get-value connector) + (connector 'value)) + +(define (set-value! connector new-value informant) + ((connector 'set-value!) new-value informant)) + +(define (forget-value! connector retractor) + ((connector 'forget) retractor)) + +(define (connect connector new-constraint) + ((connector 'connect) new-constraint)) + + +;; EXERCISE 3.34 + +(define (squarer a b) + (multiplier a a b)) + + + +;; EXERCISE 3.36 +;: (define a (make-connector)) +;: (define b (make-connector)) +;: (set-value! a 10 'user) + + +;; EXERCISE 3.37 + +(define (celsius-fahrenheit-converter x) + (c+ (c* (c/ (cv 9) (cv 5)) + x) + (cv 32))) + +;: (define C (make-connector)) +;: (define F (celsius-fahrenheit-converter C)) + +(define (c+ x y) + (let ((z (make-connector))) + (adder x y z) + z)) + + +;;;SECTION 3.4 +;;;**Need parallel-execute, available for MIT Scheme + +;;;SECTION 3.4.1 + +(define (withdraw amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")) + + +;; EXERCISE 3.38 +;: (set! balance (+ balance 10)) +;: (set! balance (- balance 20)) +;: (set! balance (- balance (/ balance 2))) + + +;;;SECTION 3.4.2 + +;: (define x 10) +;: (parallel-execute (lambda () (set! x (* x x))) +;: (lambda () (set! x (+ x 1)))) + +;: (define x 10) +;: (define s (make-serializer)) +;: (parallel-execute (s (lambda () (set! x (* x x)))) +;: (s (lambda () (set! x (+ x 1))))) + + +(define (make-account balance) + (define (withdraw amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")) + (define (deposit amount) + (set! balance (+ balance amount)) + balance) + (let ((protected (make-serializer))) + (define (dispatch m) + (cond ((eq? m 'withdraw) (protected withdraw)) + ((eq? m 'deposit) (protected deposit)) + ((eq? m 'balance) balance) + (else (error "Unknown request -- MAKE-ACCOUNT" + m)))) + dispatch)) + + +;; EXERCISE 3.39 + +;: (define x 10) +;: (define s (make-serializer)) +;: (parallel-execute (lambda () (set! x ((s (lambda () (* x x)))))) +;: (s (lambda () (set! x (+ x 1))))) + + +;; EXERCISE 3.40 + +;: (define x 10) +;: (parallel-execute (lambda () (set! x (* x x))) +;: (lambda () (set! x (* x x x)))) +;: +;: +;: (define x 10) +;: (define s (make-serializer)) +;: (parallel-execute (s (lambda () (set! x (* x x)))) +;: (s (lambda () (set! x (* x x x))))) + + +;; EXERCISE 3.41 + +(define (make-account balance) + (define (withdraw amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")) + (define (deposit amount) + (set! balance (+ balance amount)) + balance) + (let ((protected (make-serializer))) + (define (dispatch m) + (cond ((eq? m 'withdraw) (protected withdraw)) + ((eq? m 'deposit) (protected deposit)) + ((eq? m 'balance) + ((protected (lambda () balance)))) + (else (error "Unknown request -- MAKE-ACCOUNT" + m)))) + dispatch)) + +;; EXERCISE 3.42 + +(define (make-account balance) + (define (withdraw amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")) + (define (deposit amount) + (set! balance (+ balance amount)) + balance) + (let ((protected (make-serializer))) + (let ((protected-withdraw (protected withdraw)) + (protected-deposit (protected deposit))) + (define (dispatch m) + (cond ((eq? m 'withdraw) protected-withdraw) + ((eq? m 'deposit) protected-deposit) + ((eq? m 'balance) balance) + (else (error "Unknown request -- MAKE-ACCOUNT" + m)))) + dispatch))) + +;;;Multiple shared resources + +(define (exchange account1 account2) + (let ((difference (- (account1 'balance) + (account2 'balance)))) + ((account1 'withdraw) difference) + ((account2 'deposit) difference))) + +(define (make-account-and-serializer balance) + (define (withdraw amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")) + (define (deposit amount) + (set! balance (+ balance amount)) + balance) + (let ((balance-serializer (make-serializer))) + (define (dispatch m) + (cond ((eq? m 'withdraw) withdraw) + ((eq? m 'deposit) deposit) + ((eq? m 'balance) balance) + ((eq? m 'serializer) balance-serializer) + (else (error "Unknown request -- MAKE-ACCOUNT" + m)))) + dispatch)) + + +(define (deposit account amount) + (let ((s (account 'serializer)) + (d (account 'deposit))) + ((s d) amount))) + +(define (serialized-exchange account1 account2) + (let ((serializer1 (account1 'serializer)) + (serializer2 (account2 'serializer))) + ((serializer1 (serializer2 exchange)) + account1 + account2))) + + +;; EXERCISE 3.44 + +(define (transfer from-account to-account amount) + ((from-account 'withdraw) amount) + ((to-account 'deposit) amount)) + + +;; EXERCISE 3.45 + +(define (make-account-and-serializer balance) + (define (withdraw amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")) + (define (deposit amount) + (set! balance (+ balance amount)) + balance) + (let ((balance-serializer (make-serializer))) + (define (dispatch m) + (cond ((eq? m 'withdraw) (balance-serializer withdraw)) + ((eq? m 'deposit) (balance-serializer deposit)) + ((eq? m 'balance) balance) + ((eq? m 'serializer) balance-serializer) + (else (error "Unknown request -- MAKE-ACCOUNT" + m)))) + dispatch)) + +(define (deposit account amount) + ((account 'deposit) amount)) + + +;;;Implementing serializers + +(define (make-serializer) + (let ((mutex (make-mutex))) + (lambda (p) + (define (serialized-p . args) + (mutex 'acquire) + (let ((val (apply p args))) + (mutex 'release) + val)) + serialized-p))) + +(define (make-mutex) + (let ((cell (list false))) + (define (the-mutex m) + (cond ((eq? m 'acquire) + (if (test-and-set! cell) + (the-mutex 'acquire))) + ((eq? m 'release) (clear! cell)))) + the-mutex)) + +(define (clear! cell) + (set-car! cell false)) + +(define (test-and-set! cell) + (if (car cell) + true + (begin (set-car! cell true) + false))) + +;;from footnote -- MIT Scheme +(define (test-and-set! cell) + (without-interrupts + (lambda () + (if (car cell) + true + (begin (set-car! cell true) + false))))) + +;;;SECTION 3.5 + +;;;SECTION 3.5.1 + +(define (sum-primes a b) + (define (iter count accum) + (cond ((> count b) accum) + ((prime? count) (iter (+ count 1) (+ count accum))) + (else (iter (+ count 1) accum)))) + (iter a 0)) + + +(define (sum-primes a b) + (accumulate + + 0 + (filter prime? (enumerate-interval a b)))) + +;: (car (cdr (filter prime? +;: (enumerate-interval 10000 1000000)))) + +(define (stream-ref s n) + (if (= n 0) + (stream-car s) + (stream-ref (stream-cdr s) (- n 1)))) + +(define (stream-map proc s) + (if (stream-null? s) + the-empty-stream + (cons-stream (proc (stream-car s)) + (stream-map proc (stream-cdr s))))) + +(define (stream-for-each proc s) + (if (stream-null? s) + 'done + (begin (proc (stream-car s)) + (stream-for-each proc (stream-cdr s))))) + +(define (display-stream s) + (stream-for-each display-line s)) + +(define (display-line x) + (newline) + (display x)) + + + +;; stream-car and stream-cdr would normally be built into +;; the stream implementation +;: (define (stream-car stream) (car stream)) +;: (define (stream-cdr stream) (force (cdr stream))) + +;: (stream-car +;: (stream-cdr +;: (stream-filter prime? +;: (stream-enumerate-interval 10000 1000000)))) + +(define (stream-enumerate-interval low high) + (if (> low high) + the-empty-stream + (cons-stream + low + (stream-enumerate-interval (+ low 1) high)))) + +(define (stream-filter pred stream) + (cond ((stream-null? stream) the-empty-stream) + ((pred (stream-car stream)) + (cons-stream (stream-car stream) + (stream-filter pred + (stream-cdr stream)))) + (else (stream-filter pred (stream-cdr stream))))) + + +;; force would normally be built into +;; the stream implementation +;: (define (force delayed-object) +;: (delayed-object)) + +(define (memo-proc proc) + (let ((already-run? false) (result false)) + (lambda () + (if (not already-run?) + (begin (set! result (proc)) + (set! already-run? true) + result) + result)))) + + +;; EXERCISE 3.51 + +(define (show x) + (display-line x) + x) + +;: (define x (stream-map show (stream-enumerate-interval 0 10))) +;: (stream-ref x 5) +;: (stream-ref x 7) + + +;; EXERCISE 3.52 + +(define sum 0) + +(define (accum x) + (set! sum (+ x sum)) + sum) + +;: (define seq (stream-map accum (stream-enumerate-interval 1 20))) +;: (define y (stream-filter even? seq)) +;: (define z (stream-filter (lambda (x) (= (remainder x 5) 0)) +;: seq)) + +;: (stream-ref y 7) +;: (display-stream z) + + +;;;SECTION 3.5.2 + +(define (integers-starting-from n) + (cons-stream n (integers-starting-from (+ n 1)))) + +(define integers (integers-starting-from 1)) + +(define (divisible? x y) (= (remainder x y) 0)) + +(define no-sevens + (stream-filter (lambda (x) (not (divisible? x 7))) + integers)) + +;: (stream-ref no-sevens 100) + +(define (fibgen a b) + (cons-stream a (fibgen b (+ a b)))) + +(define fibs (fibgen 0 1)) + +(define (sieve stream) + (cons-stream + (stream-car stream) + (sieve (stream-filter + (lambda (x) + (not (divisible? x (stream-car stream)))) + (stream-cdr stream))))) + +(define primes (sieve (integers-starting-from 2))) + +;: (stream-ref primes 50) + + +;;;Defining streams implicitly;;;Defining streams implicitly + +(define ones (cons-stream 1 ones)) + +(define (add-streams s1 s2) + (stream-map + s1 s2)) + +(define integers (cons-stream 1 (add-streams ones integers))) + +(define fibs + (cons-stream 0 + (cons-stream 1 + (add-streams (stream-cdr fibs) + fibs)))) + +(define (scale-stream stream factor) + (stream-map (lambda (x) (* x factor)) stream)) + +(define double (cons-stream 1 (scale-stream double 2))) + +(define primes + (cons-stream + 2 + (stream-filter prime? (integers-starting-from 3)))) + +(define (prime? n) + (define (iter ps) + (cond ((> (square (stream-car ps)) n) true) + ((divisible? n (stream-car ps)) false) + (else (iter (stream-cdr ps))))) + (iter primes)) + + +;; EXERCISE 3.53 +;: (define s (cons-stream 1 (add-streams s s))) + + +;; EXERCISE 3.56 +(define (merge s1 s2) + (cond ((stream-null? s1) s2) + ((stream-null? s2) s1) + (else + (let ((s1car (stream-car s1)) + (s2car (stream-car s2))) + (cond ((< s1car s2car) + (cons-stream s1car (merge (stream-cdr s1) s2))) + ((> s1car s2car) + (cons-stream s2car (merge s1 (stream-cdr s2)))) + (else + (cons-stream s1car + (merge (stream-cdr s1) + (stream-cdr s2))))))))) + + +;; EXERCISE 3.58 +(define (expand num den radix) + (cons-stream + (quotient (* num radix) den) + (expand (remainder (* num radix) den) den radix))) + + +;; EXERCISE 3.59 +;: (define exp-series +;: (cons-stream 1 (integrate-series exp-series))) + + +;;;SECTION 3.5.3 + +(define (sqrt-improve guess x) + (average guess (/ x guess))) + + +(define (sqrt-stream x) + (define guesses + (cons-stream 1.0 + (stream-map (lambda (guess) + (sqrt-improve guess x)) + guesses))) + guesses) + +;: (display-stream (sqrt-stream 2)) + + +(define (pi-summands n) + (cons-stream (/ 1.0 n) + (stream-map - (pi-summands (+ n 2))))) + +;: (define pi-stream +;: (scale-stream (partial-sums (pi-summands 1)) 4)) + +;: (display-stream pi-stream) + + +(define (euler-transform s) + (let ((s0 (stream-ref s 0)) + (s1 (stream-ref s 1)) + (s2 (stream-ref s 2))) + (cons-stream (- s2 (/ (square (- s2 s1)) + (+ s0 (* -2 s1) s2))) + (euler-transform (stream-cdr s))))) + +;: (display-stream (euler-transform pi-stream)) + + +(define (make-tableau transform s) + (cons-stream s + (make-tableau transform + (transform s)))) + +(define (accelerated-sequence transform s) + (stream-map stream-car + (make-tableau transform s))) + +;: (display-stream (accelerated-sequence euler-transform +;: pi-stream)) + + +;; EXERCISE 3.63 +(define (sqrt-stream x) + (cons-stream 1.0 + (stream-map (lambda (guess) + (sqrt-improve guess x)) + (sqrt-stream x)))) + +;; EXERCISE 3.64 +(define (sqrt x tolerance) + (stream-limit (sqrt-stream x) tolerance)) + + +;;; Infinite streams of pairs + +;: (stream-filter (lambda (pair) +;: (prime? (+ (car pair) (cadr pair)))) +;: int-pairs) + +(define (stream-append s1 s2) + (if (stream-null? s1) + s2 + (cons-stream (stream-car s1) + (stream-append (stream-cdr s1) s2)))) + + +;: (pairs integers integers) + + +(define (interleave s1 s2) + (if (stream-null? s1) + s2 + (cons-stream (stream-car s1) + (interleave s2 (stream-cdr s1))))) + +(define (pairs s t) + (cons-stream + (list (stream-car s) (stream-car t)) + (interleave + (stream-map (lambda (x) (list (stream-car s) x)) + (stream-cdr t)) + (pairs (stream-cdr s) (stream-cdr t))))) + + +;; EXERCISE 3.68 + +(define (pairs s t) + (interleave + (stream-map (lambda (x) (list (stream-car s) x)) + t) + (pairs (stream-cdr s) (stream-cdr t)))) + + +;;; Streams as signals + +(define (integral integrand initial-value dt) + (define int + (cons-stream initial-value + (add-streams (scale-stream integrand dt) + int))) + int) + + +;; EXERCISE 3.74 + +(define (make-zero-crossings input-stream last-value) + (cons-stream + (sign-change-detector (stream-car input-stream) last-value) + (make-zero-crossings (stream-cdr input-stream) + (stream-car input-stream)))) + +;: (define zero-crossings (make-zero-crossings sense-data 0)) + + + +;; EXERCISE 3.75 + +(define (make-zero-crossings input-stream last-value) + (let ((avpt (/ (+ (stream-car input-stream) last-value) 2))) + (cons-stream (sign-change-detector avpt last-value) + (make-zero-crossings (stream-cdr input-stream) + avpt)))) + + +;;;SECTION 3.5.4 + +(define (solve f y0 dt) + (define y (integral dy y0 dt)) + (define dy (stream-map f y)) + y) + +(define (integral delayed-integrand initial-value dt) + (define int + (cons-stream initial-value + (let ((integrand (force delayed-integrand))) + (add-streams (scale-stream integrand dt) + int)))) + int) + +(define (solve f y0 dt) + (define y (integral (delay dy) y0 dt)) + (define dy (stream-map f y)) + y) + + +;: (stream-ref (solve (lambda (y) y) 1 0.001) 1000) + + +;; EXERCISE 3.77 + +(define (integral integrand initial-value dt) + (cons-stream initial-value + (if (stream-null? integrand) + the-empty-stream + (integral (stream-cdr integrand) + (+ (* dt (stream-car integrand)) + initial-value) + dt)))) + +;;;SECTION 3.5.5 + +;; same as in section 3.1.2 +(define rand + (let ((x random-init)) + (lambda () + (set! x (rand-update x)) + x))) + + +(define random-numbers + (cons-stream random-init + (stream-map rand-update random-numbers))) + + +;: (define cesaro-stream +;: (map-successive-pairs (lambda (r1 r2) (= (gcd r1 r2) 1)) +;: random-numbers)) + +(define (map-successive-pairs f s) + (cons-stream + (f (stream-car s) (stream-car (stream-cdr s))) + (map-successive-pairs f (stream-cdr (stream-cdr s))))) + + +(define (monte-carlo experiment-stream passed failed) + (define (next passed failed) + (cons-stream + (/ passed (+ passed failed)) + (monte-carlo + (stream-cdr experiment-stream) passed failed))) + (if (stream-car experiment-stream) + (next (+ passed 1) failed) + (next passed (+ failed 1)))) + +;: (define pi +;: (stream-map (lambda (p) (sqrt (/ 6 p))) +;: (monte-carlo cesaro-stream 0 0))) + + +;; same as in section 3.1.3 +(define (make-simplified-withdraw balance) + (lambda (amount) + (set! balance (- balance amount)) + balance)) + +(define (stream-withdraw balance amount-stream) + (cons-stream + balance + (stream-withdraw (- balance (stream-car amount-stream)) + (stream-cdr amount-stream)))) + + diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/concurrent.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/concurrent.scm new file mode 100644 index 0000000..b0014fc --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/concurrent.scm @@ -0,0 +1,245 @@ +;; Implementation of parallel-execute using call/cc. +;; +;; By Ben Rudiak-Gould, 10/2002. +;; +;; Requires STk (for "procedure-body" and first-class environments). + + +(define call/cc call-with-current-continuation) + + +(define (parallel-execute . thunks) + (apply run-concurrently-with-env + random + (map (lambda (thunk) + (cons (list (uncode (procedure-body thunk))) + (make-virtual-env (procedure-environment thunk)) )) + thunks )) + 'okay ) + + +(define (run-concurrently select . exprs) + (apply run-concurrently-with-env + select + (map (lambda (x) + (cons x (make-virtual-env (global-environment))) ) + exprs ))) + + +(define (run-concurrently-with-env select . exprs-with-envs) + (let ((threads + (map (lambda (exp-env) + (list (call/cc + (lambda (cont) + (let ((scheduler (call/cc cont))) + (scheduler (myeval (car exp-env) + (cdr exp-env) + scheduler ))))))) + exprs-with-envs ))) + (let loop () + (let ((active-threads + (filter (lambda (x) (continuation? (car x))) threads) )) + (if (null? active-threads) + (map car threads) + (let ((active (list-ref active-threads + (select (length active-threads)) ))) + (set-car! active (call/cc (car active))) + (loop) )))))) + + +(define (make-virtual-env real-env) + (cons + `((quote **macro** ,macro-quote) + (lambda **macro** ,macro-lambda) + (let **macro** ,macro-let) + (set! **macro** ,macro-set!) + (define **macro** ,macro-define) + (if **macro** ,macro-if) + (cond **macro** ,macro-cond) + (and **macro** ,macro-and) + (or **macro** ,macro-or) + (set-car! **prim** ,prim-set-car!) + (set-cdr! **prim** ,prim-set-cdr!) + (begin **prim** ,prim-begin) + (test-and-set! **prim** ,prim-test-and-set!) ) + real-env )) + + +(define (env-lookup-raw sym env scheduler) + (call/cc scheduler) + (let ((virtual (assq sym (car env)))) + (if virtual + (cdr virtual) + (eval sym (cdr env)) ))) + + +(define (env-lookup sym env scheduler) + (let* ((val (env-lookup-raw sym env scheduler)) + (proc-body (procedure-body val)) ) + (if (and proc-body (not (eq? (cadr proc-body) '**args**))) + (myeval (uncode proc-body) + (make-virtual-env (procedure-environment val)) + scheduler ) + val ))) + + +(define (env-set! sym val env scheduler) + (call/cc scheduler) + (let ((virtual (assq sym (car env)))) + (if virtual + (set-cdr! virtual val) + (eval `(set! ,sym ',val) (cdr env)) ))) + + +(define (env-define! sym val env scheduler) + (call/cc scheduler) + (set-car! env (cons (cons sym val) (car env))) ) + + +(define (get-special-form name env scheduler) + (if (symbol? name) + (let ((val (env-lookup-raw name env scheduler))) + (if (and (pair? val) (eq? (car val) '**macro**)) + val + #f )) + #f )) + + +(define (myeval expr env scheduler) + (cond ((pair? expr) + (let ((special (get-special-form (car expr) env scheduler))) + (if special + ((cadr special) (cdr expr) env scheduler) + (let ((evaluated (eval-seq expr env scheduler))) + (myapply (car evaluated) (cdr evaluated) scheduler) )))) + ((symbol? expr) + (env-lookup expr env scheduler) ) + (else (eval expr)) )) + + +(define (eval-seq exprs env scheduler) + (if (null? exprs) + '() + (let ((val (myeval (car exprs) env scheduler))) + (cons val (eval-seq (cdr exprs) env scheduler)) ))) + + +(define (myapply func args scheduler) + (cond ((procedure? func) + (apply func args) ) + ((and (pair? func) (eq? (car func) '**prim**)) + ((cadr func) args scheduler) ) + ((and (pair? func) (eq? (car func) '**macro**)) + ((cadr func) (map (lambda (x) (list 'quote x)) args) scheduler) ) + (else (error "apply of non-procedure" func args)) )) + + +(define (make-call-environment params args env) + (cons (let loop ((params params) (args args)) + (cond ((pair? params) + (cons (cons (car params) (car args)) + (loop (cdr params) (cdr args)) )) + ((null? params) + (car env) ) + (else (cons (cons params args) (car env))) )) + (cdr env) )) + + +(define (macro-lambda args env scheduler) + (let ((params (car args)) + (body (cdr args)) ) + (lambda **args** + (let ((new-env (make-call-environment params **args** env))) + (last (map (lambda (x) (myeval x new-env scheduler)) body)) )))) + + +(define (macro-let args env scheduler) + (let ((vars (map car (car args))) + (vals (map cadr (car args))) + (body (cdr args)) ) + (myeval `((lambda ,vars ,@body) ,@vals) env scheduler) )) + + +(define (macro-define args env scheduler) + (if (pair? (car args)) + (macro-define `(,(caar args) (lambda ,(cdar args) ,@(cdr args))) + env scheduler ) + (let ((val (myeval (cadr args) env scheduler))) + (env-define! (car args) val env scheduler) ))) + + +(define (macro-set! args env scheduler) + (let ((val (myeval (cadr args) env scheduler))) + (env-set! (car args) val env scheduler) )) + + +(define (macro-quote args env scheduler) + (car args) ) + + +(define (macro-if args env scheduler) + (if (myeval (car args) env scheduler) + (myeval (cadr args) env scheduler) + (if (pair? (cddr args)) + (myeval (caddr args) env scheduler) + 'okay ))) + + +(define (macro-cond args env scheduler) + (cond ((null? args) 'okay) + ((or (eq? (caar args) 'else) + (myeval (caar args) env scheduler) ) + (car (last-pair (eval-seq (cdar args) env scheduler))) ) + (else (macro-cond (cdr args) env scheduler)) )) + + +(define (macro-and args env scheduler) + (if (null? args) + #t + (let ((val (myeval (car args) env scheduler))) + (if (null? (cdr args)) + val + (and val (macro-and (cdr args) env scheduler)) )))) + + +(define (macro-or args env scheduler) + (if (null? args) + #f + (let ((val (myeval (car args) env scheduler))) + (if (null? (cdr args)) + val + (or val (macro-or (cdr args) env scheduler)) )))) + + +(define (prim-set-car! args scheduler) + (call/cc scheduler) + (apply set-car! args) ) + + +(define (prim-set-cdr! args scheduler) + (call/cc scheduler) + (apply set-cdr! args) ) + + +(define (prim-begin args scheduler) + (car (last-pair args)) ) + + +(define (prim-test-and-set! args scheduler) + (call/cc scheduler) + (test-and-set! (car args)) ) + + +(define (test-and-set! x) + (let ((oldval (car x))) + (set-car! x #t) + oldval )) + + +(define (last-pair lst) + (if (null? (cdr lst)) + lst + (last-pair (cdr lst)) )) + + +(load "~cs61a/lib/serial.scm") diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/constraint.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/constraint.scm new file mode 100644 index 0000000..831827d --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/constraint.scm @@ -0,0 +1,219 @@ +;; Programming with constraints, from section 3.3.5 of Abelson and Sussman. + +;; Syntactic interface to contraint and probe objects. +;; These operations inform them that a value has become defined or undefined; +;; they have to figure out which value is involved. + +(define (inform-about-value constraint) + ((constraint 'I-have-a-value))) + +(define (inform-about-no-value constraint) + ((constraint 'I-lost-my-value))) + +;; Types of constraints defined here: adder, multiplier and constant; +;; also define probe, which is a pseudo-constraint. + +(define (adder a1 a2 sum) + (define (process-new-value) + (cond ((and (has-value? a1) (has-value? a2)) + (set-value! sum + (+ (get-value a1) (get-value a2)) + me)) + ((and (has-value? a1) (has-value? sum)) + (set-value! a2 + (- (get-value sum) (get-value a1)) + me)) + ((and (has-value? a2) (has-value? sum)) + (set-value! a1 + (- (get-value sum) (get-value a2)) + me)))) + (define (process-forget-value) + (forget-value! sum me) + (forget-value! a1 me) + (forget-value! a2 me) + (process-new-value)) + + (define (me request) + (cond ((eq? request 'I-have-a-value) + process-new-value) + ((eq? request 'I-lost-my-value) + process-forget-value) + (else + (error "Unknown request -- ADDER" request)))) + + (connect a1 me) + (connect a2 me) + (connect sum me) + me) + +(define (multiplier m1 m2 product) + (define (process-new-value) + (cond ((or (if (has-value? m1) (= (get-value m1) 0) #f) + (if (has-value? m2) (= (get-value m2) 0) #f)) + (set-value! product 0 me)) + ((and (has-value? m1) (has-value? m2)) + (set-value! product + (* (get-value m1) (get-value m2)) + me)) + ((and (has-value? m1) (has-value? product)) + (set-value! m2 + (/ (get-value product) (get-value m1)) + me)) + ((and (has-value? m2) (has-value? product)) + (set-value! m1 + (/ (get-value product) (get-value m2)) + me)))) + (define (process-forget-value) + (forget-value! product me) + (forget-value! m1 me) + (forget-value! m2 me) + (process-new-value)) + + (define (me request) + (cond ((eq? request 'I-have-a-value) + process-new-value) + ((eq? request 'I-lost-my-value) + process-forget-value) + (else + (error "Unknown request -- MULTIPLIER" request)))) + + (connect m1 me) + (connect m2 me) + (connect product me) + me) + +(define (constant value connector) + (define (me request) + (error "Unknown request -- CONSTANT" request)) + (connect connector me) + (set-value! connector value me) + me) + +(define (probe name connector) + (define (process-new-value) + (display "Probe: ") + (display name) + (display " = ") + (display (get-value connector)) + (newline)) + + (define (process-forget-value) + (display "Probe: ") + (display name) + (display " = ") + (display "?") + (newline)) + + (define (me request) + (cond ((eq? request 'I-have-a-value) + process-new-value) + ((eq? request 'I-lost-my-value) + process-forget-value) + (else + (error "Unknown request -- PROBE" request)))) + (connect connector me) + me) + +;; syntactic interface to connector objects + +(define (has-value? connector) + (connector 'has-value?)) + +(define (get-value connector) + (connector 'value)) + +(define (forget-value! connector retractor) + ((connector 'forget) retractor)) + +(define (set-value! connector new-value informant) + ((connector 'set-value!) new-value informant)) + +(define (connect connector new-constraint) + ((connector 'connect) new-constraint)) + + +;; connector object generator. + +(define (make-connector) + (let ((value #f) (informant #f) (constraints '())) + + (define (set-my-value newval setter) + (cond ((not (has-value? me)) + (set! value newval) + (set! informant setter) + (for-each-except setter + inform-about-value + constraints)) + ((not (= value newval)) + (error "Contradiction" (list value newval))))) + + (define (forget-my-value retractor) + (if (eq? retractor informant) + (begin (set! informant #f) + (for-each-except retractor + inform-about-no-value + constraints)))) + + (define (connect new-constraint) + (if (not (memq new-constraint constraints)) + (set! constraints + (cons new-constraint constraints))) + (if (has-value? me) + (inform-about-value new-constraint))) + + (define (me request) + (cond ((eq? request 'has-value?) + (not (null? informant))) + ((eq? request 'value) value) + ((eq? request 'set-value!) set-my-value) + ((eq? request 'forget) forget-my-value) + ((eq? request 'connect) connect) + (else (error "Unknown operation -- CONNECTOR" request)))) + me)) + +;; Helper procedure for connector: +;; do the procedure for each element of list EXCEPT the exception. +;; used to inform connected constraints of value changes +;; (don't want to inform the source of the change) + +(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)) + +;; Example application: Centigrade/Fahrenheit converter. +;; Internal connectors and constraints are stashed in a private environment. +;; see Ex. 3.37 for a cleaner way to do this. + +(define (centigrade-fahrenheit-converter c f) + (let ((u (make-connector)) + (v (make-connector)) + (w (make-connector)) + (x (make-connector)) + (y (make-connector))) + + (multiplier c w u) + (multiplier v x u) + (adder v y f) + (constant 9 w) + (constant 5 x) + (constant 32 y))) + +(define C (make-connector)) +(define F (make-connector)) +(centigrade-fahrenheit-converter C F) +(probe "centigrade temp" C) +(probe "Fahrenheit temp" F) + +(define (fma-constraint f m a) + (multiplier m a f)) +(define force (make-connector)) +(define mass (make-connector)) +(define acceleration (make-connector)) +(fma-constraint force mass acceleration) +(probe "force" force) +(probe "mass" mass) +(probe "acceleration" acceleration) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/huffman.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/huffman.scm new file mode 100644 index 0000000..728b100 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/huffman.scm @@ -0,0 +1,61 @@ +(define (make-leaf symbol weight) + (list 'leaf symbol weight)) + +(define (leaf? object) + (eq? (car object) 'leaf)) + +(define (symbol-leaf x) (cadr x)) + +(define (weight-leaf x) (caddr x)) + +(define (make-code-tree left right) + (list left + right + (append (symbols left) (symbols right)) + (+ (weight left) (weight right)))) + +(define (left-branch tree) (car tree)) + +(define (right-branch tree) (cadr tree)) + +(define (symbols tree) + (if (leaf? tree) + (list (symbol-leaf tree)) + (caddr tree))) + +(define (weight tree) + (if (leaf? tree) + (weight-leaf tree) + (cadddr tree))) + +(define (adjoin-set x set) + (cond ((null? set) (list x)) + ((< (weight x) (weight (car set))) (cons x set)) + (else (cons (car set) + (adjoin-set x (cdr set)))))) + +(define (make-leaf-set pairs) + (if (null? pairs) + '() + (let ((pair (car pairs))) + (adjoin-set (make-leaf (car pair) ;symbol + (cadr pair)) ;frequency + (make-leaf-set (cdr pairs)))))) + +(define (decode bits tree) + (decode-1 bits tree tree)) + +(define (decode-1 bits tree current-branch) + (if (null? bits) + '() + (let ((next-branch + (choose-branch (car bits) current-branch))) + (if (leaf? next-branch) + (cons (symbol-leaf next-branch) + (decode-1 (cdr bits) tree tree)) + (decode-1 (cdr bits) tree next-branch))))) + +(define (choose-branch bit branch) + (cond ((= bit 0) (left-branch branch)) + ((= bit 1) (right-branch branch)) + (else (error "bad bit -- CHOOSE-BRANCH" bit)))) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=D;O=A b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=D;O=A new file mode 100644 index 0000000..354ca85 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=D;O=A @@ -0,0 +1,58 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<html> + <head> + <title>Index of /~bh/61a-pages/Lib</title> + </head> + <body> +<h1>Index of /~bh/61a-pages/Lib</h1> + <table> + <tr><th valign="top"><img src="../../../icons/blank.gif" alt="[ICO]"></th><th><a href="index.html?C=N%3BO=A">Name</a></th><th><a href="index.html?C=M%3BO=A">Last modified</a></th><th><a href="index.html?C=S%3BO=A">Size</a></th><th><a href="index.html?C=D%3BO=D">Description</a></th></tr> + <tr><th colspan="5"><hr></th></tr> +<tr><td valign="top"><img src="../../../icons/back.gif" alt="[PARENTDIR]"></td><td><a href="../../61a-pages">Parent Directory</a> </td><td> </td><td align="right"> - </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv-world.scm">adv-world.scm</a> </td><td align="right">2005-10-22 09:17 </td><td align="right">2.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv.scm">adv.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ambdiff">ambdiff</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="ambeval.scm">ambeval.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">9.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="analyze.scm">analyze.scm</a> </td><td align="right">2004-04-30 13:16 </td><td align="right">3.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="animal.scm">animal.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl-meta.scm">apl-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl.scm">apl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="berkeley.scmm">berkeley.scmm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 54K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="bst.scm">bst.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">710 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="calc.scm">calc.scm</a> </td><td align="right">2008-02-13 19:37 </td><td align="right">875 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter1.code">chapter1.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="chapter2.code">chapter2.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 47K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter3.code">chapter3.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 41K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="concurrent.scm">concurrent.scm</a> </td><td align="right">2003-04-23 11:28 </td><td align="right">6.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="constraint.scm">constraint.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="huffman.scm">huffman.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="labyrinth.scm">labyrinth.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">2.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="lazy.scm">lazy.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo-meta.scm">logo-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo.scm">logo.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="maze.scm">maze.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="mceval.scm">mceval.scm</a> </td><td align="right">2001-04-14 20:22 </td><td align="right"> 10K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="obj.scm">obj.scm</a> </td><td align="right">2006-01-23 09:52 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="old-obj.scm">old-obj.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="picture.scm">picture.scm</a> </td><td align="right">2000-10-02 11:44 </td><td align="right">3.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="pigl.scm">pigl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">207 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="prisoner.scm">prisoner.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="pronounce.scm">pronounce.scm</a> </td><td align="right">2012-12-14 09:39 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="query.scm">query.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 19K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ref-man.txt">ref-man.txt</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="resist.scm">resist.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="review">review</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="rps.scm">rps.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scheme1.scm">scheme1.scm</a> </td><td align="right">2007-10-10 09:09 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scmset">scmset</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 81 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="serial.scm">serial.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">542 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="small-world.scm">small-world.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tables.scm">tables.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">441 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="test.logo">test.logo</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">576 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tri.l">tri.l</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="turkey">turkey</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="twenty-one.scm">twenty-one.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.5K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="vambeval.scm">vambeval.scm</a> </td><td align="right">2000-12-03 14:33 </td><td align="right"> 14K</td><td> </td></tr> + <tr><th colspan="5"><hr></th></tr> +</table> +</body></html> diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=D;O=D b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=D;O=D new file mode 100644 index 0000000..5b79407 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=D;O=D @@ -0,0 +1,58 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<html> + <head> + <title>Index of /~bh/61a-pages/Lib</title> + </head> + <body> +<h1>Index of /~bh/61a-pages/Lib</h1> + <table> + <tr><th valign="top"><img src="../../../icons/blank.gif" alt="[ICO]"></th><th><a href="index.html?C=N%3BO=A">Name</a></th><th><a href="index.html?C=M%3BO=A">Last modified</a></th><th><a href="index.html?C=S%3BO=A">Size</a></th><th><a href="index.html?C=D%3BO=A">Description</a></th></tr> + <tr><th colspan="5"><hr></th></tr> +<tr><td valign="top"><img src="../../../icons/back.gif" alt="[PARENTDIR]"></td><td><a href="../../61a-pages">Parent Directory</a> </td><td> </td><td align="right"> - </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="vambeval.scm">vambeval.scm</a> </td><td align="right">2000-12-03 14:33 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="twenty-one.scm">twenty-one.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.5K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="turkey">turkey</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tri.l">tri.l</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="test.logo">test.logo</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">576 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tables.scm">tables.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">441 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="small-world.scm">small-world.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="serial.scm">serial.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">542 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scmset">scmset</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 81 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scheme1.scm">scheme1.scm</a> </td><td align="right">2007-10-10 09:09 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="rps.scm">rps.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="review">review</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="resist.scm">resist.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ref-man.txt">ref-man.txt</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="query.scm">query.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 19K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="pronounce.scm">pronounce.scm</a> </td><td align="right">2012-12-14 09:39 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="prisoner.scm">prisoner.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="pigl.scm">pigl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">207 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="picture.scm">picture.scm</a> </td><td align="right">2000-10-02 11:44 </td><td align="right">3.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="old-obj.scm">old-obj.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="obj.scm">obj.scm</a> </td><td align="right">2006-01-23 09:52 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="mceval.scm">mceval.scm</a> </td><td align="right">2001-04-14 20:22 </td><td align="right"> 10K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="maze.scm">maze.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo.scm">logo.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo-meta.scm">logo-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="lazy.scm">lazy.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="labyrinth.scm">labyrinth.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">2.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="huffman.scm">huffman.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="constraint.scm">constraint.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="concurrent.scm">concurrent.scm</a> </td><td align="right">2003-04-23 11:28 </td><td align="right">6.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter3.code">chapter3.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 41K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="chapter2.code">chapter2.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 47K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter1.code">chapter1.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="calc.scm">calc.scm</a> </td><td align="right">2008-02-13 19:37 </td><td align="right">875 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="bst.scm">bst.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">710 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="berkeley.scmm">berkeley.scmm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 54K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl.scm">apl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl-meta.scm">apl-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="animal.scm">animal.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="analyze.scm">analyze.scm</a> </td><td align="right">2004-04-30 13:16 </td><td align="right">3.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="ambeval.scm">ambeval.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">9.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ambdiff">ambdiff</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv.scm">adv.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv-world.scm">adv-world.scm</a> </td><td align="right">2005-10-22 09:17 </td><td align="right">2.9K</td><td> </td></tr> + <tr><th colspan="5"><hr></th></tr> +</table> +</body></html> diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=M;O=A b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=M;O=A new file mode 100644 index 0000000..31d89eb --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=M;O=A @@ -0,0 +1,58 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<html> + <head> + <title>Index of /~bh/61a-pages/Lib</title> + </head> + <body> +<h1>Index of /~bh/61a-pages/Lib</h1> + <table> + <tr><th valign="top"><img src="../../../icons/blank.gif" alt="[ICO]"></th><th><a href="index.html?C=N%3BO=A">Name</a></th><th><a href="https://people.eecs.berkeley.edu/~bh/61a-pages/Lib/?C=M;O=D">Last modified</a></th><th><a href="index.html?C=S%3BO=A">Size</a></th><th><a href="index.html?C=D%3BO=A">Description</a></th></tr> + <tr><th colspan="5"><hr></th></tr> +<tr><td valign="top"><img src="../../../icons/back.gif" alt="[PARENTDIR]"></td><td><a href="../../61a-pages">Parent Directory</a> </td><td> </td><td align="right"> - </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv.scm">adv.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ambdiff">ambdiff</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="animal.scm">animal.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl-meta.scm">apl-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl.scm">apl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter1.code">chapter1.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="chapter2.code">chapter2.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 47K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter3.code">chapter3.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 41K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="constraint.scm">constraint.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="huffman.scm">huffman.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="labyrinth.scm">labyrinth.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">2.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo-meta.scm">logo-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo.scm">logo.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="maze.scm">maze.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="old-obj.scm">old-obj.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="prisoner.scm">prisoner.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="query.scm">query.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 19K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ref-man.txt">ref-man.txt</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="resist.scm">resist.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="review">review</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="rps.scm">rps.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="serial.scm">serial.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">542 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="small-world.scm">small-world.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="turkey">turkey</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="twenty-one.scm">twenty-one.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.5K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="berkeley.scmm">berkeley.scmm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 54K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="bst.scm">bst.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">710 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="pigl.scm">pigl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">207 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scmset">scmset</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 81 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tables.scm">tables.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">441 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="test.logo">test.logo</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">576 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tri.l">tri.l</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="picture.scm">picture.scm</a> </td><td align="right">2000-10-02 11:44 </td><td align="right">3.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="vambeval.scm">vambeval.scm</a> </td><td align="right">2000-12-03 14:33 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="mceval.scm">mceval.scm</a> </td><td align="right">2001-04-14 20:22 </td><td align="right"> 10K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="concurrent.scm">concurrent.scm</a> </td><td align="right">2003-04-23 11:28 </td><td align="right">6.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="lazy.scm">lazy.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="ambeval.scm">ambeval.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">9.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="analyze.scm">analyze.scm</a> </td><td align="right">2004-04-30 13:16 </td><td align="right">3.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv-world.scm">adv-world.scm</a> </td><td align="right">2005-10-22 09:17 </td><td align="right">2.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="obj.scm">obj.scm</a> </td><td align="right">2006-01-23 09:52 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scheme1.scm">scheme1.scm</a> </td><td align="right">2007-10-10 09:09 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="calc.scm">calc.scm</a> </td><td align="right">2008-02-13 19:37 </td><td align="right">875 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="pronounce.scm">pronounce.scm</a> </td><td align="right">2012-12-14 09:39 </td><td align="right">4.7K</td><td> </td></tr> + <tr><th colspan="5"><hr></th></tr> +</table> +</body></html> diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=N;O=A b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=N;O=A new file mode 100644 index 0000000..a16ee53 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=N;O=A @@ -0,0 +1,58 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<html> + <head> + <title>Index of /~bh/61a-pages/Lib</title> + </head> + <body> +<h1>Index of /~bh/61a-pages/Lib</h1> + <table> + <tr><th valign="top"><img src="../../../icons/blank.gif" alt="[ICO]"></th><th><a href="index.html?C=N%3BO=D">Name</a></th><th><a href="index.html?C=M%3BO=A">Last modified</a></th><th><a href="index.html?C=S%3BO=A">Size</a></th><th><a href="index.html?C=D%3BO=A">Description</a></th></tr> + <tr><th colspan="5"><hr></th></tr> +<tr><td valign="top"><img src="../../../icons/back.gif" alt="[PARENTDIR]"></td><td><a href="../../61a-pages">Parent Directory</a> </td><td> </td><td align="right"> - </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv-world.scm">adv-world.scm</a> </td><td align="right">2005-10-22 09:17 </td><td align="right">2.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv.scm">adv.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ambdiff">ambdiff</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="ambeval.scm">ambeval.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">9.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="analyze.scm">analyze.scm</a> </td><td align="right">2004-04-30 13:16 </td><td align="right">3.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="animal.scm">animal.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl-meta.scm">apl-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl.scm">apl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="berkeley.scmm">berkeley.scmm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 54K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="bst.scm">bst.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">710 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="calc.scm">calc.scm</a> </td><td align="right">2008-02-13 19:37 </td><td align="right">875 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter1.code">chapter1.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="chapter2.code">chapter2.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 47K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter3.code">chapter3.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 41K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="concurrent.scm">concurrent.scm</a> </td><td align="right">2003-04-23 11:28 </td><td align="right">6.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="constraint.scm">constraint.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="huffman.scm">huffman.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="labyrinth.scm">labyrinth.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">2.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="lazy.scm">lazy.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo-meta.scm">logo-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo.scm">logo.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="maze.scm">maze.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="mceval.scm">mceval.scm</a> </td><td align="right">2001-04-14 20:22 </td><td align="right"> 10K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="obj.scm">obj.scm</a> </td><td align="right">2006-01-23 09:52 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="old-obj.scm">old-obj.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="picture.scm">picture.scm</a> </td><td align="right">2000-10-02 11:44 </td><td align="right">3.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="pigl.scm">pigl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">207 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="prisoner.scm">prisoner.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="pronounce.scm">pronounce.scm</a> </td><td align="right">2012-12-14 09:39 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="query.scm">query.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 19K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ref-man.txt">ref-man.txt</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="resist.scm">resist.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="review">review</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="rps.scm">rps.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scheme1.scm">scheme1.scm</a> </td><td align="right">2007-10-10 09:09 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scmset">scmset</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 81 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="serial.scm">serial.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">542 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="small-world.scm">small-world.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tables.scm">tables.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">441 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="test.logo">test.logo</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">576 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tri.l">tri.l</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="turkey">turkey</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="twenty-one.scm">twenty-one.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.5K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="vambeval.scm">vambeval.scm</a> </td><td align="right">2000-12-03 14:33 </td><td align="right"> 14K</td><td> </td></tr> + <tr><th colspan="5"><hr></th></tr> +</table> +</body></html> diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=N;O=D b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=N;O=D new file mode 100644 index 0000000..5b79407 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=N;O=D @@ -0,0 +1,58 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<html> + <head> + <title>Index of /~bh/61a-pages/Lib</title> + </head> + <body> +<h1>Index of /~bh/61a-pages/Lib</h1> + <table> + <tr><th valign="top"><img src="../../../icons/blank.gif" alt="[ICO]"></th><th><a href="index.html?C=N%3BO=A">Name</a></th><th><a href="index.html?C=M%3BO=A">Last modified</a></th><th><a href="index.html?C=S%3BO=A">Size</a></th><th><a href="index.html?C=D%3BO=A">Description</a></th></tr> + <tr><th colspan="5"><hr></th></tr> +<tr><td valign="top"><img src="../../../icons/back.gif" alt="[PARENTDIR]"></td><td><a href="../../61a-pages">Parent Directory</a> </td><td> </td><td align="right"> - </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="vambeval.scm">vambeval.scm</a> </td><td align="right">2000-12-03 14:33 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="twenty-one.scm">twenty-one.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.5K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="turkey">turkey</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tri.l">tri.l</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="test.logo">test.logo</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">576 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tables.scm">tables.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">441 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="small-world.scm">small-world.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="serial.scm">serial.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">542 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scmset">scmset</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 81 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scheme1.scm">scheme1.scm</a> </td><td align="right">2007-10-10 09:09 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="rps.scm">rps.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="review">review</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="resist.scm">resist.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ref-man.txt">ref-man.txt</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="query.scm">query.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 19K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="pronounce.scm">pronounce.scm</a> </td><td align="right">2012-12-14 09:39 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="prisoner.scm">prisoner.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="pigl.scm">pigl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">207 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="picture.scm">picture.scm</a> </td><td align="right">2000-10-02 11:44 </td><td align="right">3.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="old-obj.scm">old-obj.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="obj.scm">obj.scm</a> </td><td align="right">2006-01-23 09:52 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="mceval.scm">mceval.scm</a> </td><td align="right">2001-04-14 20:22 </td><td align="right"> 10K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="maze.scm">maze.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo.scm">logo.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo-meta.scm">logo-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="lazy.scm">lazy.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="labyrinth.scm">labyrinth.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">2.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="huffman.scm">huffman.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="constraint.scm">constraint.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="concurrent.scm">concurrent.scm</a> </td><td align="right">2003-04-23 11:28 </td><td align="right">6.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter3.code">chapter3.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 41K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="chapter2.code">chapter2.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 47K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter1.code">chapter1.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="calc.scm">calc.scm</a> </td><td align="right">2008-02-13 19:37 </td><td align="right">875 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="bst.scm">bst.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">710 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="berkeley.scmm">berkeley.scmm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 54K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl.scm">apl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl-meta.scm">apl-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="animal.scm">animal.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="analyze.scm">analyze.scm</a> </td><td align="right">2004-04-30 13:16 </td><td align="right">3.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="ambeval.scm">ambeval.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">9.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ambdiff">ambdiff</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv.scm">adv.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv-world.scm">adv-world.scm</a> </td><td align="right">2005-10-22 09:17 </td><td align="right">2.9K</td><td> </td></tr> + <tr><th colspan="5"><hr></th></tr> +</table> +</body></html> diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=S;O=A b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=S;O=A new file mode 100644 index 0000000..09372f1 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=S;O=A @@ -0,0 +1,58 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<html> + <head> + <title>Index of /~bh/61a-pages/Lib</title> + </head> + <body> +<h1>Index of /~bh/61a-pages/Lib</h1> + <table> + <tr><th valign="top"><img src="../../../icons/blank.gif" alt="[ICO]"></th><th><a href="index.html?C=N%3BO=A">Name</a></th><th><a href="index.html?C=M%3BO=A">Last modified</a></th><th><a href="index.html?C=S%3BO=D">Size</a></th><th><a href="index.html?C=D%3BO=A">Description</a></th></tr> + <tr><th colspan="5"><hr></th></tr> +<tr><td valign="top"><img src="../../../icons/back.gif" alt="[PARENTDIR]"></td><td><a href="../../61a-pages">Parent Directory</a> </td><td> </td><td align="right"> - </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scmset">scmset</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 81 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="pigl.scm">pigl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">207 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tables.scm">tables.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">441 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="serial.scm">serial.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">542 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="test.logo">test.logo</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">576 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="bst.scm">bst.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">710 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="calc.scm">calc.scm</a> </td><td align="right">2008-02-13 19:37 </td><td align="right">875 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="small-world.scm">small-world.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="twenty-one.scm">twenty-one.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.5K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="huffman.scm">huffman.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="turkey">turkey</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="rps.scm">rps.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="resist.scm">resist.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="maze.scm">maze.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="review">review</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="animal.scm">animal.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="labyrinth.scm">labyrinth.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">2.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv-world.scm">adv-world.scm</a> </td><td align="right">2005-10-22 09:17 </td><td align="right">2.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tri.l">tri.l</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="picture.scm">picture.scm</a> </td><td align="right">2000-10-02 11:44 </td><td align="right">3.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="analyze.scm">analyze.scm</a> </td><td align="right">2004-04-30 13:16 </td><td align="right">3.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="prisoner.scm">prisoner.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="pronounce.scm">pronounce.scm</a> </td><td align="right">2012-12-14 09:39 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="lazy.scm">lazy.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo.scm">logo.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ref-man.txt">ref-man.txt</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="constraint.scm">constraint.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl.scm">apl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="concurrent.scm">concurrent.scm</a> </td><td align="right">2003-04-23 11:28 </td><td align="right">6.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv.scm">adv.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl-meta.scm">apl-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scheme1.scm">scheme1.scm</a> </td><td align="right">2007-10-10 09:09 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="obj.scm">obj.scm</a> </td><td align="right">2006-01-23 09:52 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="old-obj.scm">old-obj.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="ambeval.scm">ambeval.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">9.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ambdiff">ambdiff</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo-meta.scm">logo-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="mceval.scm">mceval.scm</a> </td><td align="right">2001-04-14 20:22 </td><td align="right"> 10K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="vambeval.scm">vambeval.scm</a> </td><td align="right">2000-12-03 14:33 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter1.code">chapter1.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="query.scm">query.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 19K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter3.code">chapter3.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 41K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="chapter2.code">chapter2.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 47K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="berkeley.scmm">berkeley.scmm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 54K</td><td> </td></tr> + <tr><th colspan="5"><hr></th></tr> +</table> +</body></html> diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=S;O=D b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=S;O=D new file mode 100644 index 0000000..7378e03 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/index.html?C=S;O=D @@ -0,0 +1,58 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<html> + <head> + <title>Index of /~bh/61a-pages/Lib</title> + </head> + <body> +<h1>Index of /~bh/61a-pages/Lib</h1> + <table> + <tr><th valign="top"><img src="../../../icons/blank.gif" alt="[ICO]"></th><th><a href="index.html?C=N%3BO=A">Name</a></th><th><a href="index.html?C=M%3BO=A">Last modified</a></th><th><a href="index.html?C=S%3BO=A">Size</a></th><th><a href="index.html?C=D%3BO=A">Description</a></th></tr> + <tr><th colspan="5"><hr></th></tr> +<tr><td valign="top"><img src="../../../icons/back.gif" alt="[PARENTDIR]"></td><td><a href="../../61a-pages">Parent Directory</a> </td><td> </td><td align="right"> - </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="berkeley.scmm">berkeley.scmm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 54K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="chapter2.code">chapter2.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 47K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter3.code">chapter3.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 41K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="query.scm">query.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 19K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="chapter1.code">chapter1.code</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="vambeval.scm">vambeval.scm</a> </td><td align="right">2000-12-03 14:33 </td><td align="right"> 14K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="mceval.scm">mceval.scm</a> </td><td align="right">2001-04-14 20:22 </td><td align="right"> 10K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo-meta.scm">logo-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ambdiff">ambdiff</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="ambeval.scm">ambeval.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">9.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="old-obj.scm">old-obj.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="obj.scm">obj.scm</a> </td><td align="right">2006-01-23 09:52 </td><td align="right">9.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scheme1.scm">scheme1.scm</a> </td><td align="right">2007-10-10 09:09 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl-meta.scm">apl-meta.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">7.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv.scm">adv.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="concurrent.scm">concurrent.scm</a> </td><td align="right">2003-04-23 11:28 </td><td align="right">6.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="apl.scm">apl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">6.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="constraint.scm">constraint.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="ref-man.txt">ref-man.txt</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="logo.scm">logo.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">5.3K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="lazy.scm">lazy.scm</a> </td><td align="right">2004-04-30 13:15 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="pronounce.scm">pronounce.scm</a> </td><td align="right">2012-12-14 09:39 </td><td align="right">4.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="prisoner.scm">prisoner.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="analyze.scm">analyze.scm</a> </td><td align="right">2004-04-30 13:16 </td><td align="right">3.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="picture.scm">picture.scm</a> </td><td align="right">2000-10-02 11:44 </td><td align="right">3.4K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tri.l">tri.l</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">3.2K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="adv-world.scm">adv-world.scm</a> </td><td align="right">2005-10-22 09:17 </td><td align="right">2.9K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="labyrinth.scm">labyrinth.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">2.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="animal.scm">animal.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="review">review</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.8K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="maze.scm">maze.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="resist.scm">resist.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="rps.scm">rps.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.7K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/text.gif" alt="[TXT]"></td><td><a href="turkey">turkey</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="huffman.scm">huffman.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.6K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="twenty-one.scm">twenty-one.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.5K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="small-world.scm">small-world.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">1.1K</td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="calc.scm">calc.scm</a> </td><td align="right">2008-02-13 19:37 </td><td align="right">875 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="bst.scm">bst.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">710 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="test.logo">test.logo</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">576 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="serial.scm">serial.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">542 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="tables.scm">tables.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">441 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="pigl.scm">pigl.scm</a> </td><td align="right">2000-05-30 12:38 </td><td align="right">207 </td><td> </td></tr> +<tr><td valign="top"><img src="../../../icons/unknown.gif" alt="[ ]"></td><td><a href="scmset">scmset</a> </td><td align="right">2000-05-30 12:38 </td><td align="right"> 81 </td><td> </td></tr> + <tr><th colspan="5"><hr></th></tr> +</table> +</body></html> diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/labyrinth.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/labyrinth.scm new file mode 100644 index 0000000..5a1f3ab --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/labyrinth.scm @@ -0,0 +1,93 @@ +;;; To make a labyrinth underneath sproul-plaza, say +;;; (instantiate labyrinth sproul-plaza) +;;; now go down from sproul to enter + +;;; You might also want your character to maintain a list of rooms visited on +;;; its property list so you can find your way back to the earth's surface. + +(define-class (labyrinth connect-place) + (instance-vars (places (make-populated-places 100 60 4 'underground-room))) + (initialize + (can-go connect-place 'down (car places)) + (can-go (car places) 'up connect-place) + (connect-places places) + 'okay)) + +;;; You may find this helpful for moving around +;;; You may want to modify it so that you can look around +;;; in nearby rooms before entering so that you can avoid thieves. +(define (fancy-move-loop who) + (newline) + (let ((things (ask who 'look-around))) + (if things + (begin (print "You see") + (for-each (lambda (thing) + (display thing) + (display " ")) + things)))) + (newline) + (print (ask who 'exits)) + (display "? > ") + (let ((dir (read))) + (if (equal? dir 'stop) + (newline) + (begin (ask who 'go dir) + (fancy-move-loop who))))) + + + +(define (make-places count name) + (define (iter n) + (if (> n count) + '() + (cons (instantiate place (word name '- n)) + (iter (1+ n)) ))) + (iter 1)) + +(define *object-types* '(gold lead pizza potstickers burritos)) + +(define (make-populated-places n-places n-objects n-thieves place-name) + (let ((places (make-places n-places place-name))) + (dotimes n-objects + (lambda (count) + (ask (pick-random places) + 'appear + (instantiate thing (pick-random *object-types*))))) + (dotimes n-thieves + (lambda (count) + (instantiate thief + (word 'Nasty '- count) + (pick-random places)))) + places)) + +(define direction-pairs '((north . south) (south . north) + (east . west) (west . east) + (up . down) (down . up))) + +(define (connect-places places) + (for-each (lambda (place) + (connect-pair place (pick-random places))) + places)) + +(define (connect-pair place1 place2) + (define (c-p-helper place1 place2 dir-pairs) + (cond ((null? dir-pairs) 'done) + ((and (can-connect? place1 (caar dir-pairs)) + (can-connect? place2 (cdar dir-pairs))) + (can-go place1 (caar dir-pairs) place2) + (can-go place2 (cdar dir-pairs) place1)) + (else (c-p-helper place1 place2 (cdr dir-pairs))))) + (c-p-helper place1 place2 direction-pairs)) + +(define (can-connect? place direction) + (not (member? direction (ask place 'exits)))) + +(define (dotimes limit f) + ;; dotimes calls the procedure f on the numbers from 1 to the limit + ;; dotimes is for side effect only + (define (dotimes-iter count) + (if (> count limit) + 'done ;; dotimes is for side-effect + (begin (f count) + (dotimes-iter (1+ count))))) + (dotimes-iter 1)) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/lazy.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/lazy.scm new file mode 100644 index 0000000..76970cc --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/lazy.scm @@ -0,0 +1,165 @@ +;;;;LAZY EVALUATOR FROM SECTION 4.2 OF +;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS + +;;;;Matches code in ch4.scm +;;;; Also includes enlarged primitive-procedures list + +;;;;This file can be loaded into Scheme as a whole. +;;;;**NOTE**This file loads the metacircular evaluator of +;;;; sections 4.1.1-4.1.4, since it uses the expression representation, +;;;; environment representation, etc. +;;;; You may need to change the (load ...) expression to work in your +;;;; version of Scheme. + +;;;;Then you can initialize and start the evaluator by evaluating +;;;; the expression (mce). + + +;;**implementation-dependent loading of evaluator file +;;Note: It is loaded first so that the section 4.2 definition +;; of eval overrides the definition from 4.1.1 +(load "~/61a/Lib/mceval.scm") + + +;; To run without memoization, reload the first version of force-it below + + +;;;SECTION 4.2.2 + +;;; Modifying the evaluator + +(define (mc-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)) + ((begin? exp) + (eval-sequence (begin-actions exp) env)) + ((cond? exp) (mc-eval (cond->if exp) env)) + ((application? exp) ; clause from book + (mc-apply (actual-value (operator exp) env) + (operands exp) + env)) + (else + (error "Unknown expression type -- EVAL" exp)))) + +(define (actual-value exp env) + (force-it (mc-eval exp env))) + +(define (mc-apply procedure arguments env) + (cond ((primitive-procedure? procedure) + (apply-primitive-procedure + procedure + (list-of-arg-values arguments env))) ; changed + ((compound-procedure? procedure) + (eval-sequence + (procedure-body procedure) + (extend-environment + (procedure-parameters procedure) + (list-of-delayed-args arguments env) ; changed + (procedure-environment procedure)))) + (else + (error + "Unknown procedure type -- APPLY" procedure)))) + +(define (list-of-arg-values exps env) + (if (no-operands? exps) + '() + (cons (actual-value (first-operand exps) env) + (list-of-arg-values (rest-operands exps) + env)))) + +(define (list-of-delayed-args exps env) + (if (no-operands? exps) + '() + (cons (delay-it (first-operand exps) env) + (list-of-delayed-args (rest-operands exps) + env)))) + +(define (eval-if exp env) + (if (true? (actual-value (if-predicate exp) env)) + (mc-eval (if-consequent exp) env) + (mc-eval (if-alternative exp) env))) + +(define input-prompt ";;; L-Eval input:") +(define output-prompt ";;; L-Eval value:") + +(define (driver-loop) + (prompt-for-input input-prompt) + (let ((input (read))) + (let ((output + (actual-value input the-global-environment))) + (announce-output output-prompt) + (user-print output))) + (driver-loop)) + + +;;; Representing thunks + +;; non-memoizing version of force-it + +(define (force-it obj) + (if (thunk? obj) + (actual-value (thunk-exp obj) (thunk-env obj)) + obj)) + +;; thunks + +(define (delay-it exp env) + (list 'thunk exp env)) + +(define (thunk? obj) + (tagged-list? obj 'thunk)) + +(define (thunk-exp thunk) (cadr thunk)) +(define (thunk-env thunk) (caddr thunk)) + +;; "thunk" that has been forced and is storing its (memoized) value +(define (evaluated-thunk? obj) + (tagged-list? obj 'evaluated-thunk)) + +(define (thunk-value evaluated-thunk) (cadr evaluated-thunk)) + + +;; memoizing version of force-it + +(define (force-it obj) + (cond ((thunk? obj) + (let ((result (actual-value + (thunk-exp obj) + (thunk-env obj)))) + (set-car! obj 'evaluated-thunk) + (set-car! (cdr obj) result) ; replace exp with its value + (set-cdr! (cdr obj) '()) ; forget unneeded env + result)) + ((evaluated-thunk? obj) + (thunk-value obj)) + (else obj))) + + +;; A longer list of primitives -- suitable for running everything in 4.2 +;; Overrides the list in ch4-mceval.scm + +(define primitive-procedures + (list (list 'car car) + (list 'cdr cdr) + (list 'cons cons) + (list 'null? null?) + (list 'list list) + (list '+ +) + (list '- -) + (list '* *) + (list '/ /) + (list '= =) + (list 'newline newline) + (list 'display display) +;; more primitives + )) + +'LAZY-EVALUATOR-LOADED diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/logo-meta.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/logo-meta.scm new file mode 100644 index 0000000..3a9c5e1 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/logo-meta.scm @@ -0,0 +1,337 @@ +;;; logo-meta.scm Part of programming project #4 + +;;; Differences between the book and this version: Eval and apply have +;;; been changed to logo-eval and logo-apply so as not to overwrite the Scheme +;;; versions of these routines. An extra procedure initialize-logo has been +;;; added. This routine resets the global environment and then executes the +;;; driver loop. This procedure should be invoked to start the Logo +;;; evaluator executing. Note: It will reset your global environment and all +;;; definitions to the Logo interpreter will be lost. To restart the Logo +;;; interpreter without resetting the global environment, just invoke +;;; driver-loop. Don't forget that typing control-C will get you out of +;;; the Logo evaluator back into Scheme. + +;;; Problems A1, A2, and B2 are entirely in logo.scm +;;; Problems A3, B3, and 6 require you to find and change existing procedures. + +;;; Procedures that you must write from scratch: + +;;; Problem B1 eval-line + +(define (eval-line line-obj env) + (error "eval-line not written yet!")) + + +;;; Problem B3 variables (other procedures must be modified, too) +;;; data abstraction procedures + +(define (variable? exp) + #f) ;; not written yet but we fake it for now + +(define (variable-name exp) + (error "variable-name not written yet!")) + + +;;; Problem A4 handle-infix + +(define (de-infix token) + (cdr (assoc token '((+ . sum) + (- . difference) + (* . product) + (/ . quotient) + (= . equalp) + (< . lessp) + (> . greaterp))))) + +(define (handle-infix value line-obj env) + value) ;; This doesn't give an error message, so other stuff works. + + +;;; Problem B4 eval-definition + +(define (eval-definition line-obj) + (error "eval-definition not written yet!")) + + +;;; Problem 5 eval-sequence + +(define (eval-sequence exps env) + (error "eval-seqence not written yet!")) + + + + +;;; SETTING UP THE ENVIRONMENT + +(define the-primitive-procedures '()) + +(define (add-prim name count proc) + (set! the-primitive-procedures + (cons (list name 'primitive count proc) + the-primitive-procedures))) + +(add-prim 'first 1 first) +(add-prim 'butfirst 1 bf) +(add-prim 'bf 1 bf) +(add-prim 'last 1 last) +(add-prim 'butlast 1 bl) +(add-prim 'bl 1 bl) +(add-prim 'word 2 word) +(add-prim 'sentence 2 se) +(add-prim 'se 2 se) +(add-prim 'list 2 list) +(add-prim 'fput 2 cons) + +(add-prim 'sum 2 (make-logo-arith +)) +(add-prim 'difference 2 (make-logo-arith -)) +(add-prim '=unary-minus= 1 (make-logo-arith -)) +(add-prim '- 1 (make-logo-arith -)) +(add-prim 'product 2 (make-logo-arith *)) +(add-prim 'quotient 2 (make-logo-arith /)) +(add-prim 'remainder 2 (make-logo-arith remainder)) + +(add-prim 'print 1 logo-print) +(add-prim 'pr 1 logo-print) +(add-prim 'show 1 logo-show) +(add-prim 'type 1 logo-type) +(add-prim 'make '(2) make) + +(add-prim 'run '(1) run) +(add-prim 'if '(2) logo-if) +(add-prim 'ifelse '(3) ifelse) +(add-prim 'equalp 2 (logo-pred (make-logo-arith equalp))) +(add-prim 'lessp 2 (logo-pred (make-logo-arith <))) +(add-prim 'greaterp 2 (logo-pred (make-logo-arith >))) +(add-prim 'emptyp 1 (logo-pred empty?)) +(add-prim 'numberp 1 (logo-pred (make-logo-arith number?))) +(add-prim 'listp 1 (logo-pred list?)) +(add-prim 'wordp 1 (logo-pred (lambda (x) (not (list? x))))) + +(add-prim 'stop 0 (lambda () '=stop=)) +(add-prim 'output 1 (lambda (x) (cons '=output= x))) +(add-prim 'op 1 (lambda (x) (cons '=output= x))) + +(add-prim 'load 1 meta-load) + +(define the-global-environment '()) +(define the-procedures the-primitive-procedures) + +;;; INITIALIZATION AND DRIVER LOOP + +;;; The following code initializes the machine and starts the Logo +;;; system. You should not call it very often, because it will clobber +;;; the global environment, and you will lose any definitions you have +;;; accumulated. + +(define (initialize-logo) + (set! the-global-environment (extend-environment '() '() '())) + (set! the-procedures the-primitive-procedures) + (driver-loop)) + +(define (driver-loop) + (define (helper) + (prompt "? ") + (let ((line (logo-read))) + (if (not (null? line)) + (let ((result (eval-line (make-line-obj line) + the-global-environment))) + (if (not (eq? result '=no-value=)) + (logo-print (list "You don't say what to do with" result)))))) + (helper)) + (logo-read) + (helper)) + +;;; APPLYING PRIMITIVE PROCEDURES + +;;; To apply a primitive procedure, we ask the underlying Scheme system +;;; to perform the application. (Of course, an implementation on a +;;; low-level machine would perform the application in some other way.) + +(define (apply-primitive-procedure p args) + (apply (text p) args)) + + +;;; Now for the code that's based on the book!!! + + +;;; Section 4.1.1 + +;; Given an expression like (proc :a :b :c)+5 +;; logo-eval calls eval-prefix for the part in parentheses, and then +;; handle-infix to check for and process the infix arithmetic. +;; Eval-prefix is comparable to Scheme's eval. + +(define (logo-eval line-obj env) + (handle-infix (eval-prefix line-obj env) line-obj env)) + +(define (eval-prefix line-obj env) + (define (eval-helper paren-flag) + (let ((token (ask line-obj 'next))) + (cond ((self-evaluating? token) token) + ((variable? token) + (lookup-variable-value (variable-name token) env)) + ((quoted? token) (text-of-quotation token)) + ((definition? token) (eval-definition line-obj)) + ((left-paren? token) + (let ((result (handle-infix (eval-helper #t) + line-obj + env))) + (let ((token (ask line-obj 'next))) + (if (right-paren? token) + result + (error "Too much inside parens"))))) + ((right-paren? token) + (error "Unexpected ')'")) + (else + (let ((proc (lookup-procedure token))) + (if (not proc) (error "I don't know how to" token)) + (logo-apply proc + (collect-n-args (arg-count proc) + line-obj + env) + env))) ))) + (eval-helper #f)) + +(define (logo-apply procedure arguments env) + (cond ((primitive-procedure? procedure) + (apply-primitive-procedure procedure arguments)) + ((compound-procedure? procedure) + (eval-sequence (procedure-body procedure) + (extend-environment + (parameters procedure) + arguments + env))) + (else + (error "Unknown procedure type -- LOGO-APPLY" procedure)))) + +(define (collect-n-args n line-obj env) + (cond ((= n 0) '()) + ((and (< n 0) (not (ask line-obj 'empty?))) + (let ((token (ask line-obj 'next))) + (ask line-obj 'put-back token) + (if (right-paren? token) + '() + (let ((next (logo-eval line-obj env))) + (cons next + (collect-n-args (-1+ n) line-obj env)) )))) + (else + (let ((next (logo-eval line-obj env))) + (cons next + (collect-n-args (-1+ n) line-obj env)) )))) + +;;; Section 4.1.2 -- Representing expressions + +;;; numbers + +(define (self-evaluating? exp) (number? exp)) + +;;; quote + +(define (quoted? exp) + (or (list? exp) + (eq? (string-ref (word->string (first exp)) 0) #\"))) + +(define (text-of-quotation exp) + (if (list? exp) + exp + (bf exp))) + +;;; parens + +(define (left-paren? exp) (eq? exp left-paren-symbol)) + +(define (right-paren? exp) (eq? exp right-paren-symbol)) + +;;; definitions + +(define (definition? exp) + (eq? exp 'to)) + +;;; procedures + +(define (lookup-procedure name) + (assoc name the-procedures)) + +(define (primitive-procedure? p) + (eq? (cadr p) 'primitive)) + +(define (compound-procedure? p) + (eq? (cadr p) 'compound)) + +(define (arg-count proc) + (caddr proc)) + +(define (text proc) + (cadddr proc)) + +(define (parameters proc) (car (text proc))) + +(define (procedure-body proc) (cdr (text proc))) + +;;; Section 4.1.3 + +;;; Operations on environments + +(define (enclosing-environment env) (cdr env)) + +(define (first-frame env) (car env)) + +(define the-empty-environment '()) + +(define (make-frame variables values) + (cons variables values)) + +(define (frame-variables frame) (car frame)) +(define (frame-values frame) (cdr frame)) + +(define (add-binding-to-frame! var val frame) + (set-car! frame (cons var (car frame))) + (set-cdr! frame (cons val (cdr frame)))) + +(define (extend-environment vars vals base-env) + (if (= (length vars) (length vals)) + (cons (make-frame vars vals) base-env) + (if (< (length vars) (length vals)) + (error "Too many arguments supplied" vars vals) + (error "Too few arguments supplied" vars vals)))) + +(define (lookup-variable-value var env) + (define (env-loop env) + (define (scan vars vals) + (cond ((null? vars) + (env-loop (enclosing-environment env))) + ((eq? var (car vars)) + (car vals)) + (else (scan (cdr vars) (cdr vals))))) + (if (eq? env the-empty-environment) + (error "Unbound variable" var) + (let ((frame (first-frame env))) + (scan (frame-variables frame) + (frame-values frame))))) + (env-loop env)) + +(define (set-variable-value! var val env) + (define (env-loop env) + (define (scan vars vals) + (cond ((null? vars) + (env-loop (enclosing-environment env))) + ((eq? var (car vars)) + (set-car! vals val)) + (else (scan (cdr vars) (cdr vals))))) + (if (eq? env the-empty-environment) + (error "Unbound variable -- SET!" var) + (let ((frame (first-frame env))) + (scan (frame-variables frame) + (frame-values frame))))) + (env-loop env)) + +(define (define-variable! var val env) + (let ((frame (first-frame env))) + (define (scan vars vals) + (cond ((null? vars) + (add-binding-to-frame! var val frame)) + ((eq? var (car vars)) + (set-car! vals val)) + (else (scan (cdr vars) (cdr vals))))) + (scan (frame-variables frame) + (frame-values frame)))) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/logo.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/logo.scm new file mode 100644 index 0000000..49fdab1 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/logo.scm @@ -0,0 +1,173 @@ +;;; logo.scm part of programming project #4 + + +;;; Problem A1 make-line-obj + +(define (make-line-obj text) + (error "make-line-obj not written yet!")) + + +;;; Problem A2 logo-type + +(define (logo-type val) + (error "logo-type not written yet!")) + +(define (logo-print val) + (logo-type val) + (newline) + '=no-value=) + +(define (logo-show val) + (logo-print (list val))) + + + +;;; Problem B3 variables (logo-meta.scm is also affected) + +(define (make env var val) + (error "make not written yet!") + '=no-value=) + + +;;; Here are the primitives RUN, IF, and IFELSE. Problem B2 provides +;;; support for these, but you don't have to modify them. + +(define (run env exp) + (eval-line (make-line-obj exp) env)) + +(define (logo-if env t/f exp) + (cond ((eq? t/f 'true) (eval-line (make-line-obj exp) env)) + ((eq? t/f 'false) '=no-value=) + (else (error "Input to IF not true or false" t/f)))) + +(define (ifelse env t/f exp1 exp2) + (cond ((eq? t/f 'true) (eval-line (make-line-obj exp1) env)) + ((eq? t/f 'false) (eval-line (make-line-obj exp2) env)) + (else (error "Input to IFELSE not true or false" t/f)))) + + +;;; Problem B2 logo-pred + +(define (logo-pred pred) + pred) ;; This isn't written yet but we fake it for now. + + +;;; Here is an example of a Scheme predicate that will be turned into +;;; a Logo predicate by logo-pred: + +(define (equalp a b) + (if (and (number? a) (number? b)) + (= a b) + (equal? a b))) + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Stuff below here is needed for the interpreter to work but you ;;; +;;; don't have to modify anything or understand how they work. ;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + +;;; The Logo reader + +(define left-paren-symbol (string->symbol (make-string 1 #\( ))) +(define right-paren-symbol (string->symbol (make-string 1 #\) ))) +(define quote-symbol (string->symbol (make-string 1 #\" ))) + +(define (logo-read) + (define lookahead #f) + (define (logo-read-help depth) + (define (get-char) + (if lookahead + (let ((char lookahead)) + (set! lookahead #f) + char) + (let ((char (read-char))) + (if (eq? char #\\) + (list (read-char)) + char)))) + (define (quoted char) + (if (pair? char) + char + (list char))) + (define (get-symbol char) + (define (iter sofar char) + (cond ((pair? char) (iter (cons (car char) sofar) (get-char))) + ((memq char + '(#\space #\newline #\+ #\- #\* #\/ + #\= #\< #\> #\( #\) #\[ #\] )) + (set! lookahead char) + sofar) + (else (iter (cons char sofar) (get-char))) )) + (string->word (list->string (reverse (iter '() char)))) ) + (define (get-token space-flag) + (let ((char (get-char))) + (cond ((eq? char #\space) (get-token #t)) + ((memq char '(#\+ #\* #\/ #\= #\< #\> #\( #\) )) + (string->symbol (make-string 1 char))) + ((eq? char #\-) + (if space-flag + (let ((char (get-char))) + (let ((result (if (eq? char #\space) + '- + '=unary-minus=))) + (set! lookahead char) + result)) + '-)) + ((eq? char #\[) (logo-read-help (1+ depth))) + ((pair? char) (get-symbol char)) + ((eq? char #\") + (let ((char (get-char))) + (if (memq char '(#\[ #\] #\newline)) + (begin (set! lookahead char) quote-symbol) + (string->symbol (word quote-symbol + (get-symbol (quoted char))))))) + (else (get-symbol char)) ))) + (define (after-space) + (let ((char (get-char))) + (if (eq? char #\space) + (after-space) + char))) + (let ((char (get-char))) + (cond ((eq? char #\newline) + (if (> depth 0) (set! lookahead char)) + '()) + ((eq? char #\space) + (let ((char (after-space))) + (if (eq? char #\newline) + (begin (if (> depth 0) (set! lookahead char)) + '()) + (begin (set! lookahead char) + (let ((token (get-token #t))) + (cons token (logo-read-help depth))))))) + ((eq? char #\]) + (if (> depth 0) '() (error "Unexpected ]"))) + ((eof-object? char) char) + (else (set! lookahead char) + (let ((token (get-token #f))) + (cons token (logo-read-help depth)) ))))) + (logo-read-help 0)) + + +;;; Assorted stuff + +(define (make-logo-arith op) + (lambda args (apply op (map maybe-num args)))) + +(define (maybe-num val) + (string->word (word->string val))) + +(define tty-port (current-input-port)) + +(define (prompt string) + (if (eq? (current-input-port) tty-port) (display string))) + +(define (meta-load fn) + (define (loader) + (let ((exp (logo-read))) + (if (eof-object? exp) + '() + (begin (eval-line (make-line-obj exp) + the-global-environment) + (loader))))) + (with-input-from-file (symbol->string fn) loader) + '=no-value=) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/maze.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/maze.scm new file mode 100644 index 0000000..cddce36 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/maze.scm @@ -0,0 +1,64 @@ +(define make-square list) + +(define row car) + +(define column cadr) + +(define adas-maze + (list + (make-square 1 3) (make-square 1 4) + (make-square 2 1) (make-square 2 2) (make-square 2 4) + (make-square 3 1) (make-square 3 4) (make-square 3 5) + (make-square 4 1) (make-square 4 2) (make-square 4 4) + (make-square 5 2) (make-square 5 3) (make-square 5 4))) + +(define (neighbors square) + (list (make-square (- (row square) 1) (column square)) + (make-square (+ (row square) 1) (column square)) + (make-square (row square) (- (column square) 1)) + (make-square (row square) (+ (column square) 1)))) + +(define (find-path start goal maze) + (try-paths (list (list start)) goal maze)) + +(define (try-paths paths goal maze) + (define (try-loop p) + (cond ((null? p) + (try-paths (extend-all-paths paths maze) + goal + maze)) + ((complete? goal (car p)) + (car p)) + (else (try-loop (cdr p))))) + (try-loop paths)) + +(define (complete? goal path) + (same-square? goal (car path))) + +(define (same-square? s1 s2) + (and (= (row s1) (row s2)) + (= (column s1) (column s2)))) + +(define (extend-all-paths paths maze) + (if (null? paths) + '() + (append (extend-path (car paths) + (neighbors (car (car paths))) + maze) + (extend-all-paths (cdr paths) maze)))) + +(define (extend-path path neighbors maze) + (cond ((null? neighbors) '()) + ((allowed-extension? (car neighbors) maze) + (cons (cons (car neighbors) path) + (extend-path path (cdr neighbors) maze))) + (else (extend-path path (cdr neighbors) maze)))) + +(define (allowed-extension? square maze) + (square-in-list? square maze)) + +(define (square-in-list? square lst) + (cond ((null? lst) #f) + ((same-square? square (car lst)) #t) + (else (square-in-list? square (cdr lst))))) + diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/mceval.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/mceval.scm new file mode 100644 index 0000000..ed95fd1 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/mceval.scm @@ -0,0 +1,375 @@ +;;;;METACIRCULAR EVALUATOR FROM CHAPTER 4 (SECTIONS 4.1.1-4.1.4) of +;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS + +;;;;Matches code in ch4.scm + +;;;;This file can be loaded into Scheme as a whole. +;;;;Then you can initialize and start the evaluator by evaluating +;;;; the expression (mce). + +;;;from section 4.1.4 -- must precede def of metacircular apply +(define apply-in-underlying-scheme apply) + +;;;SECTION 4.1.1 + +(define (mc-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)) + ((begin? exp) + (eval-sequence (begin-actions exp) env)) + ((cond? exp) (mc-eval (cond->if exp) env)) + ((application? exp) + (mc-apply (mc-eval (operator exp) env) + (list-of-values (operands exp) env))) + (else + (error "Unknown expression type -- EVAL" exp)))) + +(define (mc-apply procedure arguments) + (cond ((primitive-procedure? procedure) + (apply-primitive-procedure procedure arguments)) + ((compound-procedure? procedure) + (eval-sequence + (procedure-body procedure) + (extend-environment + (procedure-parameters procedure) + arguments + (procedure-environment procedure)))) + (else + (error + "Unknown procedure type -- APPLY" procedure)))) + + +(define (list-of-values exps env) + (if (no-operands? exps) + '() + (cons (mc-eval (first-operand exps) env) + (list-of-values (rest-operands exps) env)))) + +(define (eval-if exp env) + (if (true? (mc-eval (if-predicate exp) env)) + (mc-eval (if-consequent exp) env) + (mc-eval (if-alternative exp) env))) + +(define (eval-sequence exps env) + (cond ((last-exp? exps) (mc-eval (first-exp exps) env)) + (else (mc-eval (first-exp exps) env) + (eval-sequence (rest-exps exps) env)))) + +(define (eval-assignment exp env) + (set-variable-value! (assignment-variable exp) + (mc-eval (assignment-value exp) env) + env) + 'ok) + +(define (eval-definition exp env) + (define-variable! (definition-variable exp) + (mc-eval (definition-value exp) env) + env) + 'ok) + +;;;SECTION 4.1.2 + +(define (self-evaluating? exp) + (cond ((number? exp) true) + ((string? exp) true) + ((boolean? exp) true) + (else false))) + +(define (quoted? exp) + (tagged-list? exp 'quote)) + +(define (text-of-quotation exp) (cadr exp)) + +(define (tagged-list? exp tag) + (if (pair? exp) + (eq? (car exp) tag) + false)) + +(define (variable? exp) (symbol? exp)) + +(define (assignment? exp) + (tagged-list? exp 'set!)) + +(define (assignment-variable exp) (cadr exp)) + +(define (assignment-value exp) (caddr exp)) + + +(define (definition? exp) + (tagged-list? exp 'define)) + +(define (definition-variable exp) + (if (symbol? (cadr exp)) + (cadr exp) + (caadr exp))) + +(define (definition-value exp) + (if (symbol? (cadr exp)) + (caddr exp) + (make-lambda (cdadr exp) + (cddr exp)))) + +(define (lambda? exp) (tagged-list? exp 'lambda)) + +(define (lambda-parameters exp) (cadr exp)) +(define (lambda-body exp) (cddr exp)) + +(define (make-lambda parameters body) + (cons 'lambda (cons parameters body))) + + +(define (if? exp) (tagged-list? exp 'if)) + +(define (if-predicate exp) (cadr exp)) + +(define (if-consequent exp) (caddr exp)) + +(define (if-alternative exp) + (if (not (null? (cdddr exp))) + (cadddr exp) + 'false)) + +(define (make-if predicate consequent alternative) + (list 'if predicate consequent alternative)) + + +(define (begin? exp) (tagged-list? exp 'begin)) + +(define (begin-actions exp) (cdr exp)) + +(define (last-exp? seq) (null? (cdr seq))) +(define (first-exp seq) (car seq)) +(define (rest-exps seq) (cdr seq)) + +(define (sequence->exp seq) + (cond ((null? seq) seq) + ((last-exp? seq) (first-exp seq)) + (else (make-begin seq)))) + +(define (make-begin seq) (cons 'begin seq)) + + +(define (application? exp) (pair? exp)) +(define (operator exp) (car exp)) +(define (operands exp) (cdr exp)) + +(define (no-operands? ops) (null? ops)) +(define (first-operand ops) (car ops)) +(define (rest-operands ops) (cdr ops)) + + +(define (cond? exp) (tagged-list? exp 'cond)) + +(define (cond-clauses exp) (cdr exp)) + +(define (cond-else-clause? clause) + (eq? (cond-predicate clause) 'else)) + +(define (cond-predicate clause) (car clause)) + +(define (cond-actions clause) (cdr clause)) + +(define (cond->if exp) + (expand-clauses (cond-clauses exp))) + +(define (expand-clauses clauses) + (if (null? clauses) + 'false ; no else clause + (let ((first (car clauses)) + (rest (cdr clauses))) + (if (cond-else-clause? first) + (if (null? rest) + (sequence->exp (cond-actions first)) + (error "ELSE clause isn't last -- COND->IF" + clauses)) + (make-if (cond-predicate first) + (sequence->exp (cond-actions first)) + (expand-clauses rest)))))) + +;;;SECTION 4.1.3 + +(define (true? x) + (not (eq? x false))) + +(define (false? x) + (eq? x false)) + + +(define (make-procedure parameters body env) + (list 'procedure parameters body env)) + +(define (compound-procedure? p) + (tagged-list? p 'procedure)) + + +(define (procedure-parameters p) (cadr p)) +(define (procedure-body p) (caddr p)) +(define (procedure-environment p) (cadddr p)) + + +(define (enclosing-environment env) (cdr env)) + +(define (first-frame env) (car env)) + +(define the-empty-environment '()) + +(define (make-frame variables values) + (cons variables values)) + +(define (frame-variables frame) (car frame)) +(define (frame-values frame) (cdr frame)) + +(define (add-binding-to-frame! var val frame) + (set-car! frame (cons var (car frame))) + (set-cdr! frame (cons val (cdr frame)))) + +(define (extend-environment vars vals base-env) + (if (= (length vars) (length vals)) + (cons (make-frame vars vals) base-env) + (if (< (length vars) (length vals)) + (error "Too many arguments supplied" vars vals) + (error "Too few arguments supplied" vars vals)))) + +(define (lookup-variable-value var env) + (define (env-loop env) + (define (scan vars vals) + (cond ((null? vars) + (env-loop (enclosing-environment env))) + ((eq? var (car vars)) + (car vals)) + (else (scan (cdr vars) (cdr vals))))) + (if (eq? env the-empty-environment) + (error "Unbound variable" var) + (let ((frame (first-frame env))) + (scan (frame-variables frame) + (frame-values frame))))) + (env-loop env)) + +(define (set-variable-value! var val env) + (define (env-loop env) + (define (scan vars vals) + (cond ((null? vars) + (env-loop (enclosing-environment env))) + ((eq? var (car vars)) + (set-car! vals val)) + (else (scan (cdr vars) (cdr vals))))) + (if (eq? env the-empty-environment) + (error "Unbound variable -- SET!" var) + (let ((frame (first-frame env))) + (scan (frame-variables frame) + (frame-values frame))))) + (env-loop env)) + +(define (define-variable! var val env) + (let ((frame (first-frame env))) + (define (scan vars vals) + (cond ((null? vars) + (add-binding-to-frame! var val frame)) + ((eq? var (car vars)) + (set-car! vals val)) + (else (scan (cdr vars) (cdr vals))))) + (scan (frame-variables frame) + (frame-values frame)))) + +;;;SECTION 4.1.4 + +(define (setup-environment) + (let ((initial-env + (extend-environment (primitive-procedure-names) + (primitive-procedure-objects) + the-empty-environment))) + (define-variable! 'true true initial-env) + (define-variable! 'false false initial-env) + (define-variable! 'import + (list 'primitive + (lambda (name) + (define-variable! name + (list 'primitive (eval name)) + the-global-environment))) + initial-env) + initial-env)) + +;[do later] (define the-global-environment (setup-environment)) + +(define (primitive-procedure? proc) + (tagged-list? proc 'primitive)) + +(define (primitive-implementation proc) (cadr proc)) + +(define primitive-procedures + (list (list 'car car) + (list 'cdr cdr) + (list 'cons cons) + (list 'null? null?) + (list '+ +) + (list '- -) + (list '* *) + (list '/ /) + (list '= =) + (list 'list list) + (list 'append append) + (list 'equal? equal?) +;; more primitives + )) + +(define (primitive-procedure-names) + (map car + primitive-procedures)) + +(define (primitive-procedure-objects) + (map (lambda (proc) (list 'primitive (cadr proc))) + primitive-procedures)) + +;[moved to start of file] (define apply-in-underlying-scheme apply) + +(define (apply-primitive-procedure proc args) + (apply-in-underlying-scheme + (primitive-implementation proc) args)) + + + +(define input-prompt ";;; M-Eval input:") +(define output-prompt ";;; M-Eval value:") + +(define (driver-loop) + (prompt-for-input input-prompt) + (let ((input (read))) + (let ((output (mc-eval input the-global-environment))) + (announce-output output-prompt) + (user-print output))) + (driver-loop)) + +(define (prompt-for-input string) + (newline) (newline) (display string) (newline)) + +(define (announce-output string) + (newline) (display string) (newline)) + +(define (user-print object) + (if (compound-procedure? object) + (display (list 'compound-procedure + (procedure-parameters object) + (procedure-body object) + '<procedure-env>)) + (display object))) + +;;;Following are commented out so as not to be evaluated when +;;; the file is loaded. +;;(define the-global-environment (setup-environment)) +;;(driver-loop) + +;; Added at Berkeley: +(define the-global-environment '()) + +(define (mce) + (set! the-global-environment (setup-environment)) + (driver-loop)) + diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/obj.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/obj.scm new file mode 100644 index 0000000..d4a9d7a --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/obj.scm @@ -0,0 +1,279 @@ +;;; obj.scm version 4.0 5/18/2000 +;;; -- implementation of the object-oriented syntax +;; By Matt Wright, based on a handout from MIT +;; Revised for STk by Brian Gaeke - removed scm and procedure->macro + +;;; Utilities + +;; MAKNAM: create a new symbol whose name is the concatenation of the +;; names of those in the symbol list SYMBOLS. +(define (maknam . symbols) + (string->symbol (apply string-append (map symbol->string symbols)))) + +;; ASK: send a message to an object + +; The dot in the first line of the definition of ASK, below, makes it +; take a variable number of arguments. The first argument is associated +; with the formal parameter OBJECT; the second with MESSAGE; any extra +; actual arguments are put in a list, and that list is associated with +; the formal parameter ARGS. (If there are only two actual args, then +; ARGS will be the empty list.) + +; APPLY takes two arguments, a procedure and a list, and applies the +; procedure to the things in the list, which are used as actual +; argument values. + +(define (ask object message . args) + (let ((method (object message))) + (if (method? method) + (apply method args) + (error "No method " message " in class " (cadr method))))) + +(define (no-method name) + (list 'no-method name)) + +(define (no-method? x) + (if (pair? x) + (eq? (car x) 'no-method) + #f)) + +(define (method? x) + (not (no-method? x))) + + +;; INSTANTIATE and INSTANTIATE-PARENT: Create an instance of a class + +; The difference is that only INSTANTIATE initializes the new object + +(define (instantiate class . arguments) + (let ((new-instance (apply (class 'instantiate) arguments))) + (ask new-instance 'initialize new-instance) + new-instance)) + +(define (instantiate-parent class . arguments) + (apply (class 'instantiate) arguments)) + +;; GET-METHOD: Send a message to several objects and return the first +;; method found (for multiple inheritance) + +(define (get-method give-up-name message . objects) + (if (null? objects) + (no-method give-up-name) + (let ((method ((car objects) message))) + (if (method? method) + method + (apply get-method (cons give-up-name + (cons message (cdr objects)) )))))) + + + +;; USUAL: Invoke a parent's method +;; Note: The 'send-usual-to-parent method is put in automatically by +;; define-class. + +(define-macro (usual . args) + `(ask dispatch 'send-usual-to-parent . ,args)) + + +;; DEFINE-CLASS: Create a new class. + +; DEFINE-CLASS is a special form. When you type (define-class body...) +; it's as if you typed (make-definitions (quote body...)). In other +; words, the argument to DEFINE-CLASS isn't evaluated. This makes sense +; because the argument isn't Scheme syntax, but rather is the special +; object-oriented programming language we're defining. +; Make-definitions transforms the OOP notation into a standard Scheme +; expression, then uses EVAL to evaluate the result. (You'll see EVAL +; again in chapter 4 with the metacircular evaluator.) + +; When you define a class named THING, for example, two global Scheme +; variables are created. The variable THING has as its value the +; procedure that represents the class. This procedure is invoked by +; INSTANTIATE to create instances of the class. A second variable, +; THING-DEFINITION, has as its value the text of the Scheme expression +; that defines THING. This text is used only by SHOW-CLASS, the +; procedure that lets you examine the result of the OOP-to-Scheme +; translation process. + +(define-macro (define-class . body) (make-definitions body)) + +(define (make-definitions form) + (let ((definition (translate form))) + (eval `(define ,(maknam (class-name form) '-definition) ',definition)) + (eval definition) + (list 'quote (class-name form)))) + +(define (show-class name) + (eval (maknam name '-definition)) ) + +; TRANSLATE does all the work of DEFINE-CLASS. +; The backquote operator (`) works just like regular quote (') except +; that expressions proceeded by a comma are evaluated. Also, expressions +; proceeded by ",@" evaluate to lists; the lists are inserted into the +; text without the outermost level of parentheses. + +(define (translate form) + (cond ((null? form) (error "Define-class: empty body")) + ((not (null? (obj-filter form (lambda (x) (not (pair? x)))))) + (error "Each argument to define-class must be a list")) + ((not (null? (extra-clauses form))) + (error "Unrecognized clause in define-class:" (extra-clauses form))) + (else + `(define ,(class-name form) + (let ,(class-var-bindings form) + (lambda (class-message) + (cond + ,@(class-variable-methods form) + ((eq? class-message 'instantiate) + (lambda ,(instantiation-vars form) + (let ((self '()) + ,@(parent-let-list form) + ,@(instance-vars-let-list form)) + (define (dispatch message) + (cond + ,(init-clause form) + ,(usual-clause form) + ,@(method-clauses form) + ,@(local-variable-methods form) + ,(else-clause form) )) + dispatch ))) + (else (error "Bad message to class" class-message)) ))))))) + +(define *legal-clauses* + '(instance-vars class-vars method default-method parent initialize)) + +(define (extra-clauses form) + (obj-filter (cdr form) + (lambda (x) (null? (member (car x) *legal-clauses*))))) + +(define class-name caar) + +(define (class-var-bindings form) + (let ((classvar-clause (find-a-clause 'class-vars form))) + (if (null? classvar-clause) + '() + (cdr classvar-clause) ))) + +(define instantiation-vars cdar) + +(define (parent-let-list form) + (let ((parent-clause (find-a-clause 'parent form))) + (if (null? parent-clause) + '() + (map (lambda (parent-and-args) + (list (maknam 'my- (car parent-and-args)) + (cons 'instantiate-parent parent-and-args))) + (cdr parent-clause))))) + +(define (instance-vars-let-list form) + (let ((instance-vars-clause (find-a-clause 'instance-vars form))) + (if (null? instance-vars-clause) + '() + (cdr instance-vars-clause)))) + +(define (init-clause form) + (define (parent-initialization form) + (let ((parent-clause (find-a-clause 'parent form))) + (if (null? parent-clause) + '() + (map + (lambda (parent-and-args) + `(ask ,(maknam 'my- (car parent-and-args)) 'initialize self) ) + (cdr parent-clause) )))) + (define (my-initialization form) + (let ((init-clause (find-a-clause 'initialize form))) + (if (null? init-clause) '() + (cdr init-clause)))) + (define (init-body form) + (append (parent-initialization form) + (my-initialization form) )) + + `((eq? message 'initialize) + (lambda (value-for-self) + (set! self value-for-self) + ,@(init-body form) ))) + +(define (variable-list var-type form) + (let ((clause (find-a-clause var-type form))) + (if (null? clause) + '() + (map car (cdr clause)) ))) + +(define (class-variable-methods form) + (cons `((eq? class-message 'class-name) (lambda () ',(class-name form))) + (map (lambda (variable) + `((eq? class-message ',variable) (lambda () ,variable))) + (variable-list 'class-vars form)))) + +(define (local-variable-methods form) + (cons `((eq? message 'class-name) (lambda () ',(class-name form))) + (map (lambda (variable) + `((eq? message ',variable) (lambda () ,variable))) + (append (cdr (car form)) + (variable-list 'instance-vars form) + (variable-list 'class-vars form))))) + +(define (method-clauses form) + (map + (lambda (method-defn) + (let ((this-message (car (cadr method-defn))) + (args (cdr (cadr method-defn))) + (body (cddr method-defn))) + `((eq? message ',this-message) + (lambda ,args ,@body)))) + (obj-filter (cdr form) (lambda (x) (eq? (car x) 'method))) )) + +(define (parent-list form) + (let ((parent-clause (find-a-clause 'parent form))) + (if (null? parent-clause) + '() + (map (lambda (class) (maknam 'my- class)) + (map car (cdr parent-clause)))))) + +(define (usual-clause form) + (let ((parent-clause (find-a-clause 'parent form))) + (if (null? parent-clause) + `((eq? message 'send-usual-to-parent) + (error "Can't use USUAL without a parent." ',(class-name form))) + `((eq? message 'send-usual-to-parent) + (lambda (message . args) + (let ((method (get-method ',(class-name form) + message + ,@(parent-list form)))) + (if (method? method) + (apply method args) + (error "No USUAL method" message ',(class-name form)) ))))))) + +(define (else-clause form) + (let ((parent-clause (find-a-clause 'parent form)) + (default-method (find-a-clause 'default-method form))) + (cond + ((and (null? parent-clause) (null? default-method)) + `(else (no-method ',(class-name form)))) + ((null? parent-clause) + `(else (lambda args ,@(cdr default-method)))) + ((null? default-method) + `(else (get-method ',(class-name form) message ,@(parent-list form))) ) + (else + `(else (let ((method (get-method ',(class-name form) + message + ,@(parent-list form)))) + (if (method? method) + method + (lambda args ,@(cdr default-method)) ))))))) + +(define (find-a-clause clause-name form) + (let ((clauses (obj-filter (cdr form) + (lambda (x) (eq? (car x) clause-name))))) + (cond ((null? clauses) '()) + ((null? (cdr clauses)) (car clauses)) + (else (error "Error in define-class: too many " + clause-name "clauses.")) ))) + +(define (obj-filter l pred) + (cond ((null? l) '()) + ((pred (car l)) + (cons (car l) (obj-filter (cdr l) pred))) + (else (obj-filter (cdr l) pred)))) + +(provide "obj") diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/old-obj.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/old-obj.scm new file mode 100644 index 0000000..cee6aae --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/old-obj.scm @@ -0,0 +1,277 @@ +;;; obj.scm version 3.0 1/9/95 +;;; -- implementation of the object-oriented syntax +;;; This version is supposed to work with Unix SCM, PC SCM, and Mac Gambit +;;; This version does class names right. +;;; Also, explicit methods come before instance variable methods +;; By Matt Wright, based on a handout from MIT + +;; ASK: send a message to an object + +; The dot in the first line of the definition of ASK, below, makes it +; take a variable number of arguments. The first argument is associated +; with the formal parameter OBJECT; the second with MESSAGE; any extra +; actual arguments are put in a list, and that list is associated with +; the formal parameter ARGS. (If there are only two actual args, then +; ARGS will be the empty list.) + +; APPLY takes two arguments, a procedure and a list, and applies the +; procedure to the things in the list, which are used as actual +; argument values. + +(define (ask object message . args) + (let ((method (object message))) + (if (method? method) + (apply method args) + (error "No method" message (cadr method))))) + +(define (no-method name) + (list 'no-method name)) + +(define (no-method? x) + (if (pair? x) + (eq? (car x) 'no-method) + #f)) + +(define (method? x) + (not (no-method? x))) + + +;; INSTANTIATE and INSTANTIATE-PARENT: Create an instance of a class + +; The difference is that only INSTANTIATE initializes the new object + +(define (instantiate class . arguments) + (let ((new-instance (apply (class 'instantiate) arguments))) + (ask new-instance 'initialize new-instance) + new-instance)) + +(define (instantiate-parent class . arguments) + (apply (class 'instantiate) arguments)) + +;; GET-METHOD: Send a message to several objects and return the first +;; method found (for multiple inheritance) + +(define (get-method give-up-name message . objects) + (if (null? objects) + (no-method give-up-name) + (let ((method ((car objects) message))) + (if (method? method) + method + (apply get-method (cons give-up-name + (cons message (cdr objects)) )))))) + + + +;; USUAL: Invoke a parent's method +;; Note: The 'send-usual-to-parent method is put in automatically by +;; define-class. + +(if scm? + (defmacro usual args `(ask dispatch 'send-usual-to-parent . ,args)) + (eval '(define-macro (usual . args) + `(ask dispatch 'send-usual-to-parent . ,args)))) + + +;; DEFINE-CLASS: Create a new class. + +; DEFINE-CLASS is a special form. When you type (define-class body...) +; it's as if you typed (make-definitions (quote body...)). In other +; words, the argument to DEFINE-CLASS isn't evaluated. This makes sense +; because the argument isn't Scheme syntax, but rather is the special +; object-oriented programming language we're defining. +; Make-definitions transforms the OOP notation into a standard Scheme +; expression, then uses EVAL to evaluate the result. (You'll see EVAL +; again in chapter 4 with the metacircular evaluator.) + +; When you define a class named THING, for example, two global Scheme +; variables are created. The variable THING has as its value the +; procedure that represents the class. This procedure is invoked by +; INSTANTIATE to create instances of the class. A second variable, +; THING-DEFINITION, has as its value the text of the Scheme expression +; that defines THING. This text is used only by SHOW-CLASS, the +; procedure that lets you examine the result of the OOP-to-Scheme +; translation process. + +(if scm? + (defmacro define-class body (make-definitions body)) + (eval '(define-macro (define-class . body) (make-definitions body)))) + +(define (make-definitions form) + (let ((definition (translate form))) + (eval `(define ,(word (class-name form) '-definition) ',definition)) + (eval definition) + (list 'quote (class-name form)))) + +(define (show-class name) + (eval (word name '-definition)) ) + +; TRANSLATE does all the work of DEFINE-CLASS. +; The backquote operator (`) works just like regular quote (') except +; that expressions proceeded by a comma are evaluated. Also, expressions +; proceeded by ",@" evaluate to lists; the lists are inserted into the +; text without the outermost level of parentheses. + +(define (translate form) + (cond ((null? form) (error "Define-class: empty body")) + ((not (null? (obj-filter form (lambda (x) (not (pair? x)))))) + (error "Each argument to define-class must be a list")) + ((not (null? (extra-clauses form))) + (error "Unrecognized clause in define-class:" (extra-clauses form))) + (else + `(define ,(class-name form) + (let ,(class-var-bindings form) + (lambda (class-message) + (cond + ,@(class-variable-methods form) + ((eq? class-message 'instantiate) + (lambda ,(instantiation-vars form) + (let ((self '()) + ,@(parent-let-list form) + ,@(instance-vars-let-list form)) + (define (dispatch message) + (cond + ,(init-clause form) + ,(usual-clause form) + ,@(method-clauses form) + ,@(local-variable-methods form) + ,(else-clause form) )) + dispatch ))) + (else (error "Bad message to class" class-message)) ))))))) + +(define *legal-clauses* + '(instance-vars class-vars method default-method parent initialize)) + +(define (extra-clauses form) + (obj-filter (cdr form) + (lambda (x) (not (member? (car x) *legal-clauses*))))) + +(define class-name caar) + +(define (class-var-bindings form) + (let ((classvar-clause (find-a-clause 'class-vars form))) + (if (null? classvar-clause) + '() + (cdr classvar-clause) ))) + +(define instantiation-vars cdar) + +(define (parent-let-list form) + (let ((parent-clause (find-a-clause 'parent form))) + (if (null? parent-clause) + '() + (map (lambda (parent-and-args) + (list (word 'my- (car parent-and-args)) + (cons 'instantiate-parent parent-and-args))) + (cdr parent-clause))))) + +(define (instance-vars-let-list form) + (let ((instance-vars-clause (find-a-clause 'instance-vars form))) + (if (null? instance-vars-clause) + '() + (cdr instance-vars-clause)))) + +(define (init-clause form) + (define (parent-initialization form) + (let ((parent-clause (find-a-clause 'parent form))) + (if (null? parent-clause) + '() + (map + (lambda (parent-and-args) + `(ask ,(word 'my- (car parent-and-args)) 'initialize self) ) + (cdr parent-clause) )))) + (define (my-initialization form) + (let ((init-clause (find-a-clause 'initialize form))) + (if (null? init-clause) '() + (cdr init-clause)))) + (define (init-body form) + (append (parent-initialization form) + (my-initialization form) )) + + `((eq? message 'initialize) + (lambda (value-for-self) + (set! self value-for-self) + ,@(init-body form) ))) + +(define (variable-list var-type form) + (let ((clause (find-a-clause var-type form))) + (if (null? clause) + '() + (map car (cdr clause)) ))) + +(define (class-variable-methods form) + (cons `((eq? class-message 'class-name) (lambda () ',(class-name form))) + (map (lambda (variable) + `((eq? class-message ',variable) (lambda () ,variable))) + (variable-list 'class-vars form)))) + +(define (local-variable-methods form) + (cons `((eq? message 'class-name) (lambda () ',(class-name form))) + (map (lambda (variable) + `((eq? message ',variable) (lambda () ,variable))) + (append (cdr (car form)) + (variable-list 'instance-vars form) + (variable-list 'class-vars form))))) + +(define (method-clauses form) + (map + (lambda (method-defn) + (let ((this-message (car (cadr method-defn))) + (args (cdr (cadr method-defn))) + (body (cddr method-defn))) + `((eq? message ',this-message) + (lambda ,args ,@body)))) + (obj-filter (cdr form) (lambda (x) (eq? (car x) 'method))) )) + +(define (parent-list form) + (let ((parent-clause (find-a-clause 'parent form))) + (if (null? parent-clause) + '() + (map (lambda (class) (word 'my- class)) + (map car (cdr parent-clause)))))) + +(define (usual-clause form) + (let ((parent-clause (find-a-clause 'parent form))) + (if (null? parent-clause) + `((eq? message 'send-usual-to-parent) + (error "Can't use USUAL without a parent." ',(class-name form))) + `((eq? message 'send-usual-to-parent) + (lambda (message . args) + (let ((method (get-method ',(class-name form) + message + ,@(parent-list form)))) + (if (method? method) + (apply method args) + (error "No USUAL method" message ',(class-name form)) ))))))) + +(define (else-clause form) + (let ((parent-clause (find-a-clause 'parent form)) + (default-method (find-a-clause 'default-method form))) + (cond + ((and (null? parent-clause) (null? default-method)) + `(else (no-method ',(class-name form)))) + ((null? parent-clause) + `(else (lambda args ,@(cdr default-method)))) + ((null? default-method) + `(else (get-method ',(class-name form) message ,@(parent-list form))) ) + (else + `(else (let ((method (get-method ',(class-name form) + message + ,@(parent-list form)))) + (if (method? method) + method + (lambda args ,@(cdr default-method)) ))))))) + +(define (find-a-clause clause-name form) + (let ((clauses (obj-filter (cdr form) + (lambda (x) (eq? (car x) clause-name))))) + (cond ((null? clauses) '()) + ((null? (cdr clauses)) (car clauses)) + (else (error "Error in define-class: too many " + clause-name "clauses.")) ))) + +(define (obj-filter l pred) + (cond ((null? l) '()) + ((pred (car l)) + (cons (car l) (obj-filter (cdr l) pred))) + (else (obj-filter (cdr l) pred)))) + diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/picture.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/picture.scm new file mode 100644 index 0000000..7369e42 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/picture.scm @@ -0,0 +1,129 @@ +;; Code for CS61A project 2 -- picture language + +(define (flipped-pairs painter) + (let ((painter2 (beside painter (flip-vert painter)))) + (below painter2 painter2))) + +(define (right-split painter n) + (if (= n 0) + painter + (let ((smaller (right-split painter (- n 1)))) + (beside painter (below smaller smaller))))) + +(define (corner-split painter n) + (if (= n 0) + painter + (let ((up (up-split painter (- n 1))) + (right (right-split painter (- n 1)))) + (let ((top-left (beside up up)) + (bottom-right (below right right)) + (corner (corner-split painter (- n 1)))) + (beside (below painter top-left) + (below bottom-right corner)))))) + +(define (square-limit painter n) + (let ((quarter (corner-split painter n))) + (let ((half (beside (flip-horiz quarter) quarter))) + (below (flip-vert half) half)))) + +(define (square-of-four tl tr bl br) + (lambda (painter) + (let ((top (beside (tl painter) (tr painter))) + (bottom (beside (bl painter) (br painter)))) + (below bottom top)))) + +(define (identity x) x) + +(define (flipped-pairs painter) + (let ((combine4 (square-of-four identity flip-vert + identity flip-vert))) + (combine4 painter))) + +;; or + +; (define flipped-pairs +; (square-of-four identity flip-vert identity flip-vert)) + +(define (square-limit painter n) + (let ((combine4 (square-of-four flip-horiz identity + rotate180 flip-vert))) + (combine4 (corner-split painter n)))) + +(define (frame-coord-map frame) + (lambda (v) + (add-vect + (origin-frame frame) + (add-vect (scale-vect (xcor-vect v) + (edge1-frame frame)) + (scale-vect (ycor-vect v) + (edge2-frame frame)))))) + +(define (segments->painter segment-list) + (lambda (frame) + (for-each + (lambda (segment) + (draw-line + ((frame-coord-map frame) (start-segment segment)) + ((frame-coord-map frame) (end-segment segment)))) + segment-list))) + +(define (draw-line v1 v2) + (penup) + (setxy (- (* (xcor-vect v1) 200) 100) + (- (* (ycor-vect v1) 200) 100)) + (pendown) + (setxy (- (* (xcor-vect v2) 200) 100) + (- (* (ycor-vect v2) 200) 100))) + +(define (transform-painter painter origin corner1 corner2) + (lambda (frame) + (let ((m (frame-coord-map frame))) + (let ((new-origin (m origin))) + (painter + (make-frame new-origin + (sub-vect (m corner1) new-origin) + (sub-vect (m corner2) new-origin))))))) + +(define (flip-vert painter) + (transform-painter painter + (make-vect 0.0 1.0) + (make-vect 1.0 1.0) + (make-vect 0.0 0.0))) + +(define (shrink-to-upper-right painter) + (transform-painter painter + (make-vect 0.5 0.5) + (make-vect 1.0 0.5) + (make-vect 0.5 1.0))) + +(define (rotate90 painter) + (transform-painter painter + (make-vect 1.0 0.0) + (make-vect 1.0 1.0) + (make-vect 0.0 0.0))) + +(define (squash-inwards painter) + (transform-painter painter + (make-vect 0.0 0.0) + (make-vect 0.65 0.35) + (make-vect 0.35 0.65))) + +(define (beside painter1 painter2) + (let ((split-point (make-vect 0.5 0.0))) + (let ((paint-left + (transform-painter painter1 + (make-vect 0.0 0.0) + split-point + (make-vect 0.0 1.0))) + (paint-right + (transform-painter painter2 + split-point + (make-vect 1.0 0.0) + (make-vect 0.5 1.0)))) + (lambda (frame) + (paint-left frame) + (paint-right frame))))) + +(define full-frame (make-frame (make-vect -0.5 -0.5) + (make-vect 2 0) + (make-vect 0 2))) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/pigl.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/pigl.scm new file mode 100644 index 0000000..74e028e --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/pigl.scm @@ -0,0 +1,10 @@ +(define (pigl wd) + (if (pl-done? wd) + (word wd 'ay) + (pigl (word (bf wd) (first wd))) ) ) + +(define (pl-done? wd) + (vowel? (first wd)) ) + +(define (vowel? letter) + (member? letter '(a e i o u)) ) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/prisoner.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/prisoner.scm new file mode 100644 index 0000000..c81e90c --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/prisoner.scm @@ -0,0 +1,133 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; OVERVIEW: +;; +;; +;; The PLAY-LOOP procedure takes as its arguments two prisoner's +;; dilemma strategies, and plays an iterated game of approximately +;; one hundred rounds. A strategy is a procedure that takes +;; two arguments: a history of the player's previous plays and +;; a history of the other player's previous plays. A history consists +;; of a sentence of C's and D's. Likewise, a strategy procedure +;; returns either the word C (for "cooperate") or D ("defect"). +;; We also need a way to find out the player's scores; GET-SCORES +;; takes two histories and computes the net score for one player. +;; +;; Note that we are inventing various types of objects: strategies, +;; histories, etc. Each type of thing has certain specified +;; properties. For example, a history is a sentence with zero or +;; more words, each of which is a C or D. To help us use these +;; objects, we write procedures that let us forget the details and +;; think about things in terms of histories and rounds, not in terms +;; of sentences and words. (For example, see GET-NTH-FROM-LAST-PLAY, +;; ADD-TO-HISTORY, etc.) +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(define (play-loop strategy1 strategy2) + + ;; returns final scores + + (define (play-loop-helper strat1 strat2 history1 history2 counter limit) + (if (= counter limit) + (final-scores history1 history2 limit) + (let ((result1 (strat1 history1 history2)) + (result2 (strat2 history2 history1))) + ;; note that the strategy's + ;; own history comes first + (play-loop-helper strat1 strat2 + (add-to-history result1 history1) + (add-to-history result2 history2) + (1+ counter) + limit)))) ; end of helper + + (play-loop-helper strategy1 strategy2 ; play-loop body + empty-history empty-history + 0 + (+ 90 (random 21)))) + + + +(define (final-scores history1 history2 num-of-rounds) + + ;; returns average score per round for the two histories + + (se (/ (get-scores history1 history2) num-of-rounds) + (/ (get-scores history2 history1) num-of-rounds) )) + + + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; Procedures about histories +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(define empty-history '()) + +(define empty-history? empty?) + +(define (add-to-history result history) + (se history result)) + +(define (last-play history) + (last history)) + +(define (previous-history history) + (bl history)) + +(define (get-nth-to-last-play n history) + (cond ((empty? history) '()) + ((= n 1) (last history)) + (else (get-nth-to-last-play (1- n) (butlast history))))) + + +(define (get-scores my-hist other-hist) + + ;; returns total score for first player + + (if (or (empty? my-hist) (empty? other-hist)) + 0 + (+ (get-score (first my-hist) (first other-hist)) + (get-scores (bf my-hist) (bf other-hist))))) + +(define (get-score my-play other-play) + + ;; returns the score of the first player for this round + + (let ((round (se my-play other-play))) + (cond ((equal? round '(C C)) 3) + ((equal? round '(C D)) 0) + ((equal? round '(D C)) 5) + ((equal? round '(D D)) 1) ))) + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; Some strategies. +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + +(define (all-defect my-history other-history) + 'D) + +(define (poor-trusting-fool my-history other-history) + 'C) + +(define (unforgiving my-history other-history) + (define (ever-defected? history) + (if (empty-history? history) + #f + (or (equal? (last-play history) 'D) + (ever-defected? (previous-history history))))) + (if (ever-defected? other-history) 'D 'C)) + +(define (tit-for-tat my-history other-history) + (if (empty-history? other-history) + 'C + (last-play other-history))) + +(define (random-strategy my-history other-history) + (nth (random 2) '(C D))) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/pronounce.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/pronounce.scm new file mode 100644 index 0000000..638e160 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/pronounce.scm @@ -0,0 +1,165 @@ +; How to pronounce cxr-family function names + +(define (say name) + (accent (pronounce name))) + +; PRONOUNCE takes a cxr-family word as argument and returns a sentence +; of syllables. + +(define (pronounce name) + (cond ((eq? name 'car) '(car)) + ((eq? name 'caar) '(cuh are)) + ((empty? name) '()) + ((eq? (first-two name) 'ca) (se 'caa (pronounce (bf-two name)))) + ((eq? (first name) 'c) (se 'cuh (pronounce (bf name)))) + ((eq? (last-three name) 'dar) (se (pronounce (bl-three name)) 'dar)) + ((eq? (last-two name) 'ar) (se (pronounce (bl-two name)) 'are)) + ((eq? (last-two name) 'dr) (se (pronounce (bl-two name)) 'der)) + ((eq? (first-two name) 'da) (se 'daa (pronounce (bf-two name)))) + ((eq? (first name) 'a) (se 'aa (pronounce (bf name)))) + (else (se 'de (pronounce (bf name)))))) + +; ACCENT takes a sentence of syllables (as returned by PRONOUNCE) and inserts +; an exclamation point at the emphasized syllable. I'm less sure that these +; rules are universally agreed to than for PRONOUNCE. + +; In particular, all my life I've said "CUH de der" for cddr, but apparently +; to be consistent I should be saying "cuh DE der." I think I have heard people +; say it that way, though, so I'm reluctant to special-case it like the first +; syllable of caar in PRONOUNCE. + +(define (accent syls) + (define (help prev rest) + (if (null? rest) + (se (word prev '!)) + (let ((winover (assoc prev '((caa . (daa dar de der)) + (cuh . (der)) + (aa . (dar de der)) + (daa . (aa are daa dar de der)) + (de . (de der)))))) + (if (member? (car rest) (cdr winover)) + (cons (word prev '!) rest) + (cons prev (help (car rest) (cdr rest))))))) + (if (null? (cdr syls)) + syls + (help (car syls) (cdr syls)))) + +; Utility functions to get a table of pronunciations of all names up to length n + +(define (table n) + (for-each (lambda (w) (print (list w (say w)))) (cr n))) + +(define (cr n) + (if (= n 0) + '() + (append (cr (- n 1)) + (map (lambda (w) (word 'c w 'r)) (cross n))))) + +(define (cross n) + (if (= n 0) + '("") + (let ((small (cross (- n 1)))) + (append (map (lambda (w) (word w 'a)) small) + (map (lambda (w) (word w 'd)) small))))) + +; Helper functions are obvious except to note that they test for too-short +; arguments so that we can make more specific tests first in PRONOUNCE above. +; ("More specific" means that we test for long substrings before short ones.) + +(define (first-two wd) + (if (< (count wd) 2) + wd + (word (first wd) (first (bf wd))))) + +(define (bf-two wd) + (if (< (count wd) 2) + "" + (bf (bf wd)))) + +(define (last-two wd) + (if (< (count wd) 2) + wd + (word (last (bl wd)) (last wd)))) + +(define (bl-two wd) + (if (< (count wd) 2) + "" + (bl (bl wd)))) + +(define (last-three wd) + (if (< (count wd) 3) + wd + (word (last (bl (bl wd))) (last (bl wd)) (last wd)))) + +(define (bl-three wd) + (if (< (count wd) 3) + "" + (bl (bl (bl wd))))) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Results: + +; > (table 5) +; (car (car)) +; (cdr (cuh! der)) +; (caar (cuh are!)) +; (cdar (cuh dar!)) +; (cadr (caa! der)) +; (cddr (cuh de! der)) +; (caaar (caa aa are!)) +; (cdaar (cuh daa! are)) +; (cadar (caa! dar)) +; (cddar (cuh de dar!)) +; (caadr (caa aa! der)) +; (cdadr (cuh daa! der)) +; (caddr (caa! de der)) +; (cdddr (cuh de! de der)) +; (caaaar (caa aa aa are!)) +; (cdaaar (cuh daa! aa are)) +; (cadaar (caa! daa are)) +; (cddaar (cuh de daa! are)) +; (caadar (caa aa! dar)) +; (cdadar (cuh daa! dar)) +; (caddar (caa! de dar)) +; (cdddar (cuh de! de dar)) +; (caaadr (caa aa aa! der)) +; (cdaadr (cuh daa! aa der)) +; (cadadr (caa! daa der)) +; (cddadr (cuh de daa! der)) +; (caaddr (caa aa! de der)) +; (cdaddr (cuh daa! de der)) +; (cadddr (caa! de de der)) +; (cddddr (cuh de! de de der)) +; (caaaaar (caa aa aa aa are!)) +; (cdaaaar (cuh daa! aa aa are)) +; (cadaaar (caa! daa aa are)) +; (cddaaar (cuh de daa! aa are)) +; (caadaar (caa aa daa! are)) +; (cdadaar (cuh daa! daa are)) +; (caddaar (caa! de daa are)) +; (cdddaar (cuh de! de daa are)) +; (caaadar (caa aa aa! dar)) +; (cdaadar (cuh daa! aa dar)) +; (cadadar (caa! daa dar)) +; (cddadar (cuh de daa! dar)) +; (caaddar (caa aa! de dar)) +; (cdaddar (cuh daa! de dar)) +; (cadddar (caa! de de dar)) +; (cddddar (cuh de! de de dar)) +; (caaaadr (caa aa aa aa! der)) +; (cdaaadr (cuh daa! aa aa der)) +; (cadaadr (caa! daa aa der)) +; (cddaadr (cuh de daa! aa der)) +; (caadadr (caa aa daa! der)) +; (cdadadr (cuh daa! daa der)) +; (caddadr (caa! de daa der)) +; (cdddadr (cuh de! de daa der)) +; (caaaddr (caa aa aa! de der)) +; (cdaaddr (cuh daa! aa de der)) +; (cadaddr (caa! daa de der)) +; (cddaddr (cuh de daa! de der)) +; (caadddr (caa aa! de de der)) +; (cdadddr (cuh daa! de de der)) +; (caddddr (caa! de de de der)) +; (cdddddr (cuh de! de de de der)) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/query.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/query.scm new file mode 100644 index 0000000..f103ac2 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/query.scm @@ -0,0 +1,667 @@ +;;;;QUERY SYSTEM FROM SECTION 4.4.4 OF +;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS + +;;;;Matches code in ch4.scm +;;;;Includes: +;;;; -- supporting code from 4.1, chapter 3, and instructor's manual +;;;; -- data base from Section 4.4.1 -- see microshaft-data-base below + +;;;;This file can be loaded into Scheme as a whole. +;;;;In order to run the query system, the Scheme must support streams. + +;;;;NB. PUT's are commented out and no top-level table is set up. +;;;;Instead use initialize-data-base (from manual), supplied in this file. + + +;;;SECTION 4.4.4.1 +;;;The Driver Loop and Instantiation + +(define input-prompt ";;; Query input:") +(define output-prompt ";;; Query results:") + +(define (query-driver-loop) + (prompt-for-input input-prompt) + (let ((q (query-syntax-process (read)))) + (cond ((assertion-to-be-added? q) + (add-rule-or-assertion! (add-assertion-body q)) + (newline) + (display "Assertion added to data base.") + (query-driver-loop)) + (else + (newline) + (display output-prompt) + ;; [extra newline at end] (announce-output output-prompt) + (display-stream + (stream-map + (lambda (frame) + (instantiate q + frame + (lambda (v f) + (contract-question-mark v)))) + (qeval q (singleton-stream '())))) + (query-driver-loop))))) + +(define (instantiate exp frame unbound-var-handler) + (define (copy exp) + (cond ((var? exp) + (let ((binding (binding-in-frame exp frame))) + (if binding + (copy (binding-value binding)) + (unbound-var-handler exp frame)))) + ((pair? exp) + (cons (copy (car exp)) (copy (cdr exp)))) + (else exp))) + (copy exp)) + + +;;;SECTION 4.4.4.2 +;;;The Evaluator + +(define (qeval query frame-stream) + (let ((qproc (get (type query) 'qeval))) + (if qproc + (qproc (contents query) frame-stream) + (simple-query query frame-stream)))) + +;;;Simple queries + +(define (simple-query query-pattern frame-stream) + (stream-flatmap + (lambda (frame) + (stream-append-delayed + (find-assertions query-pattern frame) + (delay (apply-rules query-pattern frame)))) + frame-stream)) + +;;;Compound queries + +(define (conjoin conjuncts frame-stream) + (if (empty-conjunction? conjuncts) + frame-stream + (conjoin (rest-conjuncts conjuncts) + (qeval (first-conjunct conjuncts) + frame-stream)))) + +;;(put 'and 'qeval conjoin) + + +(define (disjoin disjuncts frame-stream) + (if (empty-disjunction? disjuncts) + the-empty-stream + (interleave-delayed + (qeval (first-disjunct disjuncts) frame-stream) + (delay (disjoin (rest-disjuncts disjuncts) + frame-stream))))) + +;;(put 'or 'qeval disjoin) + +;;;Filters + +(define (negate operands frame-stream) + (stream-flatmap + (lambda (frame) + (if (stream-null? (qeval (negated-query operands) + (singleton-stream frame))) + (singleton-stream frame) + the-empty-stream)) + frame-stream)) + +;;(put 'not 'qeval negate) + +(define (lisp-value call frame-stream) + (stream-flatmap + (lambda (frame) + (if (execute + (instantiate + call + frame + (lambda (v f) + (error "Unknown pat var -- LISP-VALUE" v)))) + (singleton-stream frame) + the-empty-stream)) + frame-stream)) + +;;(put 'lisp-value 'qeval lisp-value) + +(define (execute exp) + (apply (eval (predicate exp)) ; (eval (...) user-initial-environment) + (args exp))) + +(define (always-true ignore frame-stream) frame-stream) + +;;(put 'always-true 'qeval always-true) + +;;;SECTION 4.4.4.3 +;;;Finding Assertions by Pattern Matching + +(define (find-assertions pattern frame) + (stream-flatmap (lambda (datum) + (check-an-assertion datum pattern frame)) + (fetch-assertions pattern frame))) + +(define (check-an-assertion assertion query-pat query-frame) + (let ((match-result + (pattern-match query-pat assertion query-frame))) + (if (eq? match-result 'failed) + the-empty-stream + (singleton-stream match-result)))) + +(define (pattern-match pat dat frame) + (cond ((eq? frame 'failed) 'failed) + ((equal? pat dat) frame) + ((var? pat) (extend-if-consistent pat dat frame)) + ((and (pair? pat) (pair? dat)) + (pattern-match (cdr pat) + (cdr dat) + (pattern-match (car pat) + (car dat) + frame))) + (else 'failed))) + +(define (extend-if-consistent var dat frame) + (let ((binding (binding-in-frame var frame))) + (if binding + (pattern-match (binding-value binding) dat frame) + (extend var dat frame)))) + +;;;SECTION 4.4.4.4 +;;;Rules and Unification + +(define (apply-rules pattern frame) + (stream-flatmap (lambda (rule) + (apply-a-rule rule pattern frame)) + (fetch-rules pattern frame))) + +(define (apply-a-rule rule query-pattern query-frame) + (let ((clean-rule (rename-variables-in rule))) + (let ((unify-result + (unify-match query-pattern + (conclusion clean-rule) + query-frame))) + (if (eq? unify-result 'failed) + the-empty-stream + (qeval (rule-body clean-rule) + (singleton-stream unify-result)))))) + +(define (rename-variables-in rule) + (let ((rule-application-id (new-rule-application-id))) + (define (tree-walk exp) + (cond ((var? exp) + (make-new-variable exp rule-application-id)) + ((pair? exp) + (cons (tree-walk (car exp)) + (tree-walk (cdr exp)))) + (else exp))) + (tree-walk rule))) + +(define (unify-match p1 p2 frame) + (cond ((eq? frame 'failed) 'failed) + ((equal? p1 p2) frame) + ((var? p1) (extend-if-possible p1 p2 frame)) + ((var? p2) (extend-if-possible p2 p1 frame)) ; {\em ; ***} + ((and (pair? p1) (pair? p2)) + (unify-match (cdr p1) + (cdr p2) + (unify-match (car p1) + (car p2) + frame))) + (else 'failed))) + +(define (extend-if-possible var val frame) + (let ((binding (binding-in-frame var frame))) + (cond (binding + (unify-match + (binding-value binding) val frame)) + ((var? val) ; {\em ; ***} + (let ((binding (binding-in-frame val frame))) + (if binding + (unify-match + var (binding-value binding) frame) + (extend var val frame)))) + ((depends-on? val var frame) ; {\em ; ***} + 'failed) + (else (extend var val frame))))) + +(define (depends-on? exp var frame) + (define (tree-walk e) + (cond ((var? e) + (if (equal? var e) + true + (let ((b (binding-in-frame e frame))) + (if b + (tree-walk (binding-value b)) + false)))) + ((pair? e) + (or (tree-walk (car e)) + (tree-walk (cdr e)))) + (else false))) + (tree-walk exp)) + +;;;SECTION 4.4.4.5 +;;;Maintaining the Data Base + +(define THE-ASSERTIONS the-empty-stream) + +(define (fetch-assertions pattern frame) + (if (use-index? pattern) + (get-indexed-assertions pattern) + (get-all-assertions))) + +(define (get-all-assertions) THE-ASSERTIONS) + +(define (get-indexed-assertions pattern) + (get-stream (index-key-of pattern) 'assertion-stream)) + +(define (get-stream key1 key2) + (let ((s (get key1 key2))) + (if s s the-empty-stream))) + +(define THE-RULES the-empty-stream) + +(define (fetch-rules pattern frame) + (if (use-index? pattern) + (get-indexed-rules pattern) + (get-all-rules))) + +(define (get-all-rules) THE-RULES) + +(define (get-indexed-rules pattern) + (stream-append + (get-stream (index-key-of pattern) 'rule-stream) + (get-stream '? 'rule-stream))) + +(define (add-rule-or-assertion! assertion) + (if (rule? assertion) + (add-rule! assertion) + (add-assertion! assertion))) + +(define (add-assertion! assertion) + (store-assertion-in-index assertion) + (let ((old-assertions THE-ASSERTIONS)) + (set! THE-ASSERTIONS + (cons-stream assertion old-assertions)) + 'ok)) + +(define (add-rule! rule) + (store-rule-in-index rule) + (let ((old-rules THE-RULES)) + (set! THE-RULES (cons-stream rule old-rules)) + 'ok)) + +(define (store-assertion-in-index assertion) + (if (indexable? assertion) + (let ((key (index-key-of assertion))) + (let ((current-assertion-stream + (get-stream key 'assertion-stream))) + (put key + 'assertion-stream + (cons-stream assertion + current-assertion-stream)))))) + +(define (store-rule-in-index rule) + (let ((pattern (conclusion rule))) + (if (indexable? pattern) + (let ((key (index-key-of pattern))) + (let ((current-rule-stream + (get-stream key 'rule-stream))) + (put key + 'rule-stream + (cons-stream rule + current-rule-stream))))))) + +(define (indexable? pat) + (or (constant-symbol? (car pat)) + (var? (car pat)))) + +(define (index-key-of pat) + (let ((key (car pat))) + (if (var? key) '? key))) + +(define (use-index? pat) + (constant-symbol? (car pat))) + +;;;SECTION 4.4.4.6 +;;;Stream operations + +(define (stream-append-delayed s1 delayed-s2) + (if (stream-null? s1) + (force delayed-s2) + (cons-stream + (stream-car s1) + (stream-append-delayed (stream-cdr s1) delayed-s2)))) + +(define (interleave-delayed s1 delayed-s2) + (if (stream-null? s1) + (force delayed-s2) + (cons-stream + (stream-car s1) + (interleave-delayed (force delayed-s2) + (delay (stream-cdr s1)))))) + +(define (stream-flatmap proc s) + (flatten-stream (stream-map proc s))) + +(define (flatten-stream stream) + (if (stream-null? stream) + the-empty-stream + (interleave-delayed + (stream-car stream) + (delay (flatten-stream (stream-cdr stream)))))) + + +(define (singleton-stream x) + (cons-stream x the-empty-stream)) + + +;;;SECTION 4.4.4.7 +;;;Query syntax procedures + +(define (type exp) + (if (pair? exp) + (car exp) + (error "Unknown expression TYPE" exp))) + +(define (contents exp) + (if (pair? exp) + (cdr exp) + (error "Unknown expression CONTENTS" exp))) + +(define (assertion-to-be-added? exp) + (eq? (type exp) 'assert!)) + +(define (add-assertion-body exp) + (car (contents exp))) + +(define (empty-conjunction? exps) (null? exps)) +(define (first-conjunct exps) (car exps)) +(define (rest-conjuncts exps) (cdr exps)) + +(define (empty-disjunction? exps) (null? exps)) +(define (first-disjunct exps) (car exps)) +(define (rest-disjuncts exps) (cdr exps)) + +(define (negated-query exps) (car exps)) + +(define (predicate exps) (car exps)) +(define (args exps) (cdr exps)) + + +(define (rule? statement) + (tagged-list? statement 'rule)) + +(define (conclusion rule) (cadr rule)) + +(define (rule-body rule) + (if (null? (cddr rule)) + '(always-true) + (caddr rule))) + +(define (query-syntax-process exp) + (map-over-symbols expand-question-mark exp)) + +(define (map-over-symbols proc exp) + (cond ((pair? exp) + (cons (map-over-symbols proc (car exp)) + (map-over-symbols proc (cdr exp)))) + ((symbol? exp) (proc exp)) + (else exp))) + +(define (expand-question-mark symbol) + (let ((chars (symbol->string symbol))) + (if (string=? (substring chars 0 1) "?") + (list '? + (string->symbol + (substring chars 1 (string-length chars)))) + symbol))) + +(define (var? exp) + (tagged-list? exp '?)) + +(define (constant-symbol? exp) (symbol? exp)) + +(define rule-counter 0) + +(define (new-rule-application-id) + (set! rule-counter (+ 1 rule-counter)) + rule-counter) + +(define (make-new-variable var rule-application-id) + (cons '? (cons rule-application-id (cdr var)))) + +(define (contract-question-mark variable) + (string->symbol + (string-append "?" + (if (number? (cadr variable)) + (string-append (symbol->string (caddr variable)) + "-" + (number->string (cadr variable))) + (symbol->string (cadr variable)))))) + + +;;;SECTION 4.4.4.8 +;;;Frames and bindings +(define (make-binding variable value) + (cons variable value)) + +(define (binding-variable binding) + (car binding)) + +(define (binding-value binding) + (cdr binding)) + + +(define (binding-in-frame variable frame) + (assoc variable frame)) + +(define (extend variable value frame) + (cons (make-binding variable value) frame)) + + +;;;;From Section 4.1 + +(define (tagged-list? exp tag) + (if (pair? exp) + (eq? (car exp) tag) + false)) + +(define (prompt-for-input string) + (newline) (newline) (display string) (newline)) + + +;;;;Stream support from Chapter 3 + +(define (stream-map proc s) + (if (stream-null? s) + the-empty-stream + (cons-stream (proc (stream-car s)) + (stream-map proc (stream-cdr s))))) + +(define (stream-for-each proc s) + (if (stream-null? s) + 'done + (begin (proc (stream-car s)) + (stream-for-each proc (stream-cdr s))))) + +(define (display-stream s) + (stream-for-each display-line s)) +(define (display-line x) + (newline) + (display x)) + +(define (stream-filter pred stream) + (cond ((stream-null? stream) the-empty-stream) + ((pred (stream-car stream)) + (cons-stream (stream-car stream) + (stream-filter pred + (stream-cdr stream)))) + (else (stream-filter pred (stream-cdr stream))))) + +(define (stream-append s1 s2) + (if (stream-null? s1) + s2 + (cons-stream (stream-car s1) + (stream-append (stream-cdr s1) s2)))) + +(define (interleave s1 s2) + (if (stream-null? s1) + s2 + (cons-stream (stream-car s1) + (interleave s2 (stream-cdr s1))))) + +;;;;Table support from Chapter 3, Section 3.3.3 (local tables) + +(define (make-table) + (let ((local-table (list '*table*))) + (define (lookup key-1 key-2) + (let ((subtable (assoc key-1 (cdr local-table)))) + (if subtable + (let ((record (assoc key-2 (cdr subtable)))) + (if record + (cdr record) + false)) + false))) + (define (insert! key-1 key-2 value) + (let ((subtable (assoc key-1 (cdr local-table)))) + (if subtable + (let ((record (assoc key-2 (cdr subtable)))) + (if record + (set-cdr! record value) + (set-cdr! subtable + (cons (cons key-2 value) + (cdr subtable))))) + (set-cdr! local-table + (cons (list key-1 + (cons key-2 value)) + (cdr local-table))))) + 'ok) + (define (dispatch m) + (cond ((eq? m 'lookup-proc) lookup) + ((eq? m 'insert-proc!) insert!) + (else (error "Unknown operation -- TABLE" m)))) + dispatch)) + +;;;; From instructor's manual + +(define get '()) + +(define put '()) + +(define (initialize-data-base rules-and-assertions) + (define (deal-out r-and-a rules assertions) + (cond ((null? r-and-a) + (set! THE-ASSERTIONS (list->stream assertions)) + (set! THE-RULES (list->stream rules)) + 'done) + (else + (let ((s (query-syntax-process (car r-and-a)))) + (cond ((rule? s) + (store-rule-in-index s) + (deal-out (cdr r-and-a) + (cons s rules) + assertions)) + (else + (store-assertion-in-index s) + (deal-out (cdr r-and-a) + rules + (cons s assertions)))))))) + (let ((operation-table (make-table))) + (set! get (operation-table 'lookup-proc)) + (set! put (operation-table 'insert-proc!))) + (put 'and 'qeval conjoin) + (put 'or 'qeval disjoin) + (put 'not 'qeval negate) + (put 'lisp-value 'qeval lisp-value) + (put 'always-true 'qeval always-true) + (deal-out rules-and-assertions '() '())) + +;; Do following to reinit the data base from microshaft-data-base +;; in Scheme (not in the query driver loop) +;; (initialize-data-base microshaft-data-base) + +(define microshaft-data-base + '( +;; from section 4.4.1 +(address (Bitdiddle Ben) (Slumerville (Ridge Road) 10)) +(job (Bitdiddle Ben) (computer wizard)) +(salary (Bitdiddle Ben) 60000) + +(address (Hacker Alyssa P) (Cambridge (Mass Ave) 78)) +(job (Hacker Alyssa P) (computer programmer)) +(salary (Hacker Alyssa P) 40000) +(supervisor (Hacker Alyssa P) (Bitdiddle Ben)) + +(address (Fect Cy D) (Cambridge (Ames Street) 3)) +(job (Fect Cy D) (computer programmer)) +(salary (Fect Cy D) 35000) +(supervisor (Fect Cy D) (Bitdiddle Ben)) + +(address (Tweakit Lem E) (Boston (Bay State Road) 22)) +(job (Tweakit Lem E) (computer technician)) +(salary (Tweakit Lem E) 25000) +(supervisor (Tweakit Lem E) (Bitdiddle Ben)) + +(address (Reasoner Louis) (Slumerville (Pine Tree Road) 80)) +(job (Reasoner Louis) (computer programmer trainee)) +(salary (Reasoner Louis) 30000) +(supervisor (Reasoner Louis) (Hacker Alyssa P)) + +(supervisor (Bitdiddle Ben) (Warbucks Oliver)) + +(address (Warbucks Oliver) (Swellesley (Top Heap Road))) +(job (Warbucks Oliver) (administration big wheel)) +(salary (Warbucks Oliver) 150000) + +(address (Scrooge Eben) (Weston (Shady Lane) 10)) +(job (Scrooge Eben) (accounting chief accountant)) +(salary (Scrooge Eben) 75000) +(supervisor (Scrooge Eben) (Warbucks Oliver)) + +(address (Cratchet Robert) (Allston (N Harvard Street) 16)) +(job (Cratchet Robert) (accounting scrivener)) +(salary (Cratchet Robert) 18000) +(supervisor (Cratchet Robert) (Scrooge Eben)) + +(address (Aull DeWitt) (Slumerville (Onion Square) 5)) +(job (Aull DeWitt) (administration secretary)) +(salary (Aull DeWitt) 25000) +(supervisor (Aull DeWitt) (Warbucks Oliver)) + +(can-do-job (computer wizard) (computer programmer)) +(can-do-job (computer wizard) (computer technician)) + +(can-do-job (computer programmer) + (computer programmer trainee)) + +(can-do-job (administration secretary) + (administration big wheel)) + +(rule (lives-near ?person-1 ?person-2) + (and (address ?person-1 (?town . ?rest-1)) + (address ?person-2 (?town . ?rest-2)) + (not (same ?person-1 ?person-2)))) + +(rule (same ?x ?x)) + +(rule (wheel ?person) + (and (supervisor ?middle-manager ?person) + (supervisor ?x ?middle-manager))) + +(rule (outranked-by ?staff-person ?boss) + (or (supervisor ?staff-person ?boss) + (and (supervisor ?staff-person ?middle-manager) + (outranked-by ?middle-manager ?boss)))) +)) + + +;;; Added at Berkeley: + +(define (query) + (initialize-data-base '()) + (query-driver-loop)) + +(define (aa query) + (add-rule-or-assertion! + (add-assertion-body + (query-syntax-process (list 'assert! query))))) + +(initialize-data-base '()) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/ref-man.txt b/js/games/nluqo.github.io/~bh/61a-pages/Lib/ref-man.txt new file mode 100644 index 0000000..76bfb85 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/ref-man.txt @@ -0,0 +1,148 @@ +Reference Manual for the OOP Language + +There are only three procedures that you need to use: define-class, +which defines a class; instantiate, which takes a class as its argument +and returns an instance of the class; and ask, which asks an object to do +something. Here are the explanations of the procedures: + + +ASK: (ask object message . bigit args) + +Ask gets a method from object corresponding to message. If the object +has such a method, invoke it with the given args; otherwise it's an error. + + +INSTANTIATE: (instantiate class . arguments) + +Instantiate creates a new instance of the given class, initializes it, +and returns it. To initialize a class, instantiate runs the initialize +clauses of all the parent classes of the object and then runs the +initialize clause of this class. + +The extra arguments to instantiate give the values of the new object's +instantiation variables. So if you say + +(define-class (account balance) ...) + +then saying + +(define my-acct (instantiate account 100)) + +will cause my-acct's balance variable to be bound to 100. + + +DEFINE-CLASS: + +(define-class (class-name args...) clauses...) + +This defines a new class named class-name. The instantiation arguments +for this class are args. (See the explanation of instantiate above.) + +The rest of the arguments to define-class are various clauses of the +following types. All clauses are optional. You can have any number of +method clauses, in any order. + + +(METHOD (message arguments...) body) + +A method clause gives the class a method corresponding to the message, +with the given arguments and body. A class definition may contain any +number of method clauses. You invoke methods with ask. For example, say +there's an object with a + +(method (add x y) (+ x y)) + +clause. Then (ask object 'add 2 5) returns 7. + +Inside a method, the variable self is bound to the object whose method this +is. (Note that self might be an instance of a child class of the class in +which the method is defined.) A method defined within a particular class +has access to the instantiation variables, instance variables, and class +variables that are defined within the same class, but does not have access +to variables defined in parent or child classes. (This is similar to the +scope rules for variables within procedures outside of the OOP system.) + +Any method that is usable within a given object can invoke any other such +method by invoking (ask self message). However, if a method wants to invoke +the method of the same name within a parent class, it must instead ask for +that explicitly by saying + +(usual message args...) + +where message is the name of the method you want and args... are the +arguments to the method. + + +(INSTANCE-VARS (var1 value1) (var2 value2) ...) + +Instance-vars sets up local state variables var1, var2, etc. Each +instance of the class will have its own private set of variables with +these names. These are visible inside the bodies of the methods and the +initialization code within the same class definition. The initial values +of the variables are calculated when an instance is created by evaluating +the expressions value1, value2, etc. There can be any number of +variables. If there is no instance-vars clause then the instances of +this class won't have any instance variables. It is an error for a class +definition to contain more than one instance-vars clause. + + +(CLASS-VARS (var1 value1) (var2 value2) ...) + + +Class-vars sets up local state variables var1, var2, etc. The class has +only one set of variables with these names, shared by every instance of +the class. (Compare the instance-vars clause described above.) These +variables are visible inside the bodies of the methods and the +initialization code within the same class definition. The initial values +of the variables are calculated when the class is defined by evaluating +the expressions value1, value2, etc. There can be any number of +variables. If there is no class-vars clause then the class won't have +any class variables. It is an error for a class definition to contain +more than one class-vars clause. + + +(PARENT (parent1 args...) (parent2 args...)) + +Parent defines the parents of a class. The args are the arguments used +to instantiate the parent objects. For example, let's say that the +rectangle class has two arguments: height and width: + +(define-class (rectangle height width) ...) + +A square is a kind of rectangle; the height and width of the square's +rectangle are both the side-length of the square: + +(define-class (square side-length) + (parent (rectangle side-length side-length)) + ...) + +When an object class doesn't have an explicit method for a message it +receives, it looks for methods of that name in the definitions of the +parent classes, in the order they appear in the parent clause. The method +that gets invoked is from the first parent class that recognizes the +message. + +A method can invoke a parent's method of the same name with usual; see +the notes on the method clause above. + + +(DEFAULT-METHOD body) + +A default-method clause specifies the code that an object should execute +if it receives an unrecognized message (i.e., a message that does not +name a method in this class or any of its superclasses). When the body is +executed, the variable message is bound to the message, and the variable +args is bound to a list of the additional arguments to ask. + + +(INITIALIZE body) + +The body of the initialize clause contains code that is executed whenever +an instance of this class is created. + +If the class has parents, their initialize code gets executed before the +initialize clause in the class itself. If the class has two or more +parents, their initialize code is executed in the order that they appear +in the parent clause. + + diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/resist.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/resist.scm new file mode 100644 index 0000000..42c0e6b --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/resist.scm @@ -0,0 +1,79 @@ +(define (make-resistor resistance) + (attach-type 'resistor resistance)) + +(define (resistor? ckt) + (eq? (type ckt) 'resistor)) + +(define (make-series ckt1 ckt2) + (attach-type 'series (list ckt1 ckt2))) + +(define (series? ckt) + (eq? (type ckt) 'series)) + +(define (make-parallel ckt1 ckt2) + (attach-type 'parallel (list ckt1 ckt2))) + +(define (parallel? ckt) + (eq? (type ckt) 'parallel)) + + +(define (resistance ckt) + (cond ((resistor? ckt) + (resistance-resistor (contents ckt))) + ((parallel? ckt) + (resistance-parallel (contents ckt))) + ((series? ckt) + (resistance-series (contents ckt))))) + +(define (conductance ckt) + (cond ((resistor? ckt) + (conductance-resistor (contents ckt))) + ((parallel? ckt) + (conductance-parallel (contents ckt))) + ((series? ckt) + (conductance-series (contents ckt))))) + + +(define (resistance-resistor resistor) + resistor) + +(define (conductance-resistor resistor) + (/ 1 (resistance-resistor resistor))) + + +(define (resistance-series ckt) + (+ (resistance (left-branch ckt)) + (resistance (right-branch ckt)))) + +(define (conductance-series ckt) + (/ 1 (resistance-series ckt))) + +(define (conductance-parallel ckt) + (+ (conductance (left-branch ckt)) + (conductance (right-branch ckt)))) + +(define (resistance-parallel ckt) + (/ 1 (conductance-parallel ckt))) + + + +(define left-branch car) +(define right-branch cadr) +(define attach-type cons) +(define type car) +(define contents cdr) + + + +(define (repeated f n) + (lambda (x) + (if (= n 0) + x + ((repeated f (-1+ n)) (f x))))) + +(define (L-extend base series-part parallel-part) + (make-series series-part (make-parallel parallel-part base))) + +(define (ladder-extension stages base series-part parallel-part) + ((repeated (lambda (x) (L-extend x series-part parallel-part)) stages) + base)) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/review b/js/games/nluqo.github.io/~bh/61a-pages/Lib/review new file mode 100644 index 0000000..63a4837 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/review @@ -0,0 +1,54 @@ +CS 60A Course summary + + +You aren't expected to understand this yet, but keep it for reference +during the semester and see if it starts to make sense! + +------------------------------ + +ABSTRACTION: + voluntary submission to a discipline + in order to gain expressive power + +------------------------------ + +1. FUNCTIONAL PROGRAMMING + focus: repeatable input-output behavior + composition of functions to layer complexity + hidden: side effect mechanisms (assignment) + internal control structure of procedures + +2. DATA ABSTRACTION + focus: semantic view of data aggregates + hidden: actual representation in memory + +3. OBJECT ORIENTED PROGRAMMING + focus: time-varying local state + metaphor of many autonomous actors + hidden: scheduling of interactions within the one computer + procedural methods within an object + +4. STREAMS + focus: metaphor of parallel operations on data aggregates + signal processing model of computation + hidden: actual sequence of events in the computation + +5. PROGRAMMING LANGUAGES + focus: provide a metaphor for computation + embody common elements of large groups of problems + hidden: technology-specific implementation medium + storage allocation, etc. + +6. LOGIC PROGRAMMING + focus: declarative representation of knowledge + inference rules + hidden: inference algorithm + + +Note: each of these abstractions can be approached "from above," focusing +on the view of computing that the abstraction provides, or "from below," +focusing on the techniques by which the abstraction is implemented. In +the metacircular evaluator we emphasize the view from below, since we've +been working all along with the view from above. In the query evaluator +we emphasize the view from above, barely mentioning the implementation +techniques. In our discussion of object programming both views are used. diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/rps.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/rps.scm new file mode 100644 index 0000000..70c2ed9 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/rps.scm @@ -0,0 +1,49 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; The PLAY-LOOP procedure takes as its arguments two game +;; strategies, and plays an iterated game of 25 rounds. +;; A strategy is a procedure that takes two arguments: +;; a history of this player's previous plays and +;; a history of the other player's previous plays. The strategy +;; returns one of the words ROCK, PAPER, or SCISSORS. A history +;; is a list of previous plays, most recent first. +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(define (play-loop strat0 strat1) + (define (play-loop-iter strat0 strat1 + history0 history1 + score0 score1 + rounds) + (cond ((= rounds 0) (list score0 score1)) + (else (YOU-WRITE-THIS-PART)) )) + (play-loop-iter strat0 strat1 '() '() 0 0 25) ) + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; This predicate procedure takes two plays as arguments and +;; returns TRUE if the first beats the second. +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(define (beats? play0 play1) + (cond ((equal? play0 'paper) (equal? play1 'rock)) + ((equal? play0 'scissors) (equal? play1 'paper)) + ((equal? play0 'rock) (equal? play1 'scissors)) )) + +;; A sampler of strategies + +(define (tit-for-tat my-history other-history) + (if (empty? my-history) + 'rock ;; arbitrary -- could be a random choice here + (first other-history) )) + +(define (random-strategy my-history other-history) + (nth (random 3) '(rock paper scissors)) ) + +(define (heavy-metal my-history other-history) + 'scissors) + +(define (hard-rock my-history other-history) + 'rock) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/scheme1.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/scheme1.scm new file mode 100644 index 0000000..09be791 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/scheme1.scm @@ -0,0 +1,216 @@ +;; Simple evaluator for Scheme without DEFINE, using substitution model. +;; Version 1: No DEFINE, only primitive names are global. + +;; The "read-eval-print loop" (REPL): + +(define (scheme-1) + (display "Scheme-1: ") + (flush) + (print (eval-1 (read))) + (scheme-1)) + +;; Two important procedures: +;; EVAL-1 takes an expression and returns its value. +;; APPLY-1 takes a procedure and a list of actual argument values, and +;; calls the procedure. +;; They have these names to avoid conflict with STk's EVAL and APPLY, +;; which have similar meanings. + +;; Comments on EVAL-1: + +;; There are four basic expression types in Scheme: +;; 1. self-evaluating (a/k/a constant) expressions: numbers, #t, etc. +;; 2. symbols (variables) +;; 3. special forms (in this evaluator, just QUOTE, IF, and LAMBDA) +;; 4. procedure calls (can call a primitive or a LAMBDA-generated procedure) + +;; 1. The value of a constant is itself. Unlike real Scheme, an STk +;; procedure is here considered a constant expression. You can't type in +;; procedure values, but the value of a global variable can be a procedure, +;; and that value might get substituted for a parameter in the body of a +;; higher-order function such as MAP, so the evaluator has to be ready to +;; see a built-in procedure as an "expression." Therefore, the procedure +;; CONSTANT? includes a check for (PROCEDURE? EXP). + +;; 2. In the substitution model, we should never actually evaluate a *local* +;; variable name, because we should have substituted the actual value for +;; the parameter name before evaluating the procedure body. + +;; In this simple evaluator, there is no DEFINE, and so the only *global* +;; symbols are the ones representing primitive procedures. We cheat a little +;; by using STk's EVAL to get the values of these variables. + +;; 3. The value of the expression (QUOTE FOO) is FOO -- the second element of +;; the expression. + +;; To evaluate the expression (IF A B C) we first evaluate A; then, if A is +;; true, we evaluate B; if A is false, we evaluate C. + +;; The value of a LAMBDA expression is the expression itself. There is no +;; work to do until we actually call the procedure. (This won't be true +;; when we write a more realistic interpreter that handles more Scheme +;; features, but it works in the substitution model.) + +;; 4. To evaluate a procedure call, we recursively evaluate all the +;; subexpressions. We call APPLY-1 to handle the actual procedure invocation. + +(define (eval-1 exp) + (cond ((constant? exp) exp) + ((symbol? exp) (eval exp)) ; use underlying Scheme's EVAL + ((quote-exp? exp) (cadr exp)) + ((if-exp? exp) + (if (eval-1 (cadr exp)) + (eval-1 (caddr exp)) + (eval-1 (cadddr exp)))) + ((lambda-exp? exp) exp) + ((define-exp? exp) + (eval (list 'define (cadr exp) (maybe-quote (eval-1 (caddr exp)))))) + ((pair? exp) (apply-1 (eval-1 (car exp)) ; eval the operator + (map eval-1 (cdr exp)))) + (else (error "bad expr: " exp)))) + + +;; Comments on APPLY-1: + +;; There are two kinds of procedures: primitive and LAMBDA-created. + +;; We recognize a primitive procedure using the PROCEDURE? predicate in +;; the underlying STk interpreter. + +;; If the procedure isn't primitive, then it must be LAMBDA-created. +;; In this interpreter (but not in later, more realistic ones), the value +;; of a LAMBDA expression is the expression itself. So (CADR PROC) is +;; the formal parameter list, and (CADDR PROC) is the expression in the +;; procedure body. + +;; To call the procedure, we must substitute the actual arguments for +;; the formal parameters in the body; the result of this substitution is +;; an expression which we can then evaluate with EVAL-1. + +(define (apply-1 proc args) + (cond ((procedure? proc) ; use underlying Scheme's APPLY + (apply proc args)) + ((lambda-exp? proc) + (eval-1 (substitute (caddr proc) ; the body + (cadr proc) ; the formal parameters + args ; the actual arguments + '()))) ; bound-vars, see below + (else (error "bad proc: " proc)))) + + +;; Some trivial helper procedures: + +(define (constant? exp) + (or (number? exp) (boolean? exp) (string? exp) (procedure? exp))) + +(define (exp-checker type) + (lambda (exp) (and (pair? exp) (eq? (car exp) type)))) + +(define quote-exp? (exp-checker 'quote)) +(define if-exp? (exp-checker 'if)) +(define lambda-exp? (exp-checker 'lambda)) +(define define-exp? (exp-checker 'define)) + + +;; SUBSTITUTE substitutes actual arguments for *free* references to the +;; corresponding formal parameters. For example, given the expression +;; +;; ((lambda (x y) +;; ((lambda (x) (+ x y)) +;; (* x y))) +;; 5 8) +;; +;; the body of the procedure we're calling is +;; +;; ((lambda (x) (+ x y)) +;; (* x y)) +;; +;; and we want to substitute 5 for X and 8 for Y, but the result should be +;; +;; ((lambda (x) (+ x 8)) +;; (* 5 8)) +;; +;; and *NOT* +;; +;; ((lambda (5) (+ 5 8)) +;; (* 5 8)) +;; +;; The X in (* X Y) is a "free reference," but the X in (LAMBDA (X) (+ X Y)) +;; is a "bound reference." +;; +;; To make this work, in its recursive calls, SUBSTITUTE keeps a list of +;; bound variables in the current subexpression -- ones that shouldn't be +;; substituted for -- in its argument BOUND. This argument is the empty +;; list in the top-level call to SUBSTITUTE from APPLY-1. + +;; Another complication is that when an argument value isn't a self-evaluating +;; expression, we actually want to substitute the value *quoted*. For example, +;; consider the expression +;; +;; ((lambda (x) (first x)) 'foo) +;; +;; The actual argument value is FOO, but we want the result of the +;; substitution to be +;; +;; (first 'foo) +;; +;; and not +;; +;; (first foo) +;; +;; because what we're going to do with this expression is try to evaluate +;; it, and FOO would be an unbound variable. + +;; There is a strangeness in MAYBE-QUOTE, which must handle the +;; case of a primitive procedure as the actual argument value; these +;; procedures shouldn't be quoted. + +(define (substitute exp params args bound) + (cond ((constant? exp) exp) + ((symbol? exp) + (if (memq exp bound) + exp + (lookup exp params args))) + ((quote-exp? exp) exp) + ((lambda-exp? exp) + (list 'lambda + (cadr exp) + (substitute (caddr exp) params args (append bound (cadr exp))))) + (else (map (lambda (subexp) (substitute subexp params args bound)) + exp)))) + +(define (lookup name params args) + (cond ((null? params) name) + ((eq? name (car params)) (maybe-quote (car args))) + (else (lookup name (cdr params) (cdr args))))) + +(define (maybe-quote value) + (cond ((lambda-exp? value) value) + ((constant? value) value) + ((procedure? value) value) ; real Scheme primitive procedure + (else (list 'quote value)))) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Sample evaluation, computing factorial of 5: + +; Scheme-1: ((lambda (n) +; ((lambda (f) (f f n)) +; (lambda (f n) +; (if (= n 0) +; 1 +; (* n (f f (- n 1))) )) )) +; 5) +; 120 + +;; Sample evaluation, using a primitive as argument to MAP: + +; Scheme-1: ((lambda (f n) +; ((lambda (map) (map map f n)) +; (lambda (map f n) +; (if (null? n) +; '() +; (cons (f (car n)) (map map f (cdr n))) )) )) +; first +; '(the rain in spain)) +; (t r i s) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/scmset b/js/games/nluqo.github.io/~bh/61a-pages/Lib/scmset new file mode 100644 index 0000000..47f40aa --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/scmset @@ -0,0 +1,2 @@ +setenv SCM_INIT_PATH ~cs61a/scm/Init.scm +setenv SCHEME_LIBRARY_PATH ~cs61a/slib/ diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/serial.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/serial.scm new file mode 100644 index 0000000..52a4597 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/serial.scm @@ -0,0 +1,21 @@ +(define (make-serializer) + (let ((mutex (make-mutex))) + (lambda (p) + (define (serialized-p . args) + (mutex 'acquire) + (let ((val (apply p args))) + (mutex 'release) + val)) + serialized-p))) + +(define (make-mutex) + (let ((cell (list false))) + (define (the-mutex m) + (cond ((eq? m 'acquire) + (if (test-and-set! cell) + (the-mutex 'acquire))) + ((eq? m 'release) (clear! cell)))) + the-mutex)) + +(define (clear! cell) + (set-car! cell false)) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/small-world.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/small-world.scm new file mode 100644 index 0000000..dcd6bd1 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/small-world.scm @@ -0,0 +1,28 @@ +;;; small-world.scm +;;; Miniature game world for debugging the CS61A adventure game project. +;;; You can load this instead of adv-world.scm, and reload it quickly +;;; whenever you change a class. + +;;; How to use this file: +;;; If, for example, your person class doesn't work, and you do something +;;; like (define Matt (instantiate person 'Matt)), and then fix your +;;; person class definition, Matt is still bound to the faulty person +;;; object from before. However, reloading this file whenever you +;;; change something should redefine everything in your world with the +;;; currently loaded (i.e. most recent) versions of your classes. + +(define 61A-Lab (instantiate place '61A-Lab)) +(define Lounge (instantiate place 'Lounge)) +(can-go 61A-Lab 'up Lounge) +(can-go Lounge 'down 61A-Lab) +;;; Hopefully you'll see more of the world than this in real life +;;; while you're doing the project! + +(define homework-box (instantiate thing 'homework-box)) +(ask 61A-Lab 'appear homework-box) + +(define Coke (instantiate thing 'Coke)) +(ask Lounge 'appear Coke) + +(define laba (instantiate person 'Lab-assistant 61A-Lab)) + diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/tables.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/tables.scm new file mode 100644 index 0000000..010b712 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/tables.scm @@ -0,0 +1,20 @@ +;;; Section 3.3.3 -- Tables + +;;; One-dimensional tables + +(define (lookup key table) + (let ((record (assoc key (cdr table)))) + (if (not record) + #f + (cdr record)))) + +(define (insert! key value table) + (let ((record (assoc key (cdr table)))) + (if (not record) + (set-cdr! table + (cons (cons key value) (cdr table))) + (set-cdr! record value))) + 'ok) + +(define (make-table) + (list '*table*)) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/test.logo b/js/games/nluqo.github.io/~bh/61a-pages/Lib/test.logo new file mode 100644 index 0000000..bb2e15a --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/test.logo @@ -0,0 +1,39 @@ +to second :thing +op first bf :thing +end + +to twice :thing +pr :thing +pr :thing +end + +to pigl :word +if vowelp first :word [op word :word "ay] +op pigl word bf :word first :word +end + +to vowelp :let +op memberp :let "aeiou +end + +to piglatin :sent +if emptyp :sent [op []] +op fput pigl first :sent piglatin bf :sent +end + +to factorial :n +if :n=0 [output 1] +output :n * factorial :n-1 +end + +to memberp :thing :list +if emptyp :list [op "false] +if equalp :thing first :list [op "true] +op memberp :thing bf :list +end + +to repeat :num :instr +if :num=0 [stop] +run :instr +repeat :num-1 :instr +end diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/tri.l b/js/games/nluqo.github.io/~bh/61a-pages/Lib/tri.l new file mode 100644 index 0000000..90e3918 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/tri.l @@ -0,0 +1,94 @@ +;; Program to solve triangles given partial information. + +;; Form of invocation is (triangle side1 side2 side3 angle1 angle2 angle3) +;; where each argument is zero if the corresponding side or angle is +;; unknown. Angles are entered in degrees (but converted internally +;; to radians). Angle1 is opposite side1, etc. The program must be +;; given at least three knowns, at least one of which must be a side. + +;; First, here are some useful definitions from trig class: + +(define pi 3.14159) + +(define (square x) (* x x)) + +(define (degrees rad) (* 180 (/ rad pi))) + +(define (radians deg) (* pi (/ deg 180))) + +(define (arcsin a) (atan a (sqrt (- 1 (square a))))) + +(define (arccos a) (atan (sqrt (- 1 (square a))) a)) + +;; This is the top-level procedure + +(define (triangle a b c alpha beta gamma) + + (define (triangle-rad a b c alpha beta gamma) + +;; The strategy is to keep increasing the number of sides we know, and at +;; the end to make sure we know the angles too. Here are some local +;; procedures to count how many sides we know. They are predicates that +;; return true if we know at least so many sides (or angles sometimes). + + (define (havethreesides) (not (or (= a 0) (= b 0) (= c 0)))) + + (define (havetwo a b c) + (cond ((= a 0) (not (or (= b 0) (= c 0)))) + (else (not (and (= b 0) (= c 0)))))) + (define (havetwosides) (havetwo a b c)) + (define (havetwoangles) (havetwo alpha beta gamma)) + + (define (haveone a b c) + (not (and (= a 0) (= b 0) (= c 0)))) + (define (haveoneside) (haveone a b c)) + (define (haveoneangle) (haveone alpha beta gamma)) + +;; If we know all three sides we can use the law of cosines to find +;; any angles that might be missing. + + (define (solvethreesides) + (define (lawcosangle side1 side2 hyp) + (arccos (/ (- (+ (square side1) (square side2)) (square hyp)) + (* 2 side1 side2)))) + (define (sidesbad) + (define (tri-ineq a b c) (> (+ a b) c)) + (not (and (tri-ineq a b c) (tri-ineq a c b) (tri-ineq b c a)))) + + (cond ((sidesbad) "Your sides fail the triangle inequality.") + ((= alpha 0) + (triangle-rad a b c (lawcosangle b c a) beta gamma)) + ((= beta 0) + (triangle-rad a b c alpha (lawcosangle a c b) gamma)) + ((= gamma 0) + (triangle-rad a b c alpha beta (lawcosangle a b c))) + (else + (list a b c (degrees alpha) (degrees beta) (degrees gamma))))) + +;; [You don't know about the procedure "list" yet, but it lets us return +;; more than one number in a single result.] + +;; We invoke this procedure if we know two sides: + + (define (solvetwosides) + +;; It'll make life easier if we rearrange things so that side C is unknown. + + (cond ((not (haveoneangle)) "Must know at least three values.") + ((= a 0) (triangle-rad b c a beta gamma alpha)) + ((= b 0) (triangle-rad a c b alpha gamma beta)) + (else "YOU FILL IN THE REST!"))) + +;; This is the executable body of triangle-rad: + + (cond ((havethreesides) (solvethreesides)) + ((havetwosides) (solvetwosides)) + ((haveoneside) (solveoneside)) + (else "Must know at least one side."))) + +;; This is the executable body of triangle. + + (cond ((> (+ alpha beta gamma) 180) + "Your angles add up to more than 180.") + (else (triangle-rad a b c + (radians alpha) (radians beta) (radians gamma))))) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/turkey b/js/games/nluqo.github.io/~bh/61a-pages/Lib/turkey new file mode 100644 index 0000000..1def834 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/turkey @@ -0,0 +1,50 @@ + MORTON THOMPSON'S TURKEY STUFFING + +From the book "Joe, the Wounded Tennis Player" by Morton Thompson, +adapted by Craig Claiborne in the NY Times. + +1 apple, peeled, cored, and diced +1 orange, peeled, seeded, and diced +1 no. 2 can crushed pineapple, drained +rind of one lemon, grated +3 tablespoons chopped preserved ginger +2 five-oz cans water chestnuts, drained and coarsely chopped +2 teaspoons powdered mustard +2 teaspoons caraway seeds +3 teaspoons celery seeds +2 teaspoons poppy seeds +2 1/2 teaspoons oregano +1 crushed bay leaf +1/2 teaspoon mace +1/4 teaspoon ground cloves +1/2 teaspoon turmeric +1/2 teaspoon marjoram +1/2 teaspoon summer savory +1 tablespoon poultry seasoning +3/4 teaspoon sage +3/4 teaspoon thyme +1/2 teaspoon basil +1/2 teaspoon chili powder +1/4 cup finely chopped parsley +5 cloves garlic, finely minced +6 large ribs celery, chopped +4 large onions, peeled and chopped +5 dashes Tabasco sauce +1 tablespoon salt +6 cups fresh bread crumbs, or 3 packages bread crumbs +3/4 pound ground veal +1/2 pound ground fresh pork or sausage +1/4 pound butter +1 16-pound to 20-pound turkey + +In one bowl, combine the diced apple, orange, crushed pineapple, +lemon rind, ginger, and chopped water chestnuts. Mix well. + +In a second (huge) bowl, combine all the herbs and spices, the +parsley, garlic, celery, onions, Tabasco, and salt. Toss well. + +Add the remaining ingredients and the contents of the first bowl. +Blend everything well with the fingers. Stuff the turkey and +skewer it. Roast according to any standard recipe. Any leftover +stuffing may be frozen and used later for chickens, ducks, or +another turkey. [Or just cooked on the stove and mixed in --BH] diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/twenty-one.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/twenty-one.scm new file mode 100644 index 0000000..0337c84 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/twenty-one.scm @@ -0,0 +1,42 @@ +(define (twenty-one strategy) + (define (play-dealer customer-hand dealer-hand-so-far rest-of-deck) + (cond ((> (best-total dealer-hand-so-far) 21) 1) + ((< (best-total dealer-hand-so-far) 17) + (play-dealer customer-hand + (se dealer-hand-so-far (first rest-of-deck)) + (bf rest-of-deck))) + ((< (best-total customer-hand) (best-total dealer-hand-so-far)) -1) + ((= (best-total customer-hand) (best-total dealer-hand-so-far)) 0) + (else 1))) + + (define (play-customer customer-hand-so-far dealer-up-card rest-of-deck) + (cond ((> (best-total customer-hand-so-far) 21) -1) + ((strategy customer-hand-so-far dealer-up-card) + (play-customer (se customer-hand-so-far (first rest-of-deck)) + dealer-up-card + (bf rest-of-deck))) + (else + (play-dealer customer-hand-so-far + (se dealer-up-card (first rest-of-deck)) + (bf rest-of-deck))))) + + (let ((deck (make-deck))) + (play-customer (se (first deck) (first (bf deck))) + (first (bf (bf deck))) + (bf (bf (bf deck))))) ) + +(define (make-ordered-deck) + (define (make-suit s) + (map (lambda (rank) (word rank s)) '(A 2 3 4 5 6 7 8 9 10 J Q K)) ) + (se (make-suit 'H) (make-suit 'S) (make-suit 'D) (make-suit 'C)) ) + +(define (make-deck) + (define (shuffle deck size) + (define (move-card in out which) + (if (= which 0) + (se (first in) (shuffle (se (bf in) out) (-1+ size))) + (move-card (bf in) (se (first in) out) (-1+ which)) )) + (if (= size 0) + deck + (move-card deck '() (random size)) )) + (shuffle (make-ordered-deck) 52) ) diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lib/vambeval.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lib/vambeval.scm new file mode 100644 index 0000000..360800a --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lib/vambeval.scm @@ -0,0 +1,511 @@ +;;;;Nondeterministic evaluator +;;;;Different from the one in chapter 4 of SICP, in that it's based on the +;;;; vanilla metacircular evaluator, rather than on the analyzing one. + +;;;;This file can be loaded into Scheme as a whole. +;;;;Then you can initialize and start the evaluator by evaluating +;;;; the expression (mce). + +;;;from section 4.1.4 -- must precede def of metacircular apply +(define apply-in-underlying-scheme apply) + +;;;SECTION 4.1.1 + +(define (ambeval exp env succeed fail) + (cond ((self-evaluating? exp) (succeed exp fail)) + ((variable? exp) + (succeed (lookup-variable-value exp env) + fail)) + ((quoted? exp) (succeed (text-of-quotation exp) fail)) + ((assignment? exp) (eval-assignment exp env succeed fail)) + ((definition? exp) (eval-definition exp env succeed fail)) + ((if? exp) (eval-if exp env succeed fail)) + ((lambda? exp) + (succeed (make-procedure (lambda-parameters exp) + (lambda-body exp) + env) + fail)) + ((begin? exp) + (eval-sequence (begin-actions exp) env succeed fail)) + ((cond? exp) (ambeval (cond->if exp) env succeed fail)) + ((let? exp) (ambeval (let->combination exp) env succeed fail)) ;** + ((amb? exp) (eval-amb exp env succeed fail)) ;** + ((application? exp) + (eval-application exp env succeed fail)) + (else + (error "Unknown expression type -- EVAL" exp)))) + +(define (eval-application exp env succeed fail) + (ambeval (operator exp) + env + (lambda (proc fail2) + (get-args (operands exp) + env + (lambda (args fail3) + (execute-application proc args succeed fail3)) + fail2)) + fail)) + +(define (get-args exps env succeed fail) + (if (null? exps) + (succeed '() fail) + (ambeval (car exps) + env + (lambda (arg fail2) + (get-args (cdr exps) + env + (lambda (args fail3) + (succeed (cons arg args) + fail3)) + fail2)) + fail))) + +(define (execute-application procedure arguments succeed fail) + (cond ((primitive-procedure? procedure) + (succeed (apply-primitive-procedure procedure arguments) fail)) + ((compound-procedure? procedure) + (eval-sequence + (procedure-body procedure) + (extend-environment + (procedure-parameters procedure) + arguments + (procedure-environment procedure)) + succeed + fail)) + (else + (error + "Unknown procedure type -- APPLY" procedure)))) + + +(define (eval-if exp env succeed fail) + (ambeval (if-predicate exp) + env + (lambda (pred-value fail2) + (if (true? pred-value) + (ambeval (if-consequent exp) + env + succeed + fail2) + (ambeval (if-alternative exp) + env + succeed + fail2))) + fail)) + +(define (eval-sequence exps env succeed fail) + (define (loop first-exp rest-exps succeed fail) + (if (null? rest-exps) + (ambeval first-exp env succeed fail) + (ambeval first-exp + env + (lambda (first-value fail2) + (loop (car rest-exps) (cdr rest-exps) succeed fail2)) + fail))) + (if (null? exps) + (error "Empty sequence") + (loop (car exps) (cdr exps) succeed fail))) + +(define (eval-definition exp env succeed fail) + (ambeval (definition-value exp) + env + (lambda (val fail2) + (define-variable! (definition-variable exp) val env) + (succeed 'ok fail2)) + fail)) + +(define (eval-assignment exp env succeed fail) + (ambeval (assignment-value exp) + env + (lambda (val fail2) + (let* ((var (assignment-variable exp)) + (old-value + (lookup-variable-value var env))) + (set-variable-value! var val env) + (succeed 'ok + (lambda () + (set-variable-value! var old-value env) + (fail2))))) + fail)) + + +(define (eval-amb exp env succeed fail) + (define (try-next choices) + (if (null? choices) + (fail) + (ambeval (car choices) + env + succeed + (lambda () + (try-next (cdr choices)))))) + (try-next (amb-choices exp))) + + +;;;SECTION 4.1.2 + +(define (self-evaluating? exp) + (cond ((number? exp) true) + ((string? exp) true) + ((boolean? exp) true) + (else false))) + +(define (quoted? exp) + (tagged-list? exp 'quote)) + +(define (text-of-quotation exp) (cadr exp)) + +(define (tagged-list? exp tag) + (if (pair? exp) + (eq? (car exp) tag) + false)) + +(define (variable? exp) (symbol? exp)) + +(define (assignment? exp) + (tagged-list? exp 'set!)) + +(define (assignment-variable exp) (cadr exp)) + +(define (assignment-value exp) (caddr exp)) + + +(define (definition? exp) + (tagged-list? exp 'define)) + +(define (definition-variable exp) + (if (symbol? (cadr exp)) + (cadr exp) + (caadr exp))) + +(define (definition-value exp) + (if (symbol? (cadr exp)) + (caddr exp) + (make-lambda (cdadr exp) + (cddr exp)))) + +(define (lambda? exp) (tagged-list? exp 'lambda)) + +(define (lambda-parameters exp) (cadr exp)) +(define (lambda-body exp) (cddr exp)) + +(define (make-lambda parameters body) + (cons 'lambda (cons parameters body))) + + +(define (if? exp) (tagged-list? exp 'if)) + +(define (if-predicate exp) (cadr exp)) + +(define (if-consequent exp) (caddr exp)) + +(define (if-alternative exp) + (if (not (null? (cdddr exp))) + (cadddr exp) + 'false)) + +(define (make-if predicate consequent alternative) + (list 'if predicate consequent alternative)) + + +(define (begin? exp) (tagged-list? exp 'begin)) + +(define (begin-actions exp) (cdr exp)) + +(define (last-exp? seq) (null? (cdr seq))) +(define (first-exp seq) (car seq)) +(define (rest-exps seq) (cdr seq)) + +(define (sequence->exp seq) + (cond ((null? seq) seq) + ((last-exp? seq) (first-exp seq)) + (else (make-begin seq)))) + +(define (make-begin seq) (cons 'begin seq)) + + +(define (application? exp) (pair? exp)) +(define (operator exp) (car exp)) +(define (operands exp) (cdr exp)) + +(define (no-operands? ops) (null? ops)) +(define (first-operand ops) (car ops)) +(define (rest-operands ops) (cdr ops)) + + +(define (cond? exp) (tagged-list? exp 'cond)) + +(define (cond-clauses exp) (cdr exp)) + +(define (cond-else-clause? clause) + (eq? (cond-predicate clause) 'else)) + +(define (cond-predicate clause) (car clause)) + +(define (cond-actions clause) (cdr clause)) + +(define (cond->if exp) + (expand-clauses (cond-clauses exp))) + +(define (expand-clauses clauses) + (if (null? clauses) + 'false ; no else clause + (let ((first (car clauses)) + (rest (cdr clauses))) + (if (cond-else-clause? first) + (if (null? rest) + (sequence->exp (cond-actions first)) + (error "ELSE clause isn't last -- COND->IF" + clauses)) + (make-if (cond-predicate first) + (sequence->exp (cond-actions first)) + (expand-clauses rest)))))) + +(define (amb? exp) (tagged-list? exp 'amb)) +(define (amb-choices exp) (cdr exp)) + +;;;SECTION 4.1.3 + +(define (true? x) + (not (eq? x false))) + +(define (false? x) + (eq? x false)) + + +(define (make-procedure parameters body env) + (list 'procedure parameters body env)) + +(define (compound-procedure? p) + (tagged-list? p 'procedure)) + + +(define (procedure-parameters p) (cadr p)) +(define (procedure-body p) (caddr p)) +(define (procedure-environment p) (cadddr p)) + + +(define (enclosing-environment env) (cdr env)) + +(define (first-frame env) (car env)) + +(define the-empty-environment '()) + +(define (make-frame variables values) + (cons variables values)) + +(define (frame-variables frame) (car frame)) +(define (frame-values frame) (cdr frame)) + +(define (add-binding-to-frame! var val frame) + (set-car! frame (cons var (car frame))) + (set-cdr! frame (cons val (cdr frame)))) + +(define (extend-environment vars vals base-env) + (if (= (length vars) (length vals)) + (cons (make-frame vars vals) base-env) + (if (< (length vars) (length vals)) + (error "Too many arguments supplied" vars vals) + (error "Too few arguments supplied" vars vals)))) + +(define (lookup-variable-value var env) + (define (env-loop env) + (define (scan vars vals) + (cond ((null? vars) + (env-loop (enclosing-environment env))) + ((eq? var (car vars)) + (car vals)) + (else (scan (cdr vars) (cdr vals))))) + (if (eq? env the-empty-environment) + (error "Unbound variable" var) + (let ((frame (first-frame env))) + (scan (frame-variables frame) + (frame-values frame))))) + (env-loop env)) + +(define (set-variable-value! var val env) + (define (env-loop env) + (define (scan vars vals) + (cond ((null? vars) + (env-loop (enclosing-environment env))) + ((eq? var (car vars)) + (set-car! vals val)) + (else (scan (cdr vars) (cdr vals))))) + (if (eq? env the-empty-environment) + (error "Unbound variable -- SET!" var) + (let ((frame (first-frame env))) + (scan (frame-variables frame) + (frame-values frame))))) + (env-loop env)) + +(define (define-variable! var val env) + (let ((frame (first-frame env))) + (define (scan vars vals) + (cond ((null? vars) + (add-binding-to-frame! var val frame)) + ((eq? var (car vars)) + (set-car! vals val)) + (else (scan (cdr vars) (cdr vals))))) + (scan (frame-variables frame) + (frame-values frame)))) + +;;;SECTION 4.1.4 + +(define (setup-environment) + (let ((initial-env + (extend-environment (primitive-procedure-names) + (primitive-procedure-objects) + the-empty-environment))) + (define-variable! 'true true initial-env) + (define-variable! 'false false initial-env) + initial-env)) + +;[do later] (define the-global-environment (setup-environment)) + +(define (primitive-procedure? proc) + (tagged-list? proc 'primitive)) + +(define (primitive-implementation proc) (cadr proc)) + +(define primitive-procedures + (list (list 'car car) + (list 'cdr cdr) + (list 'cons cons) + (list 'null? null?) + (list '+ +) + (list '- -) + (list '* *) + (list '/ /) + (list '= =) + (list 'list list) + (list 'append append) + (list 'equal? equal?) +;; more primitives + )) + +(define (primitive-procedure-names) + (map car + primitive-procedures)) + +(define (primitive-procedure-objects) + (map (lambda (proc) (list 'primitive (cadr proc))) + primitive-procedures)) + +;[moved to start of file] (define apply-in-underlying-scheme apply) + +(define (apply-primitive-procedure proc args) + (apply-in-underlying-scheme + (primitive-implementation proc) args)) + + + +(define input-prompt ";;; Amb-Eval input:") +(define output-prompt ";;; Amb-Eval value:") + +(define (driver-loop) + (define (internal-loop try-again) + (prompt-for-input input-prompt) + (let ((input (read))) + (if (eq? input 'try-again) + (try-again) + (begin + (newline) + (display ";;; Starting a new problem ") + (ambeval input + the-global-environment + ;; ambeval success + (lambda (val next-alternative) + (announce-output output-prompt) + (user-print val) + (internal-loop next-alternative)) + ;; ambeval failure + (lambda () + (announce-output + ";;; There are no more values of") + (user-print input) + (driver-loop))))))) + (internal-loop + (lambda () + (newline) + (display ";;; There is no current problem") + (driver-loop)))) + + +(define (prompt-for-input string) + (newline) (newline) (display string) (newline)) + +(define (announce-output string) + (newline) (display string) (newline)) + +(define (user-print object) + (if (compound-procedure? object) + (display (list 'compound-procedure + (procedure-parameters object) + (procedure-body object) + '<procedure-env>)) + (display object))) + +;;; Support for Let (as noted in footnote 56, p.428) + +(define (let? exp) (tagged-list? exp 'let)) +(define (let-bindings exp) (cadr exp)) +(define (let-body exp) (cddr exp)) + +(define (let-var binding) (car binding)) +(define (let-val binding) (cadr binding)) + +(define (make-combination operator operands) (cons operator operands)) + +(define (let->combination exp) + ;;make-combination defined in earlier exercise + (let ((bindings (let-bindings exp))) + (make-combination (make-lambda (map let-var bindings) + (let-body exp)) + (map let-val bindings)))) + +;; A longer list of primitives -- suitable for running everything in 4.3 +;; Overrides the list in ch4-mceval.scm +;; Has Not to support Require; various stuff for code in text (including +;; support for Prime?); integer? and sqrt for exercise code; +;; eq? for ex. solution + +(define primitive-procedures + (list (list 'car car) + (list 'cdr cdr) + (list 'cons cons) + (list 'null? null?) + (list 'list list) + (list 'append append) + (list 'memq memq) + (list 'member member) + (list 'not not) + (list '+ +) + (list '- -) + (list '* *) + (list '= =) + (list '> >) + (list '>= >=) + (list 'abs abs) + (list 'remainder remainder) + (list 'integer? integer?) + (list 'sqrt sqrt) + (list 'eq? eq?) + (list 'equal? equal?) + (list 'pair? pair?) +;; more primitives + )) + +;;;Following are commented out so as not to be evaluated when +;;; the file is loaded. +;;(define the-global-environment (setup-environment)) +;;(driver-loop) + +;; Added at Berkeley: +(define the-global-environment '()) + +(define (mce) + (set! the-global-environment (setup-environment)) + (ambeval '(define (require p) (if (not p) (amb))) + the-global-environment + (lambda (a b) #t) + (lambda () #t)) + (driver-loop)) + |