(scheme-1) has lambda but NOT define

Similar documents
CS61A Midterm 2 Review (v1.1)

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Functional Programming. Pure Functional Programming

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

SCHEME AND CALCULATOR 5b

A Brief Introduction to Scheme (II)

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

An Explicit-Continuation Metacircular Evaluator

Discussion 12 The MCE (solutions)

Project 2: Scheme Interpreter

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

6.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

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Turtles All The Way Down

CS 314 Principles of Programming Languages. Lecture 16

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

Lexical vs. Dynamic Scope

CSc 520 Principles of Programming Languages

Functional Programming. Pure Functional Languages

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Functional Programming. Pure Functional Languages

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Introduction to Scheme


Building a system for symbolic differentiation

CS 275 Name Final Exam Solutions December 16, 2016

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CS 360 Programming Languages Interpreters

Parsing Scheme (+ (* 2 3) 1) * 1

From Syntactic Sugar to the Syntactic Meth Lab:

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Overview. CS301 Session 3. Problem set 1. Thinking recursively

Building a system for symbolic differentiation

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

CS61A Notes Disc 11: Streams Streaming Along

6.001: Structure and Interpretation of Computer Programs

C311 Lab #3 Representation Independence: Representation Independent Interpreters

User-defined Functions. Conditional Expressions in Scheme

Administrivia. Simple data types

An Introduction to Scheme

Documentation for LISP in BASIC

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

YOUR NAME PLEASE: *** SOLUTIONS ***

Scheme Quick Reference

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

INTERPRETATION AND SCHEME LISTS 12

6.945 Adventures in Advanced Symbolic Programming

INTERPRETATION AND SCHEME LISTS 12

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

Essentials of Programming Languages Language

Essentials of Programming Languages Language

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

Scheme Quick Reference

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

LECTURE 16. Functional Programming

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Comp 311: Sample Midterm Examination

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

An Explicit Continuation Evaluator for Scheme

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

CSC 533: Programming Languages. Spring 2015

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Fall Semester, Lecture Notes { November 12, Variations on a Scheme Nondeterministic evaluation

Ways to implement a language

Functional Programming Lecture 1: Introduction

Lecture Notes on Lisp A Brief Introduction

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Simplifying Logical Formulas

ALISP interpreter in Awk

Programming Languages

COP4020 Programming Assignment 1 - Spring 2011

Intro. Scheme Basics. scm> 5 5. scm>

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Discussion 11. Streams

Organization of Programming Languages CS3200/5200N. Lecture 11

DRAWING ENVIRONMENT DIAGRAMS

Transcription:

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 2)) STk> (calc-apply '- '( 2 1)) (calc) read-eval-print loop (define (calc) (display "calc: ") (flush) (print (calc-eval (read))) (calc)) STk> (calc) calc-eval calc: (+ (* 2 ) 1 (define (calc-eval exp) ((number? exp) exp) ((list? exp) (calc-apply (car exp) (map calc-eval (cdr exp)))) (else (error Calc: bad exp )))) 2 * + 4 (scheme-1) DOES NOT HAVE DEFINE! STk> (scheme-1) Scheme-1: Scheme-1: ( ) 1

