RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Similar documents
CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

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

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

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Discussion 11. Streams

Repetition Through Recursion

What s the base case?

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

Drawing Hands, by M. C. Escher (lithograph, 1948)

Text Input and Conditionals

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

More About Recursive Data Types

CS1102: Macros and Recursion

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

Problem 1. Remove consecutive duplicates (6 points, 11 mintues)

Trombone players produce different pitches partly by varying the length of a tube.

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

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

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

CS450 - Structure of Higher Level Languages

Recursively Enumerable Languages, Turing Machines, and Decidability

Building a system for symbolic differentiation

Functions that return lists

Solutions and grading standards

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

Part II Composition of Functions

Department of Electrical Engineering and Computer Sciences Spring 2001 Instructor: Dan Garcia CS 3 Midterm #2. Personal Information

CS3: Introduction to Symbolic Programming. Lecture 5:

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Input, output, and sequence

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Have the students look at the editor on their computers. Refer to overhead projector as necessary.

6.001 Notes: Section 4.1

Animations involving numbers

RECURSION, RECURSION, (TREE) RECURSION! 3

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

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017

1.7 Limit of a Function

Building a system for symbolic differentiation

SCHEME AND CALCULATOR 5b

RECURSION, RECURSION, (TREE) RECURSION! 2

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

CS 3 Midterm 1 Review

Unit 7. 'while' Loops

