Abstraction: Procedures as Parameters CMSC 11500 Introduction to Computer Programming October 14, 2002.

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
CMSC 330: Organization of Programming Languages
Data Abstraction: Sets Binary Search Trees CMSC Introduction to Computer Programming October 30, 2002.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
Functional Programming: Lisp MacLennan Chapter 10.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Data Abstraction: Sets
Additional Scheme examples
CS 550 Programming Languages Jeremy Johnson
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
6.001 SICP Object Oriented Programming
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
The interpreter.
PPL Lazy Lists.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Closures and Streams cs784(Prasad) L11Clos
Env. Model Implementation
Original material by Eric Grimson
Introduction to Functional Programming in Racket
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture 18 Infinite Streams and
FP Foundations, Scheme In Text: Chapter 14.
Lecture 18.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Closure Closure binds a first-class function and a lexical environment together This is a complex topic, so we will build up our understanding of it we.
Lecture #8 מבוא מורחב.
6.001 SICP Further Variations on a Scheme
Explicit Application of Procedures, and Explicit Evaluation
CSCE 314: Programming Languages Dr. Dylan Shell
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
Announcements Quiz 5 HW6 due October 23
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Changing Data: (Continued)
Lecture 25: The Metacircular Evaluator Eval Apply
Bringing it all Together: Family Trees
Presentation transcript:

Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002

Roadmap Motivation: Two too similar procedures? Procedural Abstraction Laziness is a virtue –Procedures as parameters Contracts –Unnamed procedures: Lambda Scope –Binding variables –Creating local variables: Let

Two Too Similar Procedures (define (square x) (* x x)) (define (squarelist alon) (cond ((null? alon) ‘()) (else (cons (square (car alon)) (squarelist (cdr alon)))))) (define (double x) (+ x x)) (define (doublelist alon) (cond ((null? alon) ‘()) (else (cons (double (car alon)) (doublelist (cdr alon))))))

Procedure Comparison Both procedures: –Consume a list –Perform some operation on each element –Return new list Differences: –Different name –Different operation –Different recursive call

(define (abstractlist alon) (cond ((null? alon) ‘()) (else (cons (abstractop (car alon)) (abstractlist (cdr alon)))) Abstract Procedure Problem: –Want different functions for “abstractop”

Solution: Procedures as Parameters Can pass a procedure as a parameter –Bind to formal parameter (define (map proc alist) (cond ((null? alist) ‘()) (else (cons (proc (car alist)) (map proc (cdr alist)))))) (define (squarelist alist) (map square alist)) (define (doublelist alist) (map double alist))

Why Abstract? Laziness can be a virtue –Abstract procedures localize key structure Creates single point of control Changes can be made in single location –Generally simplifies programs –Avoid copying and modifying code segments

Contracts Describe input and output requirements of function –fn: { … } –where, are types of input & output Original: squarelist: alon alon Now: map: (X -> Y) (listof X) -> (listof Y) –where X,Y are parameters - any type

More Examples Triplelist Scale list Add2 list

Unnamed Functions: Lambda Issue: Defining lots of trivial procedures –E.g. add2, double, triple… Solution: unnamed function Can’t be recursive (lambda ( {.. }) exp) E.g. “Add2” (lambda (x) (+ x 2)) Apply like other procedures –((lambda (x) (+ x 2)) 10) -> 12

Redefining with Lambda (define (doublelist alon) (map (lambda (x) (+ x x)) alon)) (define (triplelist alon) (map ???? alon)) (define (scalelist scale alon) (map ???? alon))

Abstraction with Product & Sum

Let: Defining Local Variables Issue: Store partial results Solution: Embed in unnamed procedure –Parameters bind names to values Let easier to read e.g. f(x,y)=x(1+xy)^2+y(1-y)+(1+xy)(1-y) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b))))) (define (f x y) (lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y))(- 1 y)))

Scope: What’s in a Name? Where does a variable get its value?

Summary Procedural abstraction –Procedures as parameters Contracts –Single point of control Laziness is a virtue –Unnamed functions with lambda

Next Time Defining local variables with let –Scope Bound and free variables Procedures as return values