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

Similar documents
Functional Programming. Pure Functional Programming

Introduction to Scheme

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

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

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

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

CS 275 Name Final Exam Solutions December 16, 2016

CS 360 Programming Languages Interpreters

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

What is tail position?

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

An Introduction to Scheme

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

Project 2: Scheme Interpreter

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

1.3. Conditional expressions To express case distinctions like


CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

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

Agenda. CS301 Session 9. Abstract syntax: top level. Abstract syntax: expressions. The uscheme interpreter. Approaches to solving the homework problem

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

An Explicit-Continuation Metacircular Evaluator

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

Project 5 - The Meta-Circular Evaluator

Project 5 - The Meta-Circular Evaluator

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

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

Scheme Quick Reference

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

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

(scheme-1) has lambda but NOT define

COMP 105 Homework: Type Systems

A Brief Introduction to Scheme (II)

Organization of Programming Languages CS3200/5200N. Lecture 11

Scheme Quick Reference

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

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

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

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

LECTURE 16. Functional Programming

CS450 - Structure of Higher Level Languages

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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?

CS 314 Principles of Programming Languages

Data! Two new kinds of data. The automatically managed cons cell Mother of heap-allocated data structures

CS 314 Principles of Programming Languages

Quicksort. Alternative Strategies for Dividing Lists. Fundamentals of Computer Science I (CS F)

What is tail position?

Principles of Programming Languages

Fall 2017 December 4, Scheme. Instructions

Fall 2017 December 4, 2017

CSc 520 Principles of Programming Languages

Functional Programming - 2. Higher Order Functions

User-defined Functions. Conditional Expressions in Scheme

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

ALISP interpreter in Awk

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

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Lisp Basic Example Test Questions

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

Functional Programming. Pure Functional Languages

Vectors in Scheme. Data Structures in Scheme

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

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

CS61A Notes Disc 11: Streams Streaming Along

April 2 to April 4, 2018

Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?

Simplifying Logical Formulas

6.001: Structure and Interpretation of Computer Programs

From Syntactic Sugar to the Syntactic Meth Lab:

Evaluating Scheme Expressions

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))

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

Important Example: Gene Sequence Matching. Corrigiendum. Central Dogma of Modern Biology. Genetics. How Nucleotides code for Amino Acids

Essentials of Programming Languages Language

University of Massachusetts Lowell

Essentials of Programming Languages Language

Building a system for symbolic differentiation

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

Functional Programming. Pure Functional Languages

Turtles All The Way Down

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

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

Building a system for symbolic differentiation

Streams and Evalutation Strategies

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

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

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

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

Documentation for LISP in BASIC

YOUR NAME PLEASE: *** SOLUTIONS ***

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

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

INTRODUCTION TO SCHEME

Functional programming in μscheme

Functional Programming Languages (FPL)

Transcription:

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 effect: (if (< i (+ (/ n 2) 1)) (begin! (if (= (mod n i) 0)! 0! (primer n (+ i 1)))) 1) is the same as (if (< i (+ (/ n 2) 1))! (if (= (mod n i) 0)! 0! (primer n (+ i 1))) 1) Thinking recursively (define sumprimes (n) (if (= n 0) 0 (+ (nthprime n) (sumprimes (- n 1))))) versus ;n is desired number, m is current number, x is value of current, y is the sum (define sumprimehelper (n m x y) (if (= n m) y (if (= (prime x) 1)! (sumprimehelper n (+ m 1) (+ x 1) (+ y x) )! (sumprimehelper n m (+ x 1) y)))) (define sumprimes (n) (sumprimehelper n 0 2 0)) ;1st prime is 2 3 4

Extending Impcore to uscheme A more general kind of value: the S-expression (Simplified) concrete syntax: literal ::= integer #t #f 'S-exp S-exp ::= literal symbol (S-exp1... S-expn) Creators '(), list1, list2,... Producers List processing cons, append, reverse,... Observers car, cdr, null?, caar, cadr, cdar,... 5 6 Linear recursion on lists Accumulating parameters A simple pattern: (define some-function (l...) (if (null? l) (base-case) (... (some-function (cdr l)...))) ) E.g. length, equal?, append, reverse Most of you know this method and applied it in problem set 1 Improving naive reverse: (define revapp (l1 l2) (if (null? l1) l2 (revapp (cdr l1) (cons (car l1) l2)))) (define reverse (l)?) 7 8

