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

Similar documents
Building a system for symbolic differentiation

Building a system for symbolic differentiation

A Brief Introduction to Scheme (II)

6.001: Structure and Interpretation of Computer Programs

An Explicit-Continuation Metacircular Evaluator

CS61A Midterm 2 Review (v1.1)

Functional Programming. Pure Functional Programming

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

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

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

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

Introduction to Scheme

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

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

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

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

University of Massachusetts Lowell

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Documentation for LISP in BASIC

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

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

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

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

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

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

Scheme Quick Reference

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

Scheme Quick Reference

(scheme-1) has lambda but NOT define


Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

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

Lists in Lisp and Scheme

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

YOUR NAME PLEASE: *** SOLUTIONS ***

Discussion 12 The MCE (solutions)

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

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

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

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

An introduction to Scheme

Functional Programming. Pure Functional Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lecture Notes on Lisp A Brief Introduction

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

CS 314 Principles of Programming Languages

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

Functional Programming. Pure Functional Languages

From Syntactic Sugar to the Syntactic Meth Lab:

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

Functional Programming - 2. Higher Order Functions

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

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

Simplifying Logical Formulas

Evaluating Scheme Expressions

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

Essentials of Programming Languages Language

Essentials of Programming Languages Language

CSc 520 Principles of Programming Languages

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

6.945 Adventures in Advanced Symbolic Programming

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

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Programming Languages

Vectors in Scheme. Data Structures in Scheme

CSc 520. Principles of Programming Languages 7: Scheme List Processing

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

LECTURE 16. Functional Programming

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

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

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

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

Imperative, OO and Functional Languages A C program is

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

Lexical vs. Dynamic Scope

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

Sample Final Exam Questions

C311 Lab #3 Representation Independence: Representation Independent Interpreters

CS 275 Name Final Exam Solutions December 16, 2016

CS3: Introduction to Symbolic Programming. Lecture 14: Lists.

Lecture08: Scope and Lexical Address

Chapter 1. Fundamentals of Higher Order Programming

Scheme: Strings Scheme: I/O

Lisp. Versions of LISP

Modern Programming Languages. Lecture LISP Programming Language An Introduction

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

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?

COP4020 Programming Assignment 1 - Spring 2011

Functional Programming

Lecture #24: Programming Languages and Programs

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

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CS450 - Structure of Higher Level Languages

Project 2: Scheme Interpreter

Chapter 15 Functional Programming Languages

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Assignment 1. Due Tuesday October 11 at 11pm. No late assignments will be accepted. What to do

Streams and Evalutation Strategies

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

Transcription:

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

