CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Advertisements

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
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.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
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.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better 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.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming Chapter 14. History of Functional Languages Lisp is the second oldest language Motivated by a need to do symbolic, rather than.
Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
Functional Programming Chapter 14. History of Functional Languages Lisp is the second oldest language Motivated by a need to do symbolic, rather than.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
The Evolution of Programming Languages Day 2 Lecturer: Xiao Jia The Evolution of PLs1.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Chapter Fifteen: Functional Programming Languages Lesson 12.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Dr. Philip Cannata 1 Simple Lisp John McCarthy Alonzo Church David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan.
Dr. Philip Cannata 1 Programming Languages Chapter 14 – Functional Programming – Lisp.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming Part 1. Organization of Programming Languages-Cheng Big Picture u What we’ve learned so far: Imperative Programming Languages 
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Functional Programming
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming Languages
CS 550 Programming Languages Jeremy Johnson
Functional Programming
History of Computing – Lisp
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Functional Programming Languages
Programming Languages 2nd edition Tucker and Noonan
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
CS 36 – Chapter 11 Functional programming Features Practice
CSE S. Tanimoto Lambda Calculus
15.2 Mathematical Functions
Presentation transcript:

CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2 Scheme Expressions Expression Evaluation Lists Elementary Values Control Flow Defining Functions Let Expressions Example: Semantics of Clite Example: Symbolic Differentiation Example: Eight Queens

CSC321: Programming Languages14-2 Overview of Functional Languages They emerged in the 1960’s with Lisp Functional programming mirrors mathematical functions: domain = input, range = output Variables are mathematical symbols: not associated with memory locations. Pure functional programming is state-free: no assignment Referential transparency: a function’s result depends only upon the values of its parameters.

CSC321: Programming Languages14-3 Functions and the Lambda Calculus The function Square has R (the reals) as domain and range. –Square : R  R –Square(n) = n 2 A function is total if it is defined for all values of its domain. Otherwise, it is partial. E.g., Square is total. A lambda expression is a particular way to define a function: LambdaExpression  variable | ( M N) | ( variable. M ) M  LambdaExpression N  LambdaExpression E.g., ( x. x 2 ) represents the Square function.

CSC321: Programming Languages14-4 Properties of Lambda Expressions In ( x. M), x is bound. Other variables in M are free. A substitution of N for all occurrences of a variable x in M is written M[x  N]. Examples: A beta reduction (( x. M)N) of the lambda expression ( x. M) is a substitution of all bound occurrences of x in M by N. E.g., (( x. x 2 )5) = 5 2

CSC321: Programming Languages14-5 Function Evaluation In pure lambda calculus, expressions like (( x. x 2 )5) = 5 2 are uninterpreted. In a functional language, (( x. x 2 )5) is interpreted normally (25). Lazy evaluation = delaying argument evaluation in a function call until the argument is needed. –Advantage: flexibility Eager evaluation = evaluating arguments at the beginning of the call. –Advantage: efficiency

CSC321: Programming Languages14-6 Status of Functions In imperative and OO programming, functions have different (lower) status than variables. In functional programming, functions have same status as variables; they are first-class entities. –They can be passed as arguments in a call. –They can transform other functions. A function that operates on other functions is called a functional form. E.g., we can define g(f, [x1, x2, … ]) = [f(x1), f(x2), …], so that g(Square, [2, 3, 5]) = [4, 9, 25]

CSC321: Programming Languages14-7 Scheme A derivative of Lisp Our subset: –omits assignments –simulates looping via recursion –simulates blocks via functional composition Scheme is Turing complete, but Scheme programs have a different flavor

CSC321: Programming Languages14-8 Expressions Cambridge prefix notation for all Scheme expressions: (f x1 x2 … xn) E.g., –(+ 2 2); evaluates to 4 –(+ (* 5 4) (- 6 2)); means 5*4 + (6-2) –(define (Square x) (* x x)); defines a function –(define f 120); defines a global Note: Scheme comments begin with ;

CSC321: Programming Languages14-9 Expression Evaluation Three steps: 1.Replace names of symbols by their current bindings. 2.Evaluate lists as function calls in Cambridge prefix. 3.Constants evaluate to themselves. E.g., x; evaluates to 5 (+ (* x 4) (- 6 2)); evaluates to 24 5; evaluates to 5 ‘ red; evaluates to ‘ red

CSC321: Programming Languages14-10 Lists A list is a series of expressions enclosed in parentheses. –Lists represent both functions and data. –The empty list is written (). –E.g., ( ) is a list of even numbers. Here’s how it’s stored:

CSC321: Programming Languages14-11 List Transforming Functions Suppose we define the list evens to be ( ). I.e., we write (define evens ‘( )). Then: (car evens) ; gives 0 (cdr evens) ; gives ( ) (cons 1 (cdr evens)); gives ( ) (null? ‘()); gives #t, or true (equal? 5 ‘(5)) ; gives #f, or false (append ‘(1 3 5) evens); gives ( ) (list ‘(1 3 5) evens); gives ((1 3 5) ( )) Note: the last two lists are different!

CSC321: Programming Languages14-12 Elementary Values Numbers integers floats rationals Symbols Characters Functions Strings (list? evens) (symbol? ‘evens)

CSC321: Programming Languages14-13 Control Flow Conditional (if (< x 0) (- 0 x)); if-then (if (< x y) x y); if-then-else Case selection (case month ((sep apr jun nov) 30) ((feb)28) (else31) )