Using lists - primes (define remove-multiples (n l) (if (null? l) '() (if (divides n (car l)) (remove-multiples n (cdr l)) (cons (car l) (remove-multiples n (cdr l))) ))) Removing non-primes: Primes continued (define sieve (l) (if (null? l) '() (cons (car l) (sieve (remove-multiples (car l) (cdr l)))))) (define primes<= (n) (sieve (seq 2 n))) 9 10 Insertion sort (define insert (x l) (if (null? l) (list1 x) (if (< x (car l)) (cons x l) (cons (car l) (insert x (cdr l)))))) (define sort (l) (if (null? l) '() (insert (car l) (sort (cdr l))))) Invariant: no duplicate elements (define add-element (x s) (if (member? x s) s (cons x s)))...simple definition of size: Lists as sets (define size (s) (length s)) 11 12

Sets continued Sets are egalitarian!...complicated definition of union (define union (s1 s2) (if (null? s1) s2 (add-element (car s1) (union (cdr s1) s2)))) Why not use append? Anything can be a member of a "set": numbers, symbols, lists of all kinds E.g. ->(val s (add-element '(a b) (add-element 3 emptyset))) ((a b) 3) 13 14 Lists as maps: a-lists General form ((key value) (key value)...) Keys and values can be anything Lookup by linear search bind and find create and use maps Here's the definition of bind A mental exercise (define bind (x y alist) (if (null? alist) (list1 (list2 x y)) (if (equal? x (caar alist)) (cons (list2 x y) (cdr alist)) (cons (car alist) (bind x y (cdr alist)))))) What's the result of (bind 'A 'B alist) if alist is ((C D) (B C) (A E))? 15 16

Property lists Applicative programming We can bind a key in an a-list to another a-list! This was very exciting in the early days of knowledge representation research. (define property (x p plist) (find p (find x plist)))...works by applying functions, not by mutating state The meaning of a name in a given scope doesn't change over time - no set Therefore we can have confidence in some simple laws. 17 18 If all subexpressions are applicative (car (cons x y)) = x (cdr (cons x y)) = y Some laws Why do we need the caveat? -> (val x 0) 0 -> (cdr (cons (set x (+ x 1)) (set x (+ x 2)))) 3 -> (val x 0) 0 -> (set x (+ x 2)) 2 cons makes pairs! We can cons anything to anything -> (cons 1 2) (1. 2) -> (cons 1 '(2)) (1 2)...so beware! Normally you want the second thing to be a list. 19 20

(pair? (cons x y)) = #t (null? (cons x y)) = #f (pair? '()) = #f (null? '()) = #t (car '()) ==> ERROR (cdr '()) ==> ERROR More laws Laws for Booleans Again, if all subexpressions are applicative (if #t x y) = x (if #f x y) = y (if (not p) x y) = (if p y x) Laws for the novice programmer: (if p #t #f) = p (if p #f #t) = (not p) 21 22 A look ahead Local variables using let-binding E.g., instead of (begin (set r (random)) (if (< r 20)... (if (< r 40)... ))) introduce a fresh variable: (let ((r (random)) (if (< r 20)... (if (< r 40)... ))) Assignment Read R&K 3.5, 3.6, and 3.7 to the beginning of section 3.7.2 on page 75. Problem set 2 is due Friday. 23 24

Overview CS301 Session 4 Local variables Recursive tree processing Introduction to first-class functions Preview 1 2 Local variables New syntax; the bindings look like an a-list Three types Simple let binds simultaneously let* binds sequentially letrec binds recursively Simultaneous binding (define foo (x) (let ((x (car x)) (y (cdr x))) (cons y x))) -> (foo '(a b)) ((b). a) -> (foo '((a b) c)) ((c) a b) 3 4

Sequential binding (define foo (x) (let* ((x (car x)) (y (cdr x))) (cons y x))) -> (foo '(a b)) error: cdr applied to non-pair a in (cdr x) -> (foo '((a b) c)) ((b) a b) Recursive binding (define foo (x) (letrec ((x (car x)) (y (cdr x))) (cons y x))) -> (foo '(a b)) error: car applied to non-pair () in (car x) 5 6 Local functions (define naive-fib (n) (begin (set naive-fib-count (+ naive-fib-count 1)) (if (< n 2) n (+ (naive-fib (- n 1)) (naive-fib (- n 2)))))) -> (naive-fib 10) 55 -> naive-fib-count 177 Local functions (define fib (n) ;; bottom-up helper function: return a list of ;; fib_i, fib_i-1 (letrec ((fib* (lambda (i)! (begin (set fib-count (+ fib-count 1)) (if (= 1 i) (list2 1 0)!! (let* ((fibs (fib* (- i 1)))!!! (fib_i (car fibs))!!! (fib_i-1 (cadr fibs)))!! (list2 (+ fib_i fib_i-1) fib_i))))))) (car (fib* n)))) -> (fib 10) 55 -> fib-count 10 7 8

Trees and recursion Let's do binary search trees. We need a slightly different rep from the textbook's: (1 () (5 (2 () ()) ())) (define empty? (tree) (null? tree)) (define left (node) (cadr node)) (define right (node) (caddr node)) (define label (node) (if (empty? node) node (car node))) (val empty_tree '()) Searching Searching is easy - follow the data structure (define lookup (v t) (if (empty? t) #f (if (= v (label t))? (if (< v (label t))?? )))) 9 10 Also easy Insertion (define insert (v t) (if (empty? t) (list3 v '() '()) (if (< v (label t))?? ))) Examples -> (val t1 (insert 2 (insert 1 (insert 5 empty_tree)))) (5 (1 () (2 () ())) ()) -> (val t2 (insert 2 (insert 5 (insert 1 empty_tree)))) (1 () (5 (2 () ()) ())) -> (lookup 0 t2) #f -> (lookup 5 t2) #t (return to lambda) 11 12

Traversals Preorder, "level-order", and so on are left as exercises for the reader ;->...but I will take questions In uscheme we don't need define lambda -> (val plus (lambda (x y) (+ x y))) plus -> (plus 2 3) 5 -> define is "syntactic sugar"; functions are just values, bound in the value environment like other values 13 14 The power of lambda At the top level, not interesting Used for local function definition, more interesting We can pass functions as parameters...and - more interesting - return them as results Creating functions Remember the binary search trees We can create functions in our programs, e.g. -> (val look_t2 (lambda (v) (lookup v t2))) look_t2 -> (look_t2 5) #t -> (look_t2 7) #f 15 16

Functions on the fly Closures -> (define look_in (t) (lambda (v) (lookup v t))) look_in -> (val look_t2 (look_in t2)) <procedure> -> (look_t2 7) #f -> (look_t2 2) #t We know what v means - a formal parameter that will be bound when the function is applied - but what does t mean in (lambda (v) (lookup v t)))? 17 18 Values for free variables Mutation and closures Answer: it depends on the environment Evaluating a lambda-expression requires capturing the environment in a closure We don't write closures explicitly; the interpreter pairs the lambda-expression with the current environment: (lambda (v) (lookup v t)),{x ()} I just lied to you......we can't bind variables to values in environments Because of assignment, we bind variables to locations Bindings in a closure don't change, contents of locations can 19 20

Example A pair of functions that manipulate a counter (val resettable-counter-from (lambda (n) (list2 (lambda () (set n (+ n 1))) (lambda () (set n 0))))) (define step (c) ((car c))) (define reset (c) ((cadr c))) (val ten (resettable-counter-from 10)) (val twenty (resettable-counter-from 20)) -> (step ten) 11 -> (step twenty) 21 Preview of next week Closures for first-class functions Higher-order programming, e.g. (foldl (lambda (n t) (insert n t)) empty_tree '(1 5 2 4 3)))... instead of (insert 3 (insert 4... (insert 1 '()))))) 21 22