Structure and Interpretation of Computer Programs Presented by Yan Yan CSE 294, UCSD, April 13, 2011 -- Chapter 1.1-1.2 Harold Abelson, Gerald and Julie.

Slides:



Advertisements
Similar presentations
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Advertisements

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
ISBN Chapter 15 Functional Programming Languages.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
ISBN Chapter 15 Functional Programming Languages.
1 Structure and Interpretation of Computer Programs Modularity, Objects, and State (part 1) Abelson & Sussman 2 UC San Diego CSE 294 May 11, 2011 Barry.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
ISBN Chapter 15 Functional Programming Languages.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
What is the main focus of this course? This course is about Computer Science Geometry was once equally misunderstood. Term comes from ghia & metra or earth.
Building Abstractions with Variables (Part 1) CS 21a: Introduction to Computing I First Semester,
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
From Lambda Calculus to LISP Functional Programming Academic Year Alessandro Cimatti
5.3 EVALUATION OF POSTFIX EXPRESSION For example, consider the evaluation of the following postfix expression using stacks: abc+d*f/- where, a=6, b=3,
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Functional Programming Languages
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Computer Science 4 Mr. Gerb
Closures and Streams cs784(Prasad) L11Clos
CS21b: Structure and Interpretation
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Important Concepts from Clojure
Important Concepts from Clojure
Control Flow Chapter 6.
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Functional Programming
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Abstraction and Repetition
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
Recursion Taken from notes by Dr. Neil Moore
Important Concepts from Clojure
Abstraction and Repetition
Closures and Streams cs7100(Prasad) L11Clos
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
Introduction to the Lab
Chapter 15 Functional Programming 6/1/2019.
Abstraction and Repetition
Lecture 6 - Recursion.
Presentation transcript:

Structure and Interpretation of Computer Programs Presented by Yan Yan CSE 294, UCSD, April 13, Chapter Harold Abelson, Gerald and Julie Sussman

The Idea of Computational Process Process – Abstraction of execution in computers. Data – Abstract things manipulated by processes. Program – A pattern of rules that directs the evolution of a process. About Clojure

Building Abstractions with Procedures Elements of Programming –P–Primitive expressions –M–Means of combinations –M–Means of abstractions Procedures and Processes –R–Recursive Processes and Iterative Processes –R–Recursive Procedures

The Elements of Programming Every powerful language should have the following three mechanisms to accomplish the goal of “combining simple ideas to form more complex ones” – Primitive expressions the simplest entities the language is concerned with. – Means of combination by which compound elements are built from simpler ones. – Means of abstraction by which compound elements can be named and manipulated as units.

Primitive Expressions and Combinations Primitive Expressions – e.g. 5, 1.2, false – e.g. +, mod, or Combinations – e.g. (+ 10 5) – e.g. (not true)

Naming and Environment Naming – To identify a variable whose value is the object. – e.g. (def my-age 18) Then we can refer to 18 by name: (+ 5 my-age) Environment – The interpreter needs to maintain the “memory” to keep track of the “name-object” pairs. – Such “memory” is called the environment.

