CS1 Recitation. Week 2

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

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

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

RECURSION, RECURSION, (TREE) RECURSION! 2

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

RECURSION, RECURSION, (TREE) RECURSION! 3

University of Massachusetts Lowell

An introduction to Scheme

Functional Programming. Pure Functional Programming

6.001 Notes: Section 4.1

CS450 - Structure of Higher Level Languages

6.001 Recitation 23: Register Machines and Stack Frames

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

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

ENVIRONMENT DIAGRAMS AND RECURSION 2

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

Halting Measures and Termination Arguments

The power of logarithmic computations. Recursive x y. Calculate x y. Can we reduce the number of multiplications?

Class 6: Efficiency in Scheme

Problem Set CVO 103, Spring 2018

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

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

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

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

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

CS 61B Data Structures and Programming Methodology. June David Sun

Higher-Order Functions (Part I)

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

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

RECURSION, RECURSION, (TREE) RECURSION! 3

Lecture 5: Lazy Evaluation and Infinite Data Structures

CSC324 Functional Programming Efficiency Issues, Parameter Lists

Chapter 1. Fundamentals of Higher Order 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

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

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

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

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

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

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström

More About Recursive Data Types

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

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Functional Programming Languages (FPL)

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science. Issued: Wed. 26 April 2017 Due: Wed.

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

1.3. Conditional expressions To express case distinctions like

CS 310 Advanced Data Structures and Algorithms

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

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

User-defined Functions. Conditional Expressions in Scheme

Recursive Definitions

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Denotational Semantics. Domain Theory

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

Higher-Order Functions

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Measuring Efficiency

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

Discussion 12 The MCE (solutions)

CS61A Midterm 2 Review (v1.1)

Forward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack

State. Substitution relies on an identifer having a fxed value

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

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

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

CS 842 Ben Cassell University of Waterloo

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

CS1 Lecture 15 Feb. 18, 2019

CS 470 Spring Mike Lam, Professor. OpenMP

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

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

Introduction to Functional Programming

CS 1110: Introduction to Computing Using Python Loop Invariants

COP4020 Programming Assignment 1 - Spring 2011

An Explicit-Continuation Metacircular Evaluator

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

CS 470 Spring Mike Lam, Professor. OpenMP

G Programming Languages - Fall 2012

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

DM502 Programming A. Peter Schneider-Kamp.

Anatomy of a static method, S&W, 2.1, figure page 188. Class.name or just name Scope, S&W, 2.1, figure page 189. Overloading

An Elegant Weapon for a More Civilized Age

Homework. Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25

CSE341 Spring 2017, Final Examination June 8, 2017

Lexical vs. Dynamic Scope

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

Introduction. C provides two styles of flow control:

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Building a system for symbolic differentiation

ITERATION AND RECURSION 3

Operators in java Operator operands.

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Transcription:

CS1 Recitation Week 2

Sum of Squares Write a function that takes an integer n n must be at least 0 Function returns the sum of the square of each value between 0 and n, inclusive Code: (define (square x) (* x x)) (define (sum-squares n) (if (= 0 n) 0 (+ (square n) (sum-squares (- n 1)))))

Sum of Squares (2) Code: (define (sum-squares n) (if (= 0 n) 0 (+ (square n) (sum-squares (- n 1))))) What kind of process is this? Linear recursive Long chain of deferred operations (sum-squares 5) (+ 25 (+ 16 (+ 9 (+ 4 (+ 1 0))))) Why? Recursive call is part of an operand in this function Computation for this step can t complete until recursion ends

Iterative Sum of Squares Need some state to convert to linear iterative process Will still have a recursive function though! State to maintain? Sum computed so far Maximum value to process Code: (define (isum-squares n) (iss-helper 0 n)) (define (iss-helper sum n) (if (= 0 n) sum (iss-helper (+ (square n) sum) (- n 1))))

Iterative Sum of Squares (2) Code: (define (isum-squares n) (iss-helper 0 n)) (define (iss-helper sum n) (if (= 0 n) sum (iss-helper (+ (square n) sum) (- n 1)))) Any deferred operations? No (why?) Recursive call is in the operator, not in an operand All work for this step is completed before recursive call is made Linear iterative process Space requirement is constant

Linear Recursive vs. Linear Iterative Both linear recursive process and linear iterative process use recursive procedures Term is about how the computation unfolds, not whether the function calls itself Linear recursive process Produces deferred operations Linear iterative process Uses state variables to avoid deferred ops. Often implemented with a helper function Helper function includes the state vars, is called by a wrapper

Linear Recursive vs. Linear Iterative (2) Linear recursive sum-of-squares: (define (sum-squares n) (if (= 0 n) 0 (+ (square n) (sum-squares (- n 1))))) Linear iterative sum-of-squares: (define (isum-squares n) (iss-helper 0 n)) (define (iss-helper sum n) (if (= 0 n) sum Produces deferred operations State variable, updated before recursion (iss-helper (+ (square n) sum) (- n 1))))

Increment/Decrement Function Write a function to increment or decrement the input value, based on a flag Select inc/dec with an input flag #t = increment, #f = decrement Code: (define (incdec val flag) (if flag (+ val 1) (- val 1))) A bit repetitive

Functions As Results In Scheme, an expression s result can also be a function, not just a value + and are Scheme functions Original incdec code: (define (incdec val flag) (if flag (+ val 1) (- val 1))) New incdec code: (define (incdec val flag) ((if flag + -) val 1)) When expression for operator is evaluated, flag value selects + or function to apply to operands

Functions As Results (2) Evaluate (incdec 3 #t) Evaluate 3 3, evaluate #t #t Evaluate incdec (lambda (val flag) ((if flag + -) val 1)) Apply lambda expression to 3, #t Substitute 3 for val, #t for flag in ((if flag + -) val 1) ((if #t + -) 3 1) Evaluate ((if #t + -) 3 1) Evaluate 3 3, evaluate 1 1 Evaluate (if #t + -) if is a special form. Evaluate first argument. Evaluate #t #t #t is true so evaluate second argument: + + Apply + to 3, 1 4

cond Expressions Form: (cond (test 1 exp 1 ) (test 2 exp 2 )... (else exp N )) If first test evaluates to #t then first expression is evaluated Else if second test evaluates to #t then second expression is evaluated etc. If no tests evaluate to true then else expression is evaluated (if present)

Fibonacci Series with cond Definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n 1) + fib(n 2) Scheme code: (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2))))))

Pascal s Triangle Write a function to compute values in Pascal s Triangle (pascal-coefficient level n) First level is 0 First value in a level is index 0 Indexes in a level L are 0..L 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 base cases Follow pattern for designing recursive operations Identify base case(s) Figure out out decomposition step How to compute a given level and index, using recursive function calls and simple operations?

Coding Style Good code, bad code focuses on naming Short function/variable names may save typing up front They waste more time during maintenance/bug-fixing! Ideally, code is self-documenting Names should clearly describe what is going on factor? vs. fac? or f? Don t be over-verbose! is-arg-one-a-factor-of-arg-two? Some short names are OK because they re widely used x is usually fine for a single decimal value, e.g. (square x) n for an integer maximum value, e.g. (sum-ints n) i for a variable that iterates over a sequence of integer values