Using Symbols in Expressions (2) (list + x 3) evaluate sub-expressions... PrimProc PrimProc ( Number Number machine machine ) code LIST code to add 23 3 apply... PrimProc machine code to add Number 23 Number 3 ==> ([#prim-proc 7] 23 3) prints as 2

Using Symbols in Expressions (3) (list + x 3) evaluate sub-expressions... PrimProc Symbol Symbol Number ( ) machine code LIST + x 3 apply... Symbol + Symbol x Number 3 ==> (+ x 3) prints as 3

Using Symbols in Expressions (4) (2 a) Number 2 Symbol a ==> (2 a) prints as (2 (b 3)) ==> (2 (b 3)) prints as 2 b 3 4

Example: Substituting Symbols (define (substitute new old lst) (if (null? lst) () (let ((rest (substitute new old (cdr lst)))) (if (eq? old (car lst)) (cons new rest) (cons (car lst) rest))))) (substitute shemp curley (moe pounded curley on the head)) ==> (moe pounded shemp on the head) 5

Numerical Computation (define (numerical-derivative f) (define epsilon 0.0001) (lambda (x) (/ (- (f (+ x epsilon)) (f x)) epsilon))) 6

Symbolic Computation dc dx = 0 dx dx = 1 dy dx = 0 d du ( u + v) = dx dx dv + dx d dx ( uv) u dv du = dx + v dx (define (deriv exp var) (cond ((constant? exp) (make-constant 0)) ((variable? exp) (if (same-variable? exp var) (make-constant 1) (make-constant 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 (multiplicand exp) (deriv (multiplier exp) var)))))) 7

Math Expression Abstraction Constructors, Selectors, and Predicates: (make-constant <c>) (constant? <e>) Construct constant <c> Is <e> a constant? (make-variable <v>) (variable? <e>) (same-variable? <v1> <v2>) Construct a variable <v> Is <e> a variable? Are <v1> and <v2> same? (make-sum <addend> <augend>)construct sum (sum? <e>) Is <e> a sum? (addend <e>) Addend of sum <e> (augend <e>) Augend of sum <e> (make-product <multiplier> <multiplicand>) (product? <e>) Is <e> a product? (multiplier <e>) Multiplier of product <e> (multiplicand <e>) Multiplicand of product 8

Math Expression Implementation (define (make-constant x) x) (define (constant? x) (number? x)) (define (make-variable x) x) (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 (sum? x) (and (pair? x) (eq? (car x) +))) (define (addend s) (cadr s)) (define (augend s) (caddr s)) (define (make-product m1 m2) (list * m1 m2)) (define (product? x) (and (pair? x) (eq? (car x) *))) (define (multiplier m) (cadr m)) (define (multiplicand m) (caddr m)) 9

Math Expressions 4x+2 (define math (make-sum (make-product (make-constant 4) (make-variable x)) (make-constant 2))) ==> (+ (* 4 x) 2) 10

Symbolic Derivative - Spaghetti Code (define (deriv exp var) (cond ((number? exp) 0) ((symbol? exp) (if (and (symbol? var) (eq? exp var)) 1 0)) ((and (pair? exp) (eq? (car exp) +)) (list + (deriv (cadr exp) var) (deriv (caddr exp) var))) ((and (pair? exp) (eq? (car exp) *)) (list + (list * (cadr exp) (deriv (caddr exp) var)) (list * (deriv (caddr exp) var) (caddr exp)))))) 11

Math Expression Implementation (Alternative) (define (make-constant x) x) (define (constant? x) (number? x)) (define (make-variable x) x) (define (variable? x) (symbol? x)) (define (same-variable? v1 v2) (and (variable? v1) (variable? v2) (eq? v1 v2))) (define (make-sum a1 a2) (list SUM a1 a2)) (define (sum? x) (and (pair? x) (eq? (car x) SUM))) (define (addend s) (cadr s)) (define (augend s) (caddr s)) (define (make-product m1 m2) (list PROD m1 m2)) (define (product? x) (and (pair? x) (eq? (car x) PROD))) (define (multiplier m) (cadr m)) (define (multiplicand m) (caddr m)) 12

Math Expressions (Alternative) 4x+2 (define math (make-sum (make-product (make-constant 4) (make-variable x)) (make-constant 2))) ==> (SUM (PROD 4 x) 2) 13

Deriv Example math ==> (+ (* 4 x) 2) Follow substitution model through: (deriv math x) (make-sum (deriv (* 4 x) x) (deriv 2 x)) (make-sum (deriv (* 4 x) x) 0) (make-sum (make-sum (make-product 4 (deriv x x)) 0) (make-product (deriv 4 x) x)) (+ (+ (* 4 1) 0) (* 0 x)) 14

Deriv - Reduction Problem (deriv (+ x 3) x) ==> (+ 1 0) (deriv (* x y) x) ==> (+ (* x 0) (* 1 y)) (derive (* (* x y) (+ x 3)) x) ==> (+ (* (* x y) (+ 1 0)) (* (+ (* x 0) (* 1 y)) (+ x 3))) 15

(Reducing Math Expression Implementation (define (make-sum a1 a2) (cond ((and (constant? a1) (constant? a2)) (make-constant (+ a1 a2))) ((constant? a1) (if (= a1 0) a2 (list + a1 a2))) ((constant? a2) (if (= a2 0) a1 (list + a1 a2))) (else (list + a1 a2)))) (define (make-product m1 m2) (cond ((and (constant? m1) (constant? m2)) (make-constant (* m1 m2))) ((constant? m1) (cond ((= m1 0) (make-constant 0)) ((= m1 1) m2) (else (list * m1 m2)))) ((constant? m2) (cond ((= m2 0) (make-constant 0)) ((= m2 1) m1) (else (list * m1 m2)))) (else (list * m1 m2)))) (list * m1 m2)) 16

Variable # Arguments in Procedures In Scheme: (+ (* x 3) 10 (+ 2 x)) Would like to be able to build similar products and sums with arbitrary number of arguments. More Scheme syntax: dotted tail notation (define (f x. y) <body>) (f 1 2 3 4) in <body> x bound to 1 y bound to (2 3 4) 17

Math Expression Implementation - Variable # Terms (define (make-sum a1. a2) (cons + (cons a1 a2))) (define (augend s) (if (null? (cdddr s)) (caddr s) (cons + (cddr s)))) (define (multiplicand p) (if (null? (cdddr p)) (caddr p) (cons + (cddr p)))) 18

Adding Exponential Expressions to Deriv du n nu n 1 du = dx dx (define (deriv exp var) (cond... ((exponential? exp) (make-product (make-product (exponent exp) (make-exponential (base exp) (- (exponent exp) 1))) (deriv (base exp) var))))) (define (make-exponential b e) (cond ((= e 0) (make-constant 1)) ((= e 1) b) (else (list ** b e)))) (define (exponential? exp) (and (pair? exp) (eq? (car exp) **))) (define (base exp) (cadr exp)) (define (exponent exp) (caddr exp)) 19