Equivalent Notations. Higher-Order Functions. (define (f x y) ( body )) = (define f (lambda (x y) ) ) Anonymous Functions.

Similar documents
Functional Programming - 2. Higher Order Functions

CS 314 Principles of Programming Languages

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

CS 314 Principles of Programming Languages

CS 415 Midterm Exam Spring 2002

NOTE: Answer ANY FOUR of the following 6 sections:

Functional Programming Lecture 1: Introduction

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

CMSC 331 Final Exam Section 0201 December 18, 2000

Chapter 5. Names, Bindings, and Scopes

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CS 415 Midterm Exam Spring SOLUTION

Functional Programming and Haskell

Concepts of Programming Languages

SOFTWARE ARCHITECTURE 6. LISP

Functional Programming. Pure Functional Programming

CS 360 Programming Languages Interpreters

MIDTERM EXAMINATION - CS130 - Spring 2005

CSC 533: Organization of Programming Languages. Spring 2005

Organization of Programming Languages CS3200/5200N. Lecture 11

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

Final-Term Papers Solved MCQS with Reference

CS558 Programming Languages

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Recap: Functions as first-class values

Introduction to Functional Programming

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

Chapter 5 Names, Binding, Type Checking and Scopes

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan

Functional Programming

CS558 Programming Languages

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Weeks 6&7: Procedures and Parameter Passing

Chapter 1. Fundamentals of Higher Order Programming

G Programming Languages - Fall 2012

CMSC 330: Organization of Programming Languages

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.

Functional Programming Languages (FPL)

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

CS 360 Programming Languages Day 14 Closure Idioms

CS 314 Principles of Programming Languages. Lecture 16

Introduction. A. Bellaachia Page: 1

Chapter 9. Subprograms

Box-and-arrow Diagrams

Continuations provide a novel way to suspend and reexecute

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Turtles All The Way Down

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Programming Languages, Summary CSC419; Odelia Schwartz

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

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

Functional abstraction

Chapter 15 Functional Programming Languages

Scheme as implemented by Racket

CS558 Programming Languages

FP Foundations, Scheme

G Programming Languages - Fall 2012

Topic IV. Block-structured procedural languages Algol and Pascal. References:

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Course Overview. Arrays. Static Arrays. Static Arrays. Static Arrays with different lower bound. Static Arrays. PART I: overview material

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Chapter 9. Subprograms

Run-time Environments -Part 1

CSE 307: Principles of Programming Languages

Concepts Introduced in Chapter 7

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

CSCC24 Functional Programming Scheme Part 2

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

UMBC CMSC 331 Final Exam

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

CS 275 Name Final Exam Solutions December 16, 2016

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

CS558 Programming Languages

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Functional Programming. Big Picture. Design of Programming Languages

G Programming Languages - Fall 2012

Informatica 3 Syntax and Semantics

Programming Methodology

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

Programmiersprachen (Programming Languages)

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CS558 Programming Languages

Special Topics: Programming Languages

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

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Control Flow February 9, Lecture 7

Programming Languages: Lecture 11

CS201 - Introduction to Programming Glossary By

COMP 524 Spring 2018 Midterm Thursday, March 1

Transcription:

