Extended Introduction to CS Preparation for Final Exam.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Scheme in Scheme?!? …the metacircular evaluator….
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
1 Programming Languages (CS 550) Operational Semantics of Scheme using Substitution and the Lambda Calculus Jeremy R. Johnson TexPoint fonts used in EMF.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 Programming Languages (CS 550) Lecture 7 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts used in EMF. Read.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
מבוא מורחב למדעי המחשב תרגול 13 Lazy Evaluation. Lazy Evaluation Implementation Two types of values – Delayed Values – Actual Values Introduce two functions.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Metacircular Evaluator 4.1, pages definitions file on web 2.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
מבוא מורחב - שיעור 15 1 Lecture 15 Streams. מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((>
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
6.001 SICP 1 Normal (Lazy) Order Evaluation Memoization Streams.
Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
Extended Introduction to CS Preparation for Final Exam.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 13. Streams 3.5, pages definitions file on web 2.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 21. Overview 1. Dynamic Binding 2. Lazy Evaluation 3. More MC-Eval 2.
Lexical vs. Dynamic Scope …where do I point to?. Intro… Remember in Scheme whenever we call a procedure we pop a frame and point it to where the procedure.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Scheme More MCE examples. Q1 new special form which defines global variables (static ) search the global environment – Variable exists: does nothing,
1 Lecture OO, the notion of inheritance 3 Stacks in OO style (define (make-stack) (let ((top-ptr '())) (define (empty?) (null? top-ptr)) (define.
1 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure + symbol.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
1 The metacircular evaluator (Cont.) Defining new procedures (define (lambda? e) (tag-check e 'lambda)) (define (eval exp env) (cond ((number? exp)
Scheme in Scheme 1. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 15: Meta-Circular Evaluator 한 태숙.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
6.001 Final Exam Review Spring 2005 By Gerald Dalley.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 29: Typed Scheme MC Escher, Liberation.
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)
1 Read-Eval-Print Loop (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (eval input the-global-env))) (announce-output.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Lecture 19 Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
1/ SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Making the environment model concrete Defining eval defines the language –Provides.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 מבוא מורחב למדעי המחשב בשפת Scheme תרגול Outline Mutable list structure RPN calculator Vectors and sorting.
Streams Review A Review of Streams Mark Boady. What Are Steams? Delayed lists We pretend that the stream is a list In reality we only know the next value,
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
6.037 Lecture 7B Scheme Variants Normal Order Lazy Evaluation Streams Edited by Mike Phillips & Ben Vandiver Original Material by Eric Grimson & Duane.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Additional Scheme examples
6.001 SICP Variations on a Scheme
Env. Model Implementation
6.001 SICP Data abstractions
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
Lecture #7 מבוא מורחב.
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Extended Introduction to CS Preparation for Final Exam

Mutation

rotate! (define l ‘( )) (rotate! l) l ( ) First attempt: use set-cdr! Would it work? In which environment is l? and lst? l lst

Correct solution (define (rotate! lst) (define (help p rest-lst) (cond ((null? rest-lst) (set-car! lst p)) (else (let ((temp (car rest-lst))) (set-car! rest-lst p) (help temp (cdr rest-lst)))))) (help (car lst) (cdr lst)) lst)

High-order functions Functions can receive other functions as input and may return functions as output

make-star g*(x) = number of times we need to apply g until g(g(g(…(g(x)…)))<=1

make-star (define (make-star g) (define (g* x) (if (<= x 1) 0 (+ 1 (g* (g x))))) g* )

log (log 4) ==> 2 (log 1) ==> 0 (log 5) ==> 3 (define log (make-star _________________ ) (lambda (x) (/ x 2))

Streams

SOS-interleave Receives a stream of streams Should return a stream of all members (in some order)

(define (sos-interleave sos) (let ((first-of-first (stream-car (stream-car sos))) (rest-of-first (stream-cdr (stream-car sos)) (rest (stream-cdr sos))) (cons-stream ________________________ ________________________)))

SOS-interleave (define (sos-interleave sos) (let ((first-of-first (stream-car (stream-car sos))) (rest-of-first (stream-cdr (stream-car sos)) (rest (stream-cdr sos))) (cons-stream first-of-first (interleave rest-of-first (sos-interleave rest)))))

Words List of symbols (‘a or ‘b) represent a word A stream of lists represents a language 1. Generate the language of all words containing only ‘a’ 2. Generate the language of all words starting with an ‘a’

All-a (define all-a (cons-stream '(a) (stream-map (lambda (x) (append x '(a))) all-a))))

Starting-with-a (define start-with-a (cons-stream '(a) (interleave (stream-map (lambda (x) (append x '(a))) start-with-a) (stream-map (lambda (x) (append x '(b))) start-with-a))))

MC-eval

Add a new special form – decrease (decrease proc num) If proc is compound, change its body so that it will return a number smaller by num than what it should have If proc is not compound return error Otherwise assume it returns a number

;;; M-Eval input: (define (double x) (+ x x)) ;;; M-Eval value: ok ;;; M-Eval input: (double 3) ;;; M-Eval value: 6 ;;; M-Eval input: (decrease double 1) ;;; M-Eval value: ok ;;; M-Eval input: (double 3) ;;; M-Eval value: 5 ;;; M-Eval input (decrease + 1) Error: can apply decrase only to compound procedures

Section A (define (decrease? exp) ___________________________ ) (define (decrease-proc-name exp) ______________________ ) (define (decrease-number exp) _______________________ )

(define (decrease? exp) ___(tagged-list? exp 'decrease)___________________ ) (define (decrease-proc-name exp) ____(cadr exp)__________________________ ) (define (decrease-number exp) _____(caddr exp)_______________________ )

Section B Decrease returns the new procedure body (define (modify-body proc expressions-list) (set-car! (cddr proc) expressions-list)) (define (eval-decrease exp env) (let* ((proc-name ________________________________ ) (number ________________________________ ) (proc (_________________________ proc-name env))) (cond ( ____________________________________ (error “Can apply decrease only to compound procedures”)) (else (modify-body proc (decrease proc number)) ‘ok))))

(define (modify-body proc expressions-list) (set-car! (cddr proc) expressions-list)) (define (eval-decrease exp env) (let* ((proc-name ____(decrease-proc-name exp)________ ) (number ____(decrease-number exp)_____________ ) (proc (_____mc-eval _____________ proc-name env))) (cond ( _____(not (compound-procedure? proc))____________ (error “Can apply decrease only to compound procedures”)) (else (modify-body proc (decrease proc number)) ‘ok))))

Environment Model

Functional tables (define (make-table) (lambda (x) (error "key not found:" x))) (define (find key table) (table key)) (define (insert key value table) (lambda (x) (if (equal? x key) value (find x table)))) Q2

Example (define t0 (make-table)) (define t1 (insert 'a 1 t0)) (define t2 (insert 'b 2 t1)) (find 'a t2) => 1

global env Pars: – Body: (lambda (x) (error...)) make-table: Pars: key, table Body: (table key) find: Pars: key, table, value Body: (lambda (x) (if...)) insert: E1 : Pars: x Body: (error...)) t0: Pars: x Body: (if...)) key: a value: 1 table: E2: t1: key: a table: E4: key: a table: E6: x: a E5: x: a E7: key: b value: 2 table: E3: Pars: x Body: (if...)) t2: