Class 6: Efficiency in Scheme

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

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

Streams and Evalutation Strategies

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

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

CS450 - Structure of Higher Level Languages

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

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

CS1 Recitation. Week 2

CSC324 Functional Programming Efficiency Issues, Parameter Lists

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Programming Languages. Function-Closure Idioms. Adapted from Dan Grossman's PL class, U. of Washington

The Art of Recursion: Problem Set 10

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

CS61A Notes Disc 11: Streams Streaming Along

University of Massachusetts Lowell

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

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

Lecture 5: Lazy Evaluation and Infinite Data Structures

bindings (review) Topic 18 Environment Model of Evaluation bindings (review) frames (review) frames (review) frames (review) x: 10 y: #f x: 10

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

An introduction to Scheme

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

Computing PREDICT and FOLLOW sets

TAIL CALLS, ITERATORS, AND GENERATORS 11

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

Chapter 1. Fundamentals of Higher Order Programming

Introduction to Functional Programming

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

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

Math 4242 Fibonacci, Memoization lecture 13

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

Measuring Efficiency

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

An Introduction to Scheme

CS3 Midterm 2 Standards and Solutions

Introduction to Scientific Computing

Berkeley Scheme s OOP

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

Discussion 11. Streams

Comp215: More Recursion

For this chapter, switch languages in DrRacket to Advanced Student Language.

# track total function calls using a global variable global fibcallcounter fibcallcounter +=1

Tail Recursion and Accumulators

Freeing memory is a pain. Need to decide on a protocol (who frees what?) Pollutes interfaces Errors hard to track down

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

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

Principles of Programming Languages

Project 2: Scheme Interpreter

A brief tour of history

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

miniadapton A Minimal Implementation of Incremental Computation in Scheme Dakota Fisher, Matthew Hammer, William E. Byrd, Matthew Might

ENVIRONMENT DIAGRAMS AND RECURSION 2

Part I Basic Concepts 1

Halting Measures and Termination Arguments

Code example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.

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

Implementing Recursion

Environment Diagrams and Recursion Fall 2017 Discussion 2: September 6, 2017 Solutions. 1 More Environment Diagrams

6.001 Notes: Section 4.1

CS3: Introduction to Symbolic Programming. Lecture 8: Introduction to Higher Order Functions. Spring 2008 Nate Titterton

MEMOIZATION, RECURSIVE DATA, AND SETS

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

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

CSCC24 Functional Programming Scheme Part 2

CS1 Lecture 15 Feb. 18, 2019

Freeing memory is a pain. Need to decide on a protocol (who frees what?) Pollutes interfaces Errors hard to track down

More About Recursive Data Types

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

4.2 Variations on a Scheme -- Lazy Evaluation

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

CS2500 Exam 2 Fall 2011

Hints for Exercise 4: Recursion

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

INTERPRETERS AND TAIL CALLS 9

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

Scheme as implemented by Racket

(scheme-1) has lambda but NOT define

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

15-122: Principles of Imperative Computation, Fall 2015

ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion

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

Data Structures And Algorithms

ITERATORS AND STREAMS 9

This lecture covers: Prolog s execution strategy explained more precisely. Revision of the elementary Prolog data types

1.3. Conditional expressions To express case distinctions like

Lambda in different languages

Scope, Functions, and Storage Management

CMSC 330, Fall 2013, Practice Problems 3

Module 05: Types of recursion

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Unit #2: Recursion, Induction, and Loop Invariants

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

JVM ByteCode Interpreter

Recursive Search with Backtracking

CSE : Python Programming

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Transcription:

Class 6: Efficiency in Scheme SI 413 - Programming Languages and Implementation Dr. Daniel S. Roche United States Naval Academy Fall 2011 Roche (USNA) SI413 - Class 6 Fall 2011 1 / 10

Objects in Scheme We can use closures and mutation to do OOP in Scheme! Roche (USNA) SI413 - Class 6 Fall 2011 2 / 10

Objects in Scheme We can use closures and mutation to do OOP in Scheme! Recall the counter example from last class: (define (make-counter) (let ((count 0)) (lambda () (set! count (+ 1 count)) (display count) (newline)))) To add methods, the (first) argument to the returned lambda will be the name of the method we want to apply. Roche (USNA) SI413 - Class 6 Fall 2011 2 / 10