Compound Procedures The abstraction technique by which a compound operation can be given a name and then referred to as a unit, e.g. - ( defn square [ z ] ( * z z ) ) ( defn square-sum [ x y ] ( + (square x) (square y) ) ( defn f [ a ] ( square-sum (+ a 2) (- a 1) ) )

Evaluating a combination ( + (- 4 2) (/ 8 2) ) ( + 2 (/ 8 2) ) ( + 2 4) 6

Evaluating Compound Procedures Substitution for procedure application: f (5) (square-sum (+ a 2) (- a 1)) (where a = 5)  (square-sum (+ 5 2) (- 5 1)) (square-sum 7 (- 5 1)) (square-sum 7 4) (+ (square 7) (square 4)) (+ (* 7 7) (square 4)) (+ 49 (square 4)) (+ 49 (* 4 4)) ( ) 65

Applicative Order versus Normal Order Applicative order f (5)  (square-sum (+ a 2) (- a 1)) (square-sum (+ 5 2) (- 5 1)) (square-sum 7 4) (+ (square 7) (square 4)) (+ (* 7 7) (* 4 4)) ( ) 65 Normal order f (5)  (square-sum (+ 5 2) (- 5 1)) (+ (square (+ 5 2) ) (square (- 5 1)) ) (+ (* (+ 5 2) (+ 5 2) ) (* (- 5 1) (- 5 1) ) ) (+ (* 7 7) (* 4 4)) ( ) 65

An example from Chap. 4 (defn test-order [ a b ] ( if true a b)) Test which order Clojure evaluation is using: Test this: (test-order 1 (/ 1 0))

Conditional Expressions Case analysis (cond ) If : – a special case of cond (binary branch)

Procedure as Black-box Abstraction A procedure definition should be able to suppress detail. – e.g. (defn compute-hypt [a b] (square-root (square-sum a b )))

Outline Elements of Programming –P–Primitive expressions –M–Means of combinations –M–Means of abstractions Procedures and Processes –R–Recursive Processes and Iterative Processes –R–Recursive Procedures

Procedures and the Processes A procedure is a pattern for specifying the evolution of a computational process. The process generated is the abstraction of program execution.

Linear Recursion (defn factorial [n] ;compute n! (if (= n 1) 1 (* n (factorial (- n 1)))))

Linear Recursion (factorial 6) (* 6 (factorial 5)) (* 6 (* 5 (factorial 4))) (* 6 (* 5 (* 4 (factorial 3)))) (* 6 (* 5 (* 4 (* 3 (factorial 2))))) (* 6 (* 5 (* 4 (* 3 (* 2 (factorial 1)))))) (* 6 (* 5 (* 4 (* 3 (* 2 1))))) (* 6 (* 5 (* 4 (* 3 2)))) (* 6 (* 5 (* 4 6))) (* 6 (* 5 24)) (* 6 120) 720

Linear Iteration (defn factorial [n] (fact-iter 1 1 n)) ;compute n! (defn fact-iter [product counter max-count] (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count)))

Linear Iteration (factorial 6) (fact-iter 1 1 6) (fact-iter 1 2 6) (fact-iter 2 3 6) (fact-iter 6 4 6) (fact-iter ) (fact-iter ) (fact-iter ) 720 Theoretically true, but,no actually.. (We’ll revisit Linear Iteration later.)

Recursive Procedure The procedure that refers to itself – When we describe a procedure as recursive, we are referring to the syntactic fact that the procedure definition refers to the procedure itself. Recursive processes and iterative processes can both be generated from recursive procedure.

Tree Recursion Fibonacci number: 0, 1, 1, 2, 3, 5, 8, 13, 21, …

Tree Recursion (defn fib [n] (cond (= n 0) 0 (= n 1) 1 :else ( + (fib (- n 1)) (fib (- n 2))) )) ;Let’s compute (fib 5) --

Tree Recursion

To Linear Iteration? 1. fixed number of state variables? yes 2. fixed rules of state transition? yes 3. end condition? yes Then, we are ready to go -- (defn fib [n] (fib-iter 0 1 n)) (defn fib-iter [curr next count] (if (= count 0) curr (fib-iter next (+ curr next) (- count 1)))) 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …

Tail Recursion Defined recursively, but the recursion must come at the tail. (defn fib [n] (fib-iter 1 0 n)) (defn fib-iter [curr next count] (if (= count 0) curr (fib-iter next (+ curr next) (- count 1))))

Tail Recursion JVM doesn’t support TCO automatically, so (fib ) will lead to a stack overflow In Clojure, make tail calls explicit by using “recur”: (defn fib [n] (fib-iter 1 0 n)) (defn fib-iter [curr next count] (if (= count 0) curr (recur next (+ curr next) (- count 1))))

Summary Elements of Programming – Expressions and Combinations – Naming and Environments – Evaluations – Procedure as Black-box Abstractions Procedures and Processes – Recursion and Iteration – Tail Recursion

Q&A