Working with large programs! Start with small functions Understanding code Read it Recursively figure out any functions it calls Try to call the function to see what it does in different cases Trace the function and try to call it from the functions that call it. Approximate hierarchy of calls scheme-1 apply-1 substitute lookup maybe-quote lambda-exp? if-exp? quote-exp? exp-checker constant? number? boolean? string? proceedure? lambda-exp? STk> (lambda-exp? ' (+ x 2))) #t STk> (lambda-exp? '(+ ) STk> (lambda-exp? '+) STk> (lambda-exp? '(lambda)) #t It isn t as picky as we might hope Write lambda-exp? in terms of exp-checker (define (exp-checker type) (lambda (exp) (and (pair? exp) (eq? (car exp) type)))) (lambda-exp? ' (+ x 2))) Did you write it with syntactic sugar? A) Yes B)No Some Helpers (define quote-exp? (exp-checker 'quote)) (define if-exp? (exp-checker 'if)) (define (constant? exp) (or (number? exp) (boolean? exp) (string? exp) (procedure? exp))) What is string? STk> (string? "hello") #t STk> (string? 12) STk> (string? 'hello) 2

(lookup name params args) STk> (lookup 'x STk> (lookup 'y '(x y) '(2 )) STk> (lookup 'y y STk> (lookup '* * Just returns it if it isn t in there lookup full functionality STk> (lookup 'fn This already works '(x fn) '( (lambda (y) (* y y)))) (lambda (y) (* y y)) STk> (lookup 'x '(cat)) (quote cat) cat was already a word, but we want to tell other people this thing IS ACTUALLY a word Full lookup (define (lookup name params args) ((null? params) name) ((eq? name (car params)) (maybe-quote (car args)) (else (lookup name (cdr params) (cdr args))))) maybe-quote (define (maybe-quote value) ((lambda-exp? value) value) ((constant? value) value) ((procedure? value) value) (else (list 'quote value)))) Substitution using substitute STk> ( ) '(* x x) '() (* ) STk> ((lambda (x y) (+ x y)) STk>(substitute '(+ x y) '(x y) '( (+ (lambda (y) (* x y)) ) ) What does this return? '() (lambda (y) (* y)) '(lambda (y) (* x y))

Scheme substitution review (* x x)) ) #[closure arglist=(x) ff1a1f8] STk> (( (* x x))) ) What does this return? A) B) 16 C)?? (* x x)) ) #[closure arglist=(x) ff1a1f8] ' '( (apply-1 proc args) STk> (apply-1 + '( ) STk> (apply-1 Unlike calc-apply apply-1 can be called with REAL scheme functions ' Or lists representing functions Remember lambda-exp?? apply-1 (define (apply-1 proc args) ((procedure? proc) (apply proc args)) ((lambda-exp? proc) ( (substitute (caddr proc) (cadr proc) args )) (else (error "bad proc:" proc)))) scheme-1 (define (scheme-1) (display "Scheme-1: ") (flush) (print ( (read))) (scheme-1)) STk> ( ) ((constant? exp) exp) STk> ( '+) #[closure arglist=args ffde8] ((symbol? exp) (eval exp)) 4

STk> ( '(if (> )) ((if-exp? exp) (if ( (cadr exp)) ( (caddr exp)) ( (cadddr exp)))) STk> ( 'x) *** Error: unbound variable: x Current eval stack: 0 x 1 (eval exp) Things like + are quoted: '+ when they are passed to so this assumes x will be a variable not a word. with words STk> ( '(quote x)) x STk> ( ') STk> ( (quote (quote x))) x STk> ( ''x) x ((quote-exp? exp)(cadr exp)) These are all equivalent! ((lambda-exp? exp) exp) (define ( exp) ((constant? exp) exp) ((symbol? exp) (eval exp)) ((quote-exp? exp) (cadr exp)) ((if-exp? exp) (if ( (cadr exp)) ( (caddr exp)) ( (cadddr exp)))) ((lambda-exp? exp) exp) ((pair? exp) (else (error "?!?" exp)))) ((pair? exp) ) STk> ( '(+ 2 )) STk> ( '(+ (- 1) )) STk> ( '( ))

((pair? exp) ) ((pair? exp) (apply-1 ( (car exp)) (map (cdr exp)))) STk> ( '(+ 2 )) STk> ( '(+ (- 1) )) STk> ( '( )) lambda-exp? Solution (define (exp-checker type) (lambda (exp) (and (pair? exp) (eq? (car exp) type)))) (define (lambda-exp? exp) ((exp-checker 'lambda) exp)) (define lambda-exp? (exp-checker 'lambda)) Write lookup (some functionality missing) (define (lookup name params args) ((null? params) name) ((eq? name (car params)) (car args)) (else (lookup name (cdr params) (cdr args))))) Substitution using substitute STk> ( ) '(* x x) '() (* ) STk> ((lambda (x y) (+ x y)) STk>(substitute '(+ x y) '(x y) '( (+ (lambda (y) (* x y)) ) ) What does this return? #[closure arglist=(y) ff1cc48] A procedure that takes argument y and adds to it '(lambda (y) (* x y)) '() (lambda (y) (* y)) (* x x)) ) #[closure arglist=(x) ff1a1f8] A recursive call will be made where bound will be ' '( 6