Objects in Scheme We can use closures and mutation to do OOP in Scheme! More sophisticated counter: (define (make-counter-obj) (let ((count 0)) (lambda (command) (cond [(symbol=? command get) count] [(symbol=? command inc) (set! count (+ 1 count))] [(symbol=? command reset) (set! count 0)])))) The object now has three methods: get, inc, and reset. Roche (USNA) SI413 - Class 6 Fall 2011 3 / 10

Built-in Data Structures Scheme has some useful built-in data structures: Arrays (called vectors ). (define A (make-vector 5)) (vector-set! A 3 something) (vector-ref A 3) ; produces something (vector-ref A 5) ; error: out of bounds Roche (USNA) SI413 - Class 6 Fall 2011 4 / 10

Built-in Data Structures Scheme has some useful built-in data structures: Arrays (called vectors ). (define A (make-vector 5)) (vector-set! A 3 something) (vector-ref A 3) ; produces something (vector-ref A 5) ; error: out of bounds Hash tables (define H (make-hash)) (hash-set! H 2 something) (hash-set! H (list 20 #f) crazy!) (hash-ref H (20 #f)) ; produces crazy! (hash-ref H (bad)) ; error: no key (bad) Roche (USNA) SI413 - Class 6 Fall 2011 4 / 10

Efficiency in Scheme: Fibonacci numbers Recall the problem of computing Fibonacci numbers from lab 1. (define (fib n) (if (<= n 1) 1 (+ (fib (- n 1)) (fib (- n 2))))) Why is this function so slow? Roche (USNA) SI413 - Class 6 Fall 2011 5 / 10

Memoization in Scheme Memoization is remembering the results of previous function calls. Why is functional programming perfect for memoization? Roche (USNA) SI413 - Class 6 Fall 2011 6 / 10

Memoization in Scheme Memoization is remembering the results of previous function calls. Why is functional programming perfect for memoization? No side effects! Roche (USNA) SI413 - Class 6 Fall 2011 6 / 10

Memoization in Scheme Memoization is remembering the results of previous function calls. Why is functional programming perfect for memoization? No side effects! In Scheme we can use vectors or hashes to provide this functionality. Some languages have built-in memoization. Roche (USNA) SI413 - Class 6 Fall 2011 6 / 10

Memoizing Fibonacci Here s how we might memoize the Fibonacci function: (define fib-hash (make-hash)) (define (fib-memo n) (let ((saved (hash-ref fib-hash n unset))) (if (equal? saved unset) (let ((res (if (<= n 1) 1 (+ (fib-memo (- n 1)) (fib-memo (- n 2)))))) (hash-set! fib-hash n res) res) saved))) Roche (USNA) SI413 - Class 6 Fall 2011 7 / 10

Stack space in recursive calls Recursive calls can use a lot of memory, even when the results are puny. ;; Sum of squares from 1 to n (define (ssq n) (if (= n 0) 0 (+ (sqr n) (ssq (- n 1))))) Why does (ssq 4000000) run out of memory? Roche (USNA) SI413 - Class 6 Fall 2011 8 / 10

Stack space in recursive calls This function does the same thing, but takes an extra argument that serves as an accumulator. ;; Sum of squares using tail recursion (define (ssq-better n accum) (if (= n 0) accum (ssq-better (- n 1) (+ (sqr n) accum)))) Now (ssq-better 4000000 0) actually works! Roche (USNA) SI413 - Class 6 Fall 2011 9 / 10

Tail recursion The second version worked because there was no need to make a stack of recursive calls. A function is tail recursive if its output expression in every recursive case is only the recursive call. In Scheme, this means the recursive call is outermost in the returned expression. ssq-better is better because it is tail recursive! Roche (USNA) SI413 - Class 6 Fall 2011 10 / 10

Tail recursion for Fibonacci To implement tail recursion we usually make a helper function: (define (fib-tail-helper n i fib-of-i fib-of-i+1) (if (= i n) fib-of-i (fib-tail-helper n (+ i 1) fib-of-i+1 (+ fib-of-i fib-of-i+1)))) The main function then becomes: (define (fib-tail n) (fib-tail-helper n 0 1 1)) Roche (USNA) SI413 - Class 6 Fall 2011 11 / 10