CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

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

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

CS1 Recitation. Week 2

Recursion. The Recursion Pattern. Recursion 3/16/14

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

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

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

RECURSION (CH 5) A pattern for solving algorithm design problems

CS 314 Principles of Programming Languages. Lecture 16

Functional Programming - 2. Higher Order Functions

CS 1101 Exam 3 A-Term 2013

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:

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

More About Recursive Data Types

Module 5: Lists. Readings: HtDP, Sections 9, 10.

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

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

Bucket-Sort and Radix-Sort

CS 206 Introduction to Computer Science II

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

CS450 - Structure of Higher Level Languages

Recursion I and II. Discrete Structures (CS 173) Madhusudan Parthasarathy, University of Illinois 1

Building a system for symbolic differentiation

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

Bucket-Sort and Radix-Sort

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

CS 206 Introduction to Computer Science II

Chapter 1. Fundamentals of Higher Order Programming

ITERATORS AND STREAMS 9

An introduction to Scheme

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

CS 314 Principles of Programming 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))

Comp 311: Sample Midterm Examination

Recursive Definitions

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

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

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

PDF created with pdffactory Pro trial version Recursion

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

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Data Structures and Algorithms in Java

Working with recursion

Recursion & Iteration

Lecture 4. Hashing Methods

Programming II (CS300)

Halting Measures and Termination Arguments

A Brief Introduction to Scheme (II)

1.3. Conditional expressions To express case distinctions like

Lab 7: OCaml 12:00 PM, Oct 22, 2017

CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 1

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

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

Module 3: New types of data

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

Recursion CS GMU

CS 314 Principles of Programming Languages

Programming II (CS300)

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

CS 5010 Program Design Paradigms Lesson 6.1

ALGORITHM DESIGN DYNAMIC PROGRAMMING. University of Waterloo

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

Functional Programming Languages (FPL)

Lists. Readings: HtDP, sections 9 and 10. Avoid 10.3 (uses draw.ss). CS 135 Winter : Lists 1

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

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

ormap, andmap, and filter

Programming II (CS300)

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

User-defined Functions. Conditional Expressions in Scheme

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

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

Module 8: Local and functional abstraction

6.001 Notes: Section 4.1

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

RECURSION. Problem Solving with Computers-II 6

Data Structures And Algorithms

q To develop recursive methods for recursive mathematical functions ( ).

q To develop recursive methods for recursive mathematical functions ( ).

Recursive definition: A definition that is defined in terms of itself. Recursive method: a method that calls itself (directly or indirectly).

Local defini1ons. Func1on mul1ples- of

YOUR NAME PLEASE: *** SOLUTIONS ***

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

CS 206 Introduction to Computer Science II

How to Design Programs

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

Functional Programming. Pure Functional Programming

CS61A Notes Disc 11: Streams Streaming Along

Streams and Evalutation Strategies

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

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

Graphs. Directed graphs. Readings: Section 28

Module 8: Binary trees

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

Module 10: General trees

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

Graphs. Readings: Section 28. CS 135 Fall : Graphs 1

Problem Set CVO 103, Spring 2018

Transcription:

CS115 INTRODUCTION TO COMPUTER SCIENCE 1 Additional Notes Module 5

Example my-length (Slide 17) 2 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length empty) alos is replaced with empty [(empty? empty) 0] [else (+ 1 (my-length (rest empty)))]) [true 0] [else (+ 1 (my-length (rest empty)))]) from Module 3, cond substitution rules: [true exp] ) exp 0

Example my-length (Slide 17) 3 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length (cons 'a (cons 'b empty))) alos is replaced with (cons 'a (cons 'b empty))) [(empty? (cons 'a (cons 'b empty))) 0] [else (+ 1 (my-length (rest (cons 'a (cons 'b empty)))))]) [false 0] [else (+ 1 (my-length (rest (cons 'a (cons 'b empty)))))]) from Module 3, cond substitution rules: [false ][exp1 exp2] ) [exp1 exp2] )

Example my-length (Slide 17) 4 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length (cons 'a (cons 'b empty))) [else (+ 1 (my-length (rest (cons 'a (cons 'b empty)))))]) from Module 3, cond substitution rules: [else exp]) exp (+ 1 (my-length (rest (cons 'a (cons 'b empty))))) (+ 1 (my-length (cons 'b empty))) we need to function my-length again

Example my-length (Slide 17) 5 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length (cons 'a (cons 'b empty))) (+ 1 alos is replaced with (cons 'b empty)) [(empty? (cons 'b empty)) 0] [else (+ 1 (my-length (rest (cons 'b empty))))])) (+ 1 [false 0] [else (+ 1 (my-length (rest (cons 'b empty))))])) (+ 1 [else (+ 1 (my-length (rest (cons 'b empty))))]))

Example my-length (Slide 17) 6 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length (cons 'a (cons 'b empty))) (+ 1 (+ 1 (my-length (rest (cons 'b empty))))) we need to function my-length again (+ 1 (+ 1 (my-length empty ))) (+ 1 (+ 1 [(empty? empty) 0] [else (+ 1 (my-length (rest empty)))])))

Example my-length (Slide 17) 7 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length (cons 'a (cons 'b empty))) (+ 1 (+ 1 [true 0] [else (+ 1 (my-length (rest empty)))]))) (+ 1 (+ 1 0)) (+ 1 1) 2

Example: Factorial 8 f ( n) n f 1 ( n 1) if n else 0 base case f(2) = 2 x f(1) f(1) = 1 x f(0) f(0) = 1 Finally, to solve f(2), we now have: f(2) = 2 x 1 x 1 f(2) = 2

Example: Factorial 9 Visualizing Recursion using Factorial Example Example recursion trace: return 4 * 6 = 24 final answer recursivefactorial ( 4 ) return 3 * 2 = 6 recursivefactorial ( 3 ) return 2 * 1 = 2 recursivefactorial ( 2 ) return 1 * 1 = 1 recursivefactorial ( 1 ) return 1 recursivefactorial ( 0 ) Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser, Data Structures and Algorithms in Java 6/e

Example: Factorial 10 In DrRacket: (define (f n) [(equal? n 0) 1] [else (* n (f (- n 1)))])) recursion base case f ( n) n f 1 ( n 1) if n else 0

Removing Elements from a List Slide 41 11 (define (removal n alon) [(empty? alon) empty] [else example: remove 5 5 4 5 9 first [(equal? (first alon) n) (removal n (rest alon))] [else (cons (first alon) (removal n (rest alon)))])])) is alon empty? Yes, return empty and exit removal function (base case) No, check the following cases: is n equal to first element in alon? Yes Call removal and pass the rest of alon No Create a new list with first element of alon and removal to check on the rest of the elements in alon

Example: Fibonacci 12 Fibonacci series: 0, 1, 1, 2, 3, 5, 8... Every number is the sum of the previous two f(0) = 0 f(1) = 1 base cases fib( n ) = fib( n - 1 ) + fib( n 2 ) Fibonacci in DrRacket (define (f n) [(or (equal? n 0) (equal? n 1)) n] [else (+ (f (- n 1)) (f (- n 2)))]))