CSC321: Programming Languages14-14 Defining Functions (define ( name arguments ) function-body ) Examples: (define (min x y) (if (< x y) x y)) (define (abs x) (if (< x 0) (- 0 x) x)) (define (factorial n) (if (< n 1) 1 (* n (factorial (- n 1))) )) Note: be careful to match all parentheses.

CSC321: Programming Languages14-15 The subst Function (define (subst y x alist) (if (null? alist) ‘()) (if (equal? x (car alist)) (cons y (subst y x (cdr alist))) (cons (car alist) (subst y x (cdr alist))) ))) E.g., (subst ‘x 2 ‘(1 (2 3) 2)) returns (1 (2 3) x)

CSC321: Programming Languages14-16 Let Expressions Allows simplification of function definitions by defining intermediate expressions. E.g., (define (subst y x alist) (if (null? alist) ‘() (let ((head (car alist)) (tail (cdr alist))) (if (equal? x head) (cons y (subst y x tail)) (cons head (subst y x tail)) )))

CSC321: Programming Languages14-17 Functions as arguments (define (mapcar fun alist) (if (null? alist) ‘() (cons (fun (car alist)) (mapcar fun (cdr alist))) )) E.g., if (define (square x) (* x x)) then (mapcar square ‘( )) returns ( ) F

CSC321: Programming Languages14-18 Example: Symbolic Differentiation Symbolic Differentiation Rules

CSC321: Programming Languages14-19 Scheme Encoding 1.Uses Cambridge Prefix notation E.g., 2x + 1 is written as (+ (* 2 x) 1) 2.Function diff incorporates these rules. E.g., (diff ‘x ‘(+ (* 2 x) 1)) should give an answer. 3.However, no simplification is performed. E.g. the answer for (diff ‘x ‘(+ (* 2 x) 1)) is (+ (+ (* 2 1) (* x 0)) 0) which is equivalent to the simplified answer, 2.

CSC321: Programming Languages14-20 Scheme Program (define (diff x expr) (if (not (list? Expr)) (if (equal? x expr) 1 0) (let ((u (cadr expr)) (v (caddr expr))) (case (car expr) ((+) (list ‘+ (diff x u) (diff x v))) ((-) (list ‘- (diff x u) (diff x v))) ((*) (list ‘+ (list ‘* u (diff x v)) (list ‘* v (diff x u)))) ((/) (list ‘div (list ‘- (list ‘* v (diff x u)) (list ‘* u (diff x v))) (list ‘* u v))) ))))

CSC321: Programming Languages14-21 Trace of the Example (diff ‘x ‘(+ ‘(* 2 x) 1)) = (list ‘+ (diff ‘x ‘(*2 x)) (diff ‘x 1)) = (list ‘+ (list ‘+ (list ‘* 2 (diff ‘x ‘x)) (list ‘* x (diff ‘x 2))) (diff ‘x 1)) = (list ‘+ (list ‘+ (list ‘* 2 1) (list ‘* x (diff ‘x 2))) (diff ‘x 1)) = (list ‘+ (list ‘+ ‘(* 2 1) (list ‘* x (diff ‘x 2))) (diff ‘x 1)) = (list ‘+ (list ‘+ ‘(* 2 1) (list ‘* x (diff ‘x 2))) (diff ‘x 1)) = (list ‘+ (list ‘+ ‘(* 2 1) (list ‘* x 0)) 0) = ‘(+ (+ (* 2 1) (* x 0)) 0)

CSC321: Programming Languages14-22 Example: Eight Queens A backtracking algorithm for which each trial move ’ s: 1.Row must not be occupied, 2.Row and column ’ s SW diagonal must not be occupied, and 3.Row and column ’ s SE diagonal must not be occupied. If a trial move fails any of these tests, the program backtracks and tries another. The process continues until each row has a queen (or until all moves have been tried).

CSC321: Programming Languages14-23 Checking for a Valid Move (define (valid move soln) (let ((col (length (cons move soln)))) (and (not (member move soln)) (not (member (seDiag move col) (selist soln))) (not (member (swDiag move col) (swlist soln))) ))) Note: the and encodes the three rules listed on the previous slide.

CSC321: Programming Languages14-24 Representing the Developing Solution Positions of the queens kept in a list soln whose nth entry gives the row position of the queen in column n, in reverse order. E.g., soln = (5 3 1) represents queens in (row, column) positions (1,1), (3,2), and (5,3); i.e., see previous slide. End of the game occurs when soln has N (= 8) entries: (define (done soln) (>= (length soln) N)) Continuing the game tests hasmore and generates nextmove: (define (hasmore move) (<= move N)) (define (nextmove move) (+ move 1)

CSC321: Programming Languages14-25 Generating Trial Moves (define (trywh move soln) (if (and (hasmore move) (not (car soln))) (let ((atry (tryone move (cdr soln)))) (if (car atry) atry (trywh (nextmove move) soln)) ) soln )) The try function sets up the first move: (define (try soln) (trywh 1 (cons #f soln)))

CSC321: Programming Languages14-26 Trying One Move (define (tryone move soln) (let ((xsoln (cons move soln))) (if (valid move soln) (if (done xsoln) (cons #t xsoln) (try xsoln)) (cons #f soln)) )) Note: the #t or #f reveals whether the solution is complete.

CSC321: Programming Languages14-27 SW and SE Diagonals (define (swdiag row col) (+ row col)) (define (sediag row col) (- row col)) (define (swlist alist) (if (null? Alist) ‘() (cons (swDiag (car alist) (length alist)) (swlist (cdr alist))))) (define (selist alist) (if (null? Alist) ‘() (cons (seDiag (car alist) (length alist)) (selist (cdr alist)))))