;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for
|
|
- Millicent Jenkins
- 6 years ago
- Views:
Transcription
1 Page 1/11 (require (lib "trace")) Allow tracing to be turned on and off (define tracing #f) Define a string to hold the error messages created during syntax checking (define error msg ""); Used for fancy error messages (define type ""); (define set error message (lambda (message) (set! error msg message))) Environment related function (define make new environment (lambda (bindings old env) (cons bindings old env))) (define lookup in environment (lambda (name env trace?) (if (eq? env null) #f (if trace? (output (format "Looking for ~a in ~a" name (pretty environment env)) # t #t)) (let ((pair (assv name (car env)))) pair (lookup in environment name (cdr env) trace?))))))) (define set in environment (lambda (name env value trace?) (if (eq? env null) #f (if trace? (output (format "Looking for ~a in ~a" name (pretty environment env)) # t #t)) (let ((pair (assv name (car env)))) (set cdr! pair value) (output (format "Setting ~a to ~a!" name value) #t #t) #t) (set in environment name (cdr env) value trace?))))))) (define pretty environment (lambda (env) (list (map (lambda (n v) (list n := v)) (map car (car env)) (map (lambda (x) (if (list? x) (list λ (cadr x)...) x)) (map cdr (car env)))) > parent))) Define a list of primitives (define prim (cons (list (cons + +) (cons ) (cons / /) (cons * *) (cons = =) (cons > >) (cons < <)) null)) is primitive? Determines if e is a primitive by looking it up in the primitive environment prim (define is primitive? (lambda (e) (assv e (car prim)))) Define the global environment (define global env (cons () prim)) Define indentation and output routines for the output for the evaluator (define set error msg (lambda (msg) (set! error msg msg) #f)) (define indent 0) (define output (lambda (str indent? newline?) (if tracing (if indent? (do indent)) (display str) (if newline? ))))) (define do indent (lambda () (letrec ((myindent (lambda (n) (if (> n 0) (display " ") (myindent ( n 2))))))) (myindent indent)))) =============================== ===== RULE IMPLEMENTATION ===== =============================== Page 2/11 Initialization Rule (init rule) Clears the global environment. If you want the global environment to hold an y values at start up you can places them here ; ) old init rule (define init rule (lambda () (set! env (list (cons fact (lambda (n) (if (= n 0) 1 (* n (fact ( n 1))))))))))) new init rule An environment is a pair (e,n), e is the environment bindings, and n is the parent environment. 1/6
2 Page 3/11 the global environment does not have a parent environment. (define init rule (lambda () (set! global env (cons () prim)))) Lambda Rule (lambda rule) As discussed a lambda form has an IMPLICIT begin in its body, so why not just put an EXPLICIT one in there and let the Begin Rule take care of business. Ex. (lambda (x) x) => (lambda (x) x)) ==========> old Lambda Rule (define lambda rule (lambda (lambda form) (let ((l (list lambda (cadr lambda form) (cons begin (cddr lambda form) )))) (output (format "=> ~a [Lambda Rule]" l) #t #t) l))) ==========> new Lambda Rule A lambda (function abstraction) must now carry with it an environment; the e nvironment belonging to a lambda form is the environment which were the present one w hen the lambda rule was called. The lambda rule must thus take in an environment as well. We will represent a lambda as a pair (l,e), where l is the actual lambda for m, and e is the associated environment, i.e., the environment to which the static l ink of any frame evaluating this lambda form must point. (define lambda rule (lambda (lambda form env) (let ((l (list lambda (cadr lambda form) (cons begin (cddr lambda form)) e nv))) (output (format "=> ~a [Lambda Rule]" (list λ (cadr l) (caddr l))) #t #t) l))) Number Rule (number rule) This rule is obsolete; it is being replaced by the Literal Rule; this allows us to use strings and boolean literals too. Though we do not have a way to comp are anything by numbers unless we implement string equal?, and and or. (define number rule (lambda (number) (output (format "=> ~a [Number Rule]" number) #f #t) number))) Literal Rule (literal rule) See the description for the Number Rule (define literal rule (lambda (literal) (output (format "=> ~a [Literal Rule]" literal) #f #t) literal))) Name Rule (name rule) Accepts a name and returns the value from the environment. Page 4/11 ==========> old Name Rule (define name rule (lambda (name) (let ((pair (assv name env))) (output (format "=> ~a [Name Rule]" (cdr pair)) #f #t) (cdr pair)) (let ((pair (assv name prim))) (output (format "=> ~a [Name Rule]" (cdr pair)) #f #t) (cdr pair)) (display (format "reference to undefined identifier: ~a" na me)) (loop)))))))) ==========> new Name Rule A call to the name rule must now include an environment, and instead of doin g an assv call on the global environment, we use lookup in environment to look through the cha in of environments. (define name rule (lambda (name env) (if tracing ) (let ((pair (lookup in environment name env #t))) (output (format "=> ~a [Name Rule]" (if (and (list? (cdr pair)) (eq? l ambda (cadr pair))) (list λ (caddr pair) (cadd dr pair)) (cdr pair))) #t #t) (cdr pair)) (display (format "reference to undefined identifier: ~a" name)) (loop)))))) Primitive Rule (primitive rule) Accepts a primitive and a list of arguments, and applies the primitive to the list of arguments using apply. Note, and and or (which we have not implemented cannot be primitives as Scheme does not allow the use of and and or with apply ; ( (define primitive rule (lambda (primitive arguments) (let ((result (apply primitive arguments))) (output (format "=> ~a [Primitive Rule]" result) #t #t) result))) 2/6
3 Page 5/11 Application Rule (application rule) Evaluates the arguments and calls either the primitive rule or the procedure rule. We do not need an environment here as the lambda form carries with it its own environment. ========> old Application Rule (define application rule (lambda (lst) (let ((primitive? (is primitive? (car lst))) (evaluated args (map evaluate lst))) (if primitive? (primitive rule (car evaluated args) (cdr evaluated args)) (procedure rule (car evaluated args) (cdr evaluated args)))))) ==========> new Application Rule We have recursive calls to evaluate here, so we need to bring along the envi ronment (define application rule (lambda (lst env) (let ((primitive? (is primitive? (car lst))) (evaluated args (map (evaluate form env)) lst))) (if primitive? (primitive rule (car evaluated args) (cdr evaluated args)) (procedure rule (car evaluated args) (cdr evaluated args)))))) Procedure Rule (procedure rule) The procedure rule calls subst to perform the needed substitutions according to the new procedure rule given in the handout, and the calls evaluate on the resulting form. ==========> old Procedure Rule (define procedure rule (lambda (lambda form args) (set! indent (+ indent 1)) (output (format "[Procedure Rule]") #t #t) (let ((result (evaluate (subst lambda form args)))) (set! indent ( indent 1)) result))) in the global environment. Note, if the name is already there it is simply updated using set cdr! Note, nothing stops this evaluator from having define forms that are not at the top level... just don t do that! ==========> old Define Rule (define define rule (lambda (name form) (output (format "[Define Rule]") #f #t) (let ((value (evaluate form)) (pair (assv name global env))) (set cdr! pair value) (set! global env (cons (cons name value) global env)))))) =========> new Define Rule (define define rule (lambda (name form env) (output (format "[Define Rule]") #f #t) (let ((value (evaluate form env)) (pair (lookup in environment name global env #f))) (set cdr! pair value) (set car! global env (append (car global env) (list (cons name value)) )))))) If Rule (if rule) Evaluates the boolean expression and based on the value evaluates either the true form or the false form. An if form is really a special kind of form called a special form because the word if is not evaluated. The same is true for a define form, the keyword define is not evaluated. =========> old If Rule (define if rule (lambda (test form true form false form) (output (format "[If Rule]") #f #t) (if (evaluate test form) (evaluate true form) (evaluate false form)))) Page 6/11 ==========> new Procedure Rule Instead of substituting we now simply call evaluate with the environment in which we wish to evaluate the application of the procedure. This environment if a new environment where the actual parameters are bound to the formal one s, and the parent environment is the one passed in (define procedure rule (lambda (lambda form args) (set! indent (+ indent 1)) (output (format "[Procedure Rule]") #t #t) (let* ((env (cadddr lambda form)) (new env (make new environment (map cons (cadr lambda form) args) env )) (result (evaluate (caddr lambda form) new env))) (set! indent ( indent 1)) result))) Define Rule (define rule) Evaluates the argument and binds the value of the argument to the name =========> new If Rule We need an environment here too! (define if rule (lambda (test form true form false form env) (output (format "[If Rule]") #f #t) (if (evaluate test form env) (evaluate true form env) (evaluate false form env)))) Begin Rule rule) All arguments are evaluated in order, and the value of the last evaluated form is returned. A begin form is special too, the keyword begin is not evaluated. =========> old Begin Rule (define begin rule (lambda (cmd list) (output (format "[Begin Rule]") #f #t) (car (reverse (map evaluate cmd list))))) 3/6
4 Page 7/11 ==========> new Begin Rule We need an environment here too. (define begin rule (lambda (cmd list env) (output (format "[Begin Rule]") #f #t) (let ((result (car (reverse (map (evaluate form env)) cmd lis t))))) result))) (letrec () b) => b) (letrec ((n1 b1)) b) ==> (let ((n1 #f)) (set! n1 b1) b)) (letrec ((n1 b1)... (nk bk)) b) => (let ((n1 #f)) (set! n1 b1) (letrec ((n2 b2)... (nk bk)) b)) Page 8/11 Let Rule (let rule) Rewrite (let ((n1 v1)... (nk vk) body) to ((lambda (n1... nk) body) v1... vk) (define let rule (lambda (let form env) (let ((n1 f1)... (nk fk) b) (output (format "[Let Rule]") #f #t) (let ((binding list (cadr let form))) (if (null? binding list) (evaluate (cons begin (cddr let form)) env) (let () b) => (be gin b) (evaluate (cons (append (list lambda ((λ (map car binding list)) (n1 n2... nk) (cddr let form)) b (map cadr binding list)) env))))) ) f1 f2... fk) (define ll (let ((f #f)) (set! f (lambda (x) (if (= x 0) 1 (* x (f ( x 1)))))) (f 4))) Let* Rule (let* rule) Rewrite (let* ((n1 v1)... (nk vk)) body) according to this: (let* () body) := body ;; (let* ((n v)) body) := (let ((n v )) body) (let* ((n1 v1)... (nk vk)) body) := (let ((n1 v1)) (let* ((n2 v2)... (nk vk)) body)) and then evaluate the new nested lambda forms/applications (define let* helper (lambda (let* form) (let ((binding list (cadr let* form))) (if (= 1 (length binding list)) (append (list let binding list) (cddr let* form)) (list let (list (car binding list)) (let* helper (append (list let* (cdr binding list)) (cddr let* form)))))))) (define let* rule (lambda (let* form env) (output (format "[Let* Rule]") #f #t) (if (null? (cadr let* form)) (evaluate (cons begin (cddr let* form)) env) (evaluate (let* helper let* form) env)))) Letrec Rule (letrec rule) (define letrec rule (lambda (letrec form env) (output (format "[Letrec Rule]") #f #t) ;; your code here! ) ) Set! Rule (set! rule) calls the helper set in environment, which is a simple modification of the deep search function that instead of returning a pair sets the cdr of it to the new value. (define set! rule (lambda (set! form env trace?) (output (format "[Set! Rule]") #f #t) (let ((name (cadr set! form)) (value (evaluate (caddr set! form) env))) (if (set in environment name env value trace?) (void) (display (format "set!: cannot set undefined identifier: ~a" name)) (loop)))))) ======================================= ====== END OF RULE IMPLEMENTATION ===== ======================================= ======================================== ===== THE MAIN EVALUATOR FUNCTIONS ===== ======================================== We need to carry an environment around when we evaluate. (define evaluate (lambda (cmd env) ; function application, primitive application or function definition? (output (format "evaluating ~a in ~a " cmd (pretty environment env)) #t #f) (if (list? cmd) can be one of: define, lambda, if, begin, or an application (case (car cmd) ((define) (define rule (cadr cmd) (caddr cmd) global env)) ((lambda) (output (format "=> #<procedure>") #f #t) (lambda rule cmd env))) ((if) (if rule (cadr cmd) (caddr cmd) (cadddr cmd) env)) () rule (cdr cmd) env)) ((let) (let rule cmd env)) ((let*) (let* rule cmd env)) ((letrec) (letrec rule cmd env)) ((set!) (set! rule cmd env #t)) (else If not one of define, lambda, if, begin, le /let*, or set! then application 4/6
5 Page 9/11 string (output (format "[Application Rule]") #f #t) (application rule cmd env)))) (if (or (boolean? cmd) (string? cmd) (number? cmd)) (literal rule cmd) If the command is a boolean, a number or a (name rule cmd env))))) else it must be a name ===== SYNTAX CHECKING PROCEDURES ===== ====================================== (define is form? (lambda (exp) (syntax check exp))) Page 10/11 The main read evaluate print loop (loop) 1. Read a form 2. Syntax check and evaluate 3. Print the result and recurse ==========> new loop added the global environment to the call to evaluate. (define loop (lambda () (set! indent 0) (display "> ") (let ((command (read))) (if (eq? command stop) #t (if (and (list? command) (> (length command) 0) (eq? (car command) t race)) switches tracing on and off (set! tracing (eq? (cadr command) on)) (loop)) (if (not (syntax check command)) check the syntax (or at le ast try!) (display (format "~a: ~a" type error msg)) (loop)) (let ((result (evaluate command global env))) everythin g was good, now evaluate (if (not (void? result)) (if (string? result) (display (format "Result: \"~a\"" result)) string look better with " " around (display (format "Result: ~a" result)))) (set! indent 0) (loop))))))))) (define is param list? (lambda (lst) (fold (lambda (x y) (and x y)) (map is name? lst) #t))) This is how you apply with and and or ; ) (define is name? (if (and (symbol? form) (not (number? form))) #t (set error msg (format "Not an identifier ~a." form))))) (define is lambda? (set! type "lambda") (if (not (= (length form) 3)) (set error msg (format "bad syntax in: ~a" form)) (if (not (is param list? (cadr form))) #f (if (not (is form? (caddr form))) (set error message (format "bad syntax (not a form) in: ~a" (caddr form))) #t))))) (define is begin? (set! type "begin") (fold (lambda (x y) (and x y)) (map is form? (cdr form)) #t))) (define is define? (set! type "define") (if (not (= (length form) 3)) (set error msg (format "bad syntax (multiple expression after identifier in: ~a" form)) (if (not (is name? (cadr form))) (set error msg (format "bad syntax in: ~a" (cadr form))) (if (not (is form? (caddr form))) (set error msg (format "bad syntax (not a form) in: ~a" (caddr form))) #t))))) =============================================== ===== END OF THE MAIN EVALUATOR FUNCTIONS ===== =============================================== ============================ ===== HELPER FUNCTIONS ===== ============================ fold fold is a version of apply that allows you to specify the neutral element. Example:(foldr f (a b c d) e) = f(f(f(f(e d) c) b) a) This is _really_ foldl for fold left because the neutral element and the recursion is left associative (define fold (lambda (f lst e) (if (null? lst) e (f (fold f (cdr lst) e) (car lst))))) ====================================== (define is if? (set! type "if") (if (not (= (length form) 4)) (set error msg (format "bad syntax in: ~a" form)) (if (not (is form? (cadr form))) (set error msg (format "bad syntax (not a form) in: ~a" (cadr form))) (if (not (is form? (caddr form))) (set error msg (format "bad syntax (not a form) in: ~a" (caddr form))) (if (not (is form? (cadddr form))) (set error msg (format "bad syntax (not a form) in: ~a" (caddr form)) ) #t)))))) (define is application? (fold (lambda (x y) (and x y)) (map is form? form) #t))) (define is binding list? (lambda (form lst) (if (not (list? lst)) (set error msg (format "bad syntax in: ~a" form)) (if (null? lst) #t 5/6
6 Page 11/11 )))) (let ((be (car lst))) (if (not (symbol? (car be))) (set error msg (format "bad syntax (not an identifier) in ~a" (car be))) (and (is form? (cadr be)) (is binding list? form (cdr lst))))) (define is let? (if (not (list? form)) (set error message (format "bad syntax in: ~a" form)) (set! type (car form)) (if (< (length form) 3) (set error message (format "bad syntax in: ~a" form)) (and (is binding list? form (cadr form)) (is form? (cons begin (cddr form))))))))) (define is let*? (is let? form))) (define is letrec? (is let? form))) (define syntax check (if (list? form) (if (= (length form) 0) (set! type "application") (set error msg (format "bad syntax in: ~a" form))) (case (car form) ((define) (is define? form)) ((lambda) (is lambda? form)) ((if) (is if? form)) () (is begin? form)) ((let) (is let? form)) ((letrec) (is letrec? form)) ((let*) (is let*? form)) (else (is application? form)))) (or (number? form) ;; numbers (symbol? form) ;; names (string? form) ;; (boolean? form))))) ======================================== ===== END OF SYNTAX CHECKING RULES ===== ======================================== (init rule) initialize (loop) GO! 6/6
Scheme Quick Reference
Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries
More informationScheme Quick Reference
Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete
More informationWhy do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.
.00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do
More informationComputer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing
Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible
More information(scheme-1) has lambda but NOT define
CS61A Lecture 12 2011-0-11 Colleen Lewis (calc) review (scheme-1) has lambda but NOT define Remember calc-apply? STk> (calc-apply '+ '(1 2 )) 6 STk> (calc-apply '* '(2 4 )) 24 STk> (calc-apply '/ '(10
More informationCS61A Midterm 2 Review (v1.1)
Spring 2006 1 CS61A Midterm 2 Review (v1.1) Basic Info Your login: Your section number: Your TA s name: Midterm 2 is going to be held on Tuesday 7-9p, at 1 Pimentel. What will Scheme print? What will the
More informationFunctional Programming. Pure Functional Programming
Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).
More informationProject 2: Scheme Interpreter
Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same
More informationScheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7
Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions
More informationAnnouncements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.
The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper
More informationUsing Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add
Using Symbols in Expressions (1) (define z y) z Symbol y z ==> y prints as (+ x 3) evaluate sub-expressions... PrimProc Number Number ( ) machine code to add 23 3 Number 26 apply... ==> 26 prints as 1
More informationregsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13
1 File: regsim.scm Register machine simulator from section 5.2 of STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS This file can be loaded into Scheme as a whole. Then you can define and simulate machines
More informationFunctional Programming. Pure Functional Languages
Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure
More informationSummer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define
CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationFunctional Programming. Pure Functional Languages
Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure
More informationCS 275 Name Final Exam Solutions December 16, 2016
CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 17: Functional Programming Zheng (Eddy Zhang Rutgers University April 4, 2018 Class Information Homework 6 will be posted later today. All test cases
More informationSyntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want
Computer Science 21b (Spring Term, 2017) Structure and Interpretation of Computer Programs Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want Here is one of the big ideas
More informationA Brief Introduction to Scheme (II)
A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data
More informationSCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015
SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More information6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.
6.034 Artificial Intelligence February 9, 2007 Recitation # 1 1 Lists and Trees (a) You are given two lists: (define x (list 1 2 3)) (define y (list 4 5 6)) What would the following evaluate to: (append
More information6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson
6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator
More informationIntroduction to Scheme
How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free
More informationCSc 520 Principles of Programming Languages
CSc 520 Principles of Programming Languages 9: Scheme Metacircular Interpretation Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian
More informationScheme in Scheme: The Metacircular Evaluator Eval and Apply
Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,
More informationCOP4020 Programming Languages. Functional Programming Prof. Robert van Engelen
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 18: Functional Programming Zheng (Eddy) Zhang Rutgers University April 9, 2018 Review: Defining Scheme Functions (define ( lambda (
More informationMORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.
MORE SCHEME COMPUTER SCIENCE MENTORS 61A October 30 to November 3, 2017 1 What Would Scheme Print? Solutions begin on the following page. 1. What will Scheme output? Draw box-and-pointer diagrams to help
More informationCSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018
CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018 Deliverables: Your code (submit to PLC server). A13 participation survey (on Moodle, by the day after the A13 due date). This is
More informationAn Explicit-Continuation Metacircular Evaluator
Computer Science (1)21b (Spring Term, 2018) Structure and Interpretation of Computer Programs An Explicit-Continuation Metacircular Evaluator The vanilla metacircular evaluator gives a lot of information
More informationMore Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?
More Scheme CS 331 Quiz 1. What is (car ((2) 3 4))? (2) 2. What is (cdr ((2) (3) (4)))? ((3)(4)) 3. What is (cons 2 (2 3 4))? (2 2 3 4) 4. What is the length of the list (()()()())? 4 5. Which element
More informationComp 311: Sample Midterm Examination
Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief
More informationFrom Syntactic Sugar to the Syntactic Meth Lab:
From Syntactic Sugar to the Syntactic Meth Lab: Using Macros to Cook the Language You Want a V E N E TRUT H Brandeis S PA R T U N T O I T S I N N E R M OS T COMPUTER SCIENCE 21B: Structure and Interpretation
More informationOverview. CS301 Session 3. Problem set 1. Thinking recursively
Overview CS301 Session 3 Review of problem set 1 S-expressions Recursive list processing Applicative programming A look forward: local variables 1 2 Problem set 1 Don't use begin unless you need a side
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.
More information6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter
6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,
More informationLECTURE 16. Functional Programming
LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative
More informationBuilding a system for symbolic differentiation
Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)
More informationBuilding a system for symbolic differentiation
Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)
More informationFall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of
1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { October 31,
More informationFall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives
CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write
More informationCSc 520. Principles of Programming Languages 7: Scheme List Processing
CSc 520 Principles of Programming Languages 7: Scheme List Processing Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg
More informationBelow are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.
6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs
More informationSpring 2018 Discussion 7: March 21, Introduction. 2 Primitives
CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme
More informationDocumentation for LISP in BASIC
Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have
More informationUser-defined Functions. Conditional Expressions in Scheme
User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment
More informationFall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives
CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write
More informationSCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017
SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More information1 of 5 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 2 solutions 1. Box and pointer. Note: Please draw actual boxes, as in the book and the lectures, not XX and X/ as in these ASCII-art solutions. Also,
More informationCSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing
Constructing Lists CSc 520 Principles of Programming Languages 7: Scheme List Processing Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona The most important
More informationEssentials of Programming Languages Language
Essentials of Programming Languages Language Version 5.3 August 6, 2012 The Essentials of Programming Languages language in DrRacket provides a subset of functions and syntactic forms of racket mostly
More informationEssentials of Programming Languages Language
Essentials of Programming Languages Language Version 6.90.0.26 April 20, 2018 The Essentials of Programming Languages language in DrRacket provides a subset of functions and syntactic forms of racket mostly
More informationCS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang
CS61A Notes Week 6B: Streams Streaming Along A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the
More informationApril 2 to April 4, 2018
MORE SCHEME COMPUTER SCIENCE MENTORS 61A April 2 to April 4, 2018 1 Scheme 1. What will Scheme output? Draw box-and-pointer diagrams to help determine this. (a) (cons (cons 1 nil) (cons 2 (cons (cons 3
More informationLecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.
Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)
More informationAn Introduction to Scheme
An Introduction to Scheme Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Stéphane Ducasse 1 Scheme Minimal Statically scoped Functional Imperative Stack manipulation Specification
More informationSample Final Exam Questions
91.301, Organization of Programming Languages Fall 2015, Prof. Yanco Sample Final Exam Questions Note that the final is a 3 hour exam and will have more questions than this handout. The final exam will
More information6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq
1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Spring Semester, 1998, Final Exam Solutions Your
More informationEvaluating Scheme Expressions
Evaluating Scheme Expressions How Scheme evaluates the expressions? (procedure arg 1... arg n ) Find the value of procedure Find the value of arg 1 Find the value of arg n Apply the value of procedure
More informationCS 360 Programming Languages Interpreters
CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like
More informationCS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.
CS 61A, Fall, 2002, Midterm #2, L. Rowe 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. a) d) 3 1 2 3 1 2 e) b) 3 c) 1 2 3 1 2 1 2 For each of the following Scheme
More informationScheme as implemented by Racket
Scheme as implemented by Racket (Simple view:) Racket is a version of Scheme. (Full view:) Racket is a platform for implementing and using many languages, and Scheme is one of those that come out of the
More informationCS 314 Principles of Programming Languages. Lecture 16
CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:
More informationCS450 - Structure of Higher Level Languages
Spring 2018 Streams February 24, 2018 Introduction Streams are abstract sequences. They are potentially infinite we will see that their most interesting and powerful uses come in handling infinite sequences.
More information;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))
Homework 1 Due 13 September Handout 2 CSC 131: Fall, 2006 6 September Reading 1. Read Mitchell, Chapter 3. 2. The Scheme Tutorial and the Scheme Quick Reference from the Links web page, as needed for the
More informationCSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp http://www.cs.washington.edu/341/ Lists (list expr2... exprn) '(value1 value2...
More informationPrinciples of Programming Languages
Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,
More informationBuilding up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.
6.001 SICP Variations on a Scheme Scheme Evaluator A Grand Tour Techniques for language design: Interpretation: eval/appl Semantics vs. snta Sntactic transformations Building up a language... 3. 1. eval/appl
More informationCS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan
CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan 1 Reading SICP, page 11-19, Section 1.1.6 Little Schemer, Chapter 2 2 The Idea of Syntax Abstraction Problem. Often programming tasks are repetitive,
More informationPrinciples of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003
Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 CS 314, LS, LTM: Functional Programming 1 Scheme A program is an expression to be evaluated (in
More informationSCHEME AND CALCULATOR 5b
SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationLecture 3: Expressions
CS 7400 September 23 30, 2009 Dr. Wand Readings: EOPL3, Secs. 3.1 3.5 Lecture 3: Expressions Key Concepts: syntax and semantics expressed and denoted values expressions environment specifying the behavior
More informationCSC 533: Programming Languages. Spring 2015
CSC 533: Programming Languages Spring 2015 Functional programming LISP & Scheme S-expressions: atoms, lists functional expressions, evaluation, define primitive functions: arithmetic, predicate, symbolic,
More informationHow to Design Programs Languages
How to Design Programs Languages Version 4.1 August 12, 2008 The languages documented in this manual are provided by DrScheme to be used with the How to Design Programs book. 1 Contents 1 Beginning Student
More information(Func&onal (Programming (in (Scheme)))) Jianguo Lu
(Func&onal (Programming (in (Scheme)))) Jianguo Lu 1 Programming paradigms Func&onal No assignment statement No side effect Use recursion Logic OOP AOP 2 What is func&onal programming It is NOT what you
More informationIntro. Scheme Basics. scm> 5 5. scm>
Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if
More informationCSC324 Functional Programming Efficiency Issues, Parameter Lists
CSC324 Functional Programming Efficiency Issues, Parameter Lists Afsaneh Fazly 1 January 28, 2013 1 Thanks to A.Tafliovich, P.Ragde, S.McIlraith, E.Joanis, S.Stevenson, G.Penn, D.Horton 1 Example: efficiency
More informationDiscussion 12 The MCE (solutions)
Discussion 12 The MCE (solutions) ;;;;METACIRCULAR EVALUATOR FROM CHAPTER 4 (SECTIONS 4.1.1-4.1.4) of ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS ;;;from section 4.1.4 -- must precede def of
More informationQuicksort. Alternative Strategies for Dividing Lists. Fundamentals of Computer Science I (CS F)
Fundamentals of Computer Science I (CS151.01 2006F) Quicksort Summary: In a recent reading, you explored merge sort, a comparatively efficient algorithm for sorting lists or vectors. In this reading, we
More informationCS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.
CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3
More informationCSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8
Question 1. (12 points) For each of the following, what value is printed? (Assume that each group of statements is executed independently in a newly reset Scheme environment.) (a) (define x 1) (define
More informationSCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012
SCHEME COMPUTER SCIENCE 6A October 29th, 202 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually
More informationLisp. Versions of LISP
Lisp Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks is based on Common Lisp Scheme is one of the major
More informationCS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers
CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers QUESTION We can also test if procedures are equal?. Consider this: > (define (square x) (* x x)) > (define (sqr x) (* x x)) > (eq?
More information6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science
6.821 Programming Languages Handout Fall 2002 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science Problem Set 6 Problem 1: cellof Subtyping Do exercise 11.6
More informationInterpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4
CS 61A Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions 1 Calculator We are beginning to dive into the realm of interpreting computer programs that is, writing programs that
More informationProcedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt
Data abstractions Abstractions and their variations Basic data abstractions Why data abstractions are useful Procedural abstraction Process of procedural abstraction Define formal parameters, capture process
More informationLexical vs. Dynamic Scope
Intro Lexical vs. Dynamic Scope where do I point to? Remember in Scheme whenever we call a procedure we pop a frame and point it to where the procedure points to (its defining environment). This is called
More informationTurtles All The Way Down
Turtles All The Way Down Bertrand Russell had just finished giving a public lecture on the nature of the universe. An old woman said Prof. Russell, it is well known that the earth rests on the back of
More informationStreams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson
Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation
More informationScheme Basics > (butfirst '(help!)) ()
Scheme Basics > (butfirst '(help!)) () [The butfirst of a *sentence* containing one word is all but that word, i.e., the empty sentence. (BUTFIRST 'HELP!) without the inner parentheses would be butfirst
More informationNormal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?
Normal Order (Lazy) Evaluation Alternative models for computation: Normal (Lazy) Order Evaluation Memoization Streams Applicative Order: evaluate all arguments, then apply operator Normal Order: pass unevaluated
More information6.945 Adventures in Advanced Symbolic Programming
MIT OpenCourseWare http://ocw.mit.edu 6.945 Adventures in Advanced Symbolic Programming Spring 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. ps.txt
More informationFunctional Programming - 2. Higher Order Functions
Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class
More informationINTRODUCTION TO SCHEME
INTRODUCTION TO SCHEME PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2019 Dalhousie University 1/110 SCHEME: A FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be passed as
More informationFirst-class continuations. call/cc, stack-passing CEK machines
First-class continuations call/cc, stack-passing CEK machines But first Assignment 2 e ::= (letrec* ([x e]...) e) (letrec ([x e]...) e) (let* ([x e]...) e) (let ([x e]...) e) (let x ([x e]...) e) (lambda
More informationSTREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?
STREAMS 10 COMPUTER SCIENCE 61AS Basics of Streams 1. What is a stream? A stream is an element and a promise to evaluate the rest of the stream. Streams are a clever idea that allows one to use sequence
More informationCSE 341 Section Handout #6 Cheat Sheet
Cheat Sheet Types numbers: integers (3, 802), reals (3.4), rationals (3/4), complex (2+3.4i) symbols: x, y, hello, r2d2 booleans: #t, #f strings: "hello", "how are you?" lists: (list 3 4 5) (list 98.5
More informationScheme: Strings Scheme: I/O
Scheme: Strings Scheme: I/O CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Wednesday, April 5, 2017 Glenn G. Chappell Department of Computer Science University of
More informationThis exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.
CS 61A Final Exam May 16, 2008 Your name login: cs61a This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions. This booklet contains 18 numbered pages including
More information