(first (hello)) (hello) CS61A Lecture 2. Computer Science. Hierarchy of Abstraction. Functions. REVIEW: Two Types of( s so far

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

Introduction to Functional Programming

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

Catalan Numbers. Table 1: Balanced Parentheses

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

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

O(1) How long does a function take to run? CS61A Lecture 6

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

UNIVERSITY of CALIFORNIA at Berkeley Department of Electrical Engineering and Computer Sciences Computer Sciences Division

RECURSION 3. 1 Recursion COMPUTER SCIENCE 61A. June 30, 2016

6.001 Notes: Section 8.1

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

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

--- stands for the horizontal line.

12 Macros. Localmake. is an abbreviation for the two instructions. localmake "fred 87. local "fred make "fred 87

5.5 Completing the Square for the Vertex

Environment Diagrams. Administrivia. Agenda Step by Step Environment Diagram Stuff. The Rules. Practice it s on! What are The Rules?

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

CS1 Recitation. Week 2

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

Table of Laplace Transforms

Fundamental Concepts. Chapter 1

Topic. Section 4.1 (3, 4)

Meeting 1 Introduction to Functions. Part 1 Graphing Points on a Plane (REVIEW) Part 2 What is a function?

Midterm 2 Solutions. CS70 Discrete Mathematics and Probability Theory, Spring 2009

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

CS3L Summer 2011 Exam 2 Time: up to 170 minutes (you may leave when finished; or, you must stop promptly at noon)

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

Introduction to Parallel Computing Errata

1 Computer arithmetic with unsigned integers

Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems

(Refer Slide Time: 02.06)

Shorthand for values: variables

Algorithm Design and Recursion. Search and Sort Algorithms

step is to see how C++ implements type polymorphism, and this Exploration starts you on that journey.

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

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

In the old days, they defined procedures like this.

Iteration Preview of Coming Attractions The Need to Get Your Act Together A Process for Developing While Statements

1. Problem Representation

Module 8: Local and functional abstraction

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

RECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012

CS115 - Module 10 - General Trees

Animations that make decisions

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

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

An introduction to Scheme

Transcription:

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1 COMPUTER SCIENCE 61AS 1. What is functional programming? Give an example of a function below: Functional Programming In functional programming, you do not do assignments, functions have no memory and leave no side effects, and each function always returns the same thing each time, given the same parameter. For example, -> (square 3) 9 -> (square 3) 9 This may seem obvious, but later in this class, we will cover programming that is not functional. 2. Not all procedures are functions. Give an example of a procedure that is not functional. It doesn t have to be a procedure you ve seen before! The built-in random procedure is not functional. -> (random 10) 3 -> (random 10) 8 1

DISCUSSION 1: RACKET BASICS, ORDER OF EVALUATION, RECURSION Page 2 The Substitution Model We consider how Racket evaluates an expression by using the substitution model. For most students, the substitution model is the most intuitive way to think about expression evaluation. Note, however, that this is not how Racket actually works. As we ll find out later in the course, the substitution model eventually will become inadequate for evaluating our expressions or, more specifically, when we introduce assignments. But for now, while were still in the realm of functional programming, the substitution model will serve us nicely. 1. According to the substitution model, how should we evaluate an expression? Specifically, show how (+ 20 (square 2)) is evaluated, assuming square is already defined. First, lets review how an expression is evaluated again: (a) Evaluate all subexpressions of the combination (including the procedure and the arguments) so that we know exactly what they are. (b) Apply the evaluated procedure to the evaluated arguments that we obtained in the previous step. To perform step 2, basically, we evaluate the body of the procedure after we replace the formal parameters with the given arguments. Here s how the substitution model wants us to think: (a) First, evaluate all subexpressions. + evaluates to the addition procedure, and 20 evaluates to 20. (b) Before we can apply the + procedure, we need to evaluate (square 2). We know square is a procedure and 2 is 2. Now it s time to substitute! (c) (square 2) is replaced with the body of square, with a 2 substituted for x. (* 2 2) We would evaluate this expression in the same way as before, finally getting 4. (d) Now we just need to evaluate (+ 20 4), which gives us 24. Applicative vs. Normal Order 1. Show how (f (+ 2 1)) is evaluated in both applicative and normal order, given the definitions below.

DISCUSSION 1: RACKET BASICS, ORDER OF EVALUATION, RECURSION Page 3 (define (double x) (* x 2)) (define (square y) (* y y)) (define (f z) (+ (square (double z)) 1)) Applicative order: (f (+ 2 1)) ==> (f 3) ==> (+ (square (double 3)) 1) ==> (+ (square (* 3 2)) 1) ==> (+ (square 6) 1) ==> (+ (* 6 6) 1) ==> 37 Normal order: (f (+ 2 1)) ==> (+ (square (double (+ 2 1))) 1) ==> (+ (square (* (+ 2 1) 2)) 1) ==> (+ (* (* (+ 2 1) 2) (* (+ 2 1) 2)) 1) ==> (+ (* (* 3 2) (* 3 2)) 1) ==> (+ (* 6 6) 1) ==> (+ 36 1) ==> 37 2. In Exercise 1 above, you should have found that applicative order is more efficient than normal order. Define a procedure where normal order is more efficient. Anything where not evaluating the arguments will save time works. Most trivially, (define (f x) 3) ;; a function that always returns 3 When you call (f (fib 10000)), applicative order would choke, but normal order would just happily drop (fib 10000) and just return 3. 3. Evaluate this expression using both applicative and normal order: (square (random 5)). Do you get the same result? Why or why not?

DISCUSSION 1: RACKET BASICS, ORDER OF EVALUATION, RECURSION Page 4 Unless youre lucky, the result will be quite different. Expanding to normal order, you have (* (random x) (random x)), and the two separate calls to random will probably return different values. 4. Consider a magical function count that takes in no arguments, and each time it is invoked returns 1 more than it did before, starting with 1. Therefore, (+ (count) (count)) will return 3. Evaluate (square (square (count))) with both applicative and normal order; explain your result. For applicative order, (count) is only called once returns 1 and is squared twice. So you have (square (square 1)), which evaluates to 1. For normal order, (count) is called FOUR times: (* (square (count)) (square (count))) ==> (* (* (count) (count)) (* (count) (count))) ==> (* (* 1 2) (* 3 4)) ==> 24 1. What is recursion? The Basics of Recursion Function calls itself Simplifies a large problem Till there s a base case 2. What are three things you find in every recursive function? (a) One or more base cases (b) Way(s) to make the problem into a smaller problem of the same type (so that it can be solved recursively). (c) One or more recursive cases that solve the smaller problem and then uses that to solve the original (large) problem. 3. When you write a recursive function, you seem to call it before it has been fully defined. Why doesn t this break the Racket interpreter, or cause any infinite loops?

DISCUSSION 1: RACKET BASICS, ORDER OF EVALUATION, RECURSION Page 5 When you define a function, Racket does not evaluate the body of the function. The body of the function is evaluated only when we call the procedure. Practice with Recursion 1. Consider a function sum-every-other, which sums every other number in a sentence, starting from the second element. It should work as shown below. -> (sum-every-other (1 2 3 4 5)) 6 -> (sum-every-other (7 2 7 4 7)) 6 -> (sum-every-other (1)) 0 a. What is the domain and range of sum-every-other? The domain (what the function takes in) is a sentence of numbers. range (what the function outputs) is a single number. The b. What s wrong with the following code? Fix all mistakes you find. (define (sum-every-other sent) (if (empty? sent) 0 (+ (first sent) (sum-every-other (bf sent))))) The current code sums every number in the sentence. If you don t see why, run through a small example step-by-step. To instead sum every other element, the code should be as follows: (define (sum-every-other sent) (if (<= (count sent) 1) 0 (+ (first (bf sent)) (sum-every-other (bf (bf sent))))))

DISCUSSION 1: RACKET BASICS, ORDER OF EVALUATION, RECURSION Page 6 2. Write a procedure (expt base power) which implements the exponents function. For example, (expt 3 2) returns 9, and (expt 2 3) returns 8. (define (expt base power) (if (= power 0) 1 (* base (expt base (- power 1))))) 3. Define a procedure subsent that takes in a sentence and a parameter i, and returns a sentence with elements starting from position i to the end. The first element has i=0. -> (subsent (6 4 2 7 5 8) 3) (7 5 8) (define (subsent sent i) (if (= i 0) sent (subsent (bf sent) (- i 1)))) Note that were assuming i is valid (or, not larger than length of the sentence). 4. Consider the procedure sum-of-sents which sums the numbers in two sentences. Note, the sentences don t have to be the same size. -> (sum-of-sents (1 2 3) (6 3 9)) (7 5 12) -> (sum-of-sents (1 2 3 4 5) (8 9)) (9 11 3 4 5) a. What is the domain and range of sum-of-sents? Be as specific as possible! The domain is two sentences of numbers. The range is a sentence of numbers. b. What should your base case(s) be? Write in code a condition that tells us when our base case is reached as well as what it should return.

DISCUSSION 1: RACKET BASICS, ORDER OF EVALUATION, RECURSION Page 7 We ll have two base cases: (cond ((empty? s1) s2) ((empty? s2) s1) (...)) c. Define sum-of-sents. (define (sum-of-sents s1 s2) (cond ((empty? s1) s2) ((empty? s2) s1) (else (se (+ (first s1) (first s2)) (sum-of-sents (bf s1) (bf s2))))))