Equivalent Notations Higher-Order Functions cs480 (Prasad L156HOF 1 (define (f x y ( body = (define f (lambda (x y ( body cs480 (Prasad L156HOF 2 Function Values (define tag (lambda (t l (cons t l (tag int (1 -> (int 1 What characterizes a function? Expression in the body of the definition. The sequence of formal parameters. cs480 (Prasad L156HOF 3 Anonymous Functions ( (lambda (t l (cons t l int (1 Name of the function is irrelevant. Simplification: (define tag cons Assigning function values is similar to assigning primitive values. (first-class values. cs480 (Prasad L156HOF 4 1

Higher-order Functions In Scheme, function values can be a passed to other functions. b returned from functions. c stored in data structures. FORTRAN, Ada, etc prohibit a, b, c. Pascal allows only a. LISP/C++/Java support approximations in a round about way. LISP : Meta-programming, C++: Function pointers. Java : via Objects (Closures coming in JDK 7 Scala, Python and C# (delegates are multi-paradigm languages with support for higher-order functions. ML/Scheme are functional languages that treat function values as first-class citizens. Implementing subprograms using a stack breaks down here. Heap and garbage collection required. Applications Abstracting commonly occurring patterns of control. (factoring Defining generic functions. Instantiating generics. (ORTHOGONALITY Eventually contribute to readability, reliability, and reuse. Library: collection of higher-order functions. cs480 (Prasad L156HOF 5 cs480 (Prasad L156HOF 6 (1 2 n -> (1 4 n^2 (define (sql L (if (null? L ( (cons (* (car L (car L (sql (cdr L Factoring Commonality (a b c -> ((a (b (c (define (brac L (if (null? L ( (cons (list (car L (brac (cdr L cs480 (Prasad L156HOF 7 The map function (define (map fn lis (if (null? lis ( (cons (fn (car lis (map fn (cdr lis (map (lambda(x (* x x '(1 2 3 (map (lambda(x (list x '(a b c Built-in map: (map + '(1 2 3 '(4 5 6 = (5 7 9 cs480 (Prasad L156HOF 8 2

foldr (reduce and foldl (accumulate (Racket : > Intermediate Level (foldr + 100 (list 2 4 8 = 2 + 4 + 8 + 100 = 114 (foldr cons '(c '(a b = (a b c (foldl * 100 (list 2 4 8 = 8 * 4 * 2 * 100 = 6400 (foldl cons ' (c '(a b = (b a c cs480 (Prasad L156HOF 9 (define (imax x y (if (< x y y x (define (cmax x y (if (char-<? x y y x Parameterize wrt comparison Templates/Generics Generalization (define (max lt? x y (if (lt? x y y x Specialization (max string-<? a ab Passing function values cs480 (Prasad L156HOF 10 Instantiation (define (maxg lt? (lambda (x y (max lt? x y ( (maxg > 4 5 (* customization at run-time * Function generating function. Closure construction. Higher-order functions in a library may be tailored this way for different use. cs480 (Prasad L156HOF 11 Recursion vs Higher-Order Functions (define-struct cost ($ ; generates code for make-cost & cost-$ (define costlist (list (make-cost 5 (make-cost 10 (make-cost 25 (check-expect (totalcost costlist 40 (check-expect (totalcost costlist (totalcosthf costlist cs480 (Prasad L156HOF 12 3

Simple Recursion vs Map-Reduce version (define (totalcost cl (if (null? cl 0 [+ (cost-$ (car cl (totalcost (cdr cl] (define (totalcosthf cl (foldr + 0 (map cost-$ cl Both foldl and foldr admissible. Requires DrRacket : > HtDP Intermediate Level Scoping Rules governing the association of use of a variable with the corresponding declaration. proc p (x: int ; proc q; var y: int; begin x := y end; begin q end; Imperative Language : Local / non-local variables. Functional Language : Bound / free variables. cs480 (Prasad L156HOF 13 cs480 (Prasad L156HOF 14 Scoping rules enable determination of declaration corresponding to a non-local / free variable. Alternatives Fixed at function definition time Static / lexical scoping Scheme, Ada, C++, Java, C#, etc Fixed at function call time Dynamic scoping Franz LISP, APL, etc. Pascal Example proc p; var z:int; proc q; begin z := 5 end; proc r; var z:int; begin q end; proc s; var z:int; begin q end; begin end; cs480 (Prasad L156HOF 15 cs480 (Prasad L156HOF 16 4

Static r: s: Calls to q in r and s update the variable z declared in p. Scoping : z?? Dynamic r: s: Calls to q in r and s update the variables z and z declared in r and s respectively. Scoping : functional style (define y 5 ( (lambda (x (+ x y 3 Point of definition = Point of call. (define (f x (+ x y (define (g y (f y (g 16 Naming context of definition =/= Naming context of call. cs480 (Prasad L156HOF 17 cs480 (Prasad L156HOF 18 Static Scoping (g y y <- 16 (f y y <- 16 (+ x y x <- 16 y <- 5 21 Scoping Rules Dynamic Scoping (g y y <- 16 (f y y <- 16 (+ x y x <- 16 y <- 16 y <- 5 32 (define (addn n (lambda (x (+ x n (addn 5 = Closure (lambda (x (+x n n <- 5 Closure = Function Definition + Creation-time environment ( to enforce lexical scoping for free variables cs480 (Prasad L156HOF 19 cs480 (Prasad L156HOF 20 5

Application Instantiating generic functions. Object-oriented Programming Using (1 let-construct (to introduce local variables and (2 assignments, objects and classes can be simulated. Streams Creation of infinite data structures. Lambda Expressions (define (id x x (define id (lambda (x x (lambda (x x ( (lambda (x x 5 (lambda ( 5 cs480 (Prasad L156HOF 21 cs480 (Prasad L156HOF 22 Lambda Expressions (lambda ( 1 ( (lambda ( 1 ( (lambda (x x (lambda (y y ( (lambda (x (x x (lambda (y y cs480 (Prasad L156HOF 23 6