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

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Advertisements

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
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.
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.
Functional Programming Languages
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
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.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
ISBN Chapter 15 Functional Programming Languages.
TES3111 October 2001 Artificial Intelligence LISP.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,
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.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
ISBN Chapter 15 Functional Programming Languages.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
ISBN Chapter 15 Functional Programming Languages.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
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.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
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. 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.
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
ISBN Chapter 15 Functional Programming Languages.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a.
Functional Programming
Programming Languages 2nd edition Tucker and Noonan
Functional Programming
Functional Programming Languages
Functional Programming Languages
Chapter 15 :Functional Programming Languages
Functional Programming
History of Computing – Lisp
A lightening tour in 45 minutes
Haskell.
Functional Programming Languages
Programming Languages 2nd edition Tucker and Noonan
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Programming Languages 2nd edition Tucker and Noonan
Functional Programming Languages
15.2 Mathematical Functions
Chapter 15 Functional Programming 6/1/2019.
Functional Programming Languages
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 14.1 Functions and the Lambda Calculus 14.2 Scheme 14.3 Haskell Introduction Expressions Lists and List Comprehensions Elementary Types and Values Control Flow Defining Functions Tuples Example: Semantics of Clite Example: Symbolic Differentiation Example: Eight Queens

Copyright © 2006 The McGraw-Hill Companies, Inc. Overview of Functional Languages They emerged in the 1960s 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 functions result depends only upon the values of its parameters.

Copyright © 2006 The McGraw-Hill Companies, Inc 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.

Copyright © 2006 The McGraw-Hill Companies, Inc. 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

Copyright © 2006 The McGraw-Hill Companies, Inc. 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

Copyright © 2006 The McGraw-Hill Companies, Inc. 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]

Copyright © 2006 The McGraw-Hill Companies, Inc Haskell A more modern functional language Many similarities with Lisp and Scheme Key distinctions: Lazy Evaluation An Extensive Type System Cleaner syntax Notation closer to mathematics Infinite lists

Copyright © 2006 The McGraw-Hill Companies, Inc Introduction Minimal syntax for writing functions. E.g., -- two equivalent definitions of factorial fact1 n = if n==0 then 1 else n*fact2(n-1) fact2 n | n==0= 1 | otherwise = n*fact3(n-1) Note: Haskell comments begin with -- Infinite precision integers : > fact2 30 >

Copyright © 2006 The McGraw-Hill Companies, Inc Expressions Infix notation. E.g., 5*(4+6)-2-- evaluates to 48 5*4^2-2-- evaluates to 78 … or prefix notation. E.g., (-) ((*) 5 ((+) 4 6)) 2 Many operators: ! !! // ^ ** * / `div` `mod` `rem` `quot` + - : /= >= `elem` && ||

Copyright © 2006 The McGraw-Hill Companies, Inc Lists and List Comprehensions A list is a series of expressions separated by commas and enclosed in brackets. The empty list is written []. evens = [0, 2, 4, 6, 8] declares a list of even numbers. evens = [0, 2.. 8] is equivalent. A list comprehension can be defined using a generator: moreevens = [2*x | x <- [0..10]] The condition that follows the vertical bar says, all integers x from 0 to 10. The symbol <- suggests set membership ( ).

Copyright © 2006 The McGraw-Hill Companies, Inc. Infinite Lists Generators may include additional conditions, as in: factors n = [f | f <- [1..n], n `mod` f == 0] This means all integers from 1 to n that divide f evenly. List comprehensions can also be infinite. E.g.: mostevens = [2*x | x <- [0,1..]] mostevens = [0,2..]

Copyright © 2006 The McGraw-Hill Companies, Inc. List Transforming Functions The operator : concatenates a new element onto the head of a list. E.g., 4:[6, 8] gives the list [4, 6, 8]. Suppose we define evens = [0, 2, 4, 6, 8]. Then: head evens -- gives 0 tail evens -- gives [2,4,6,8] head (tail evens)-- gives 2 tail (tail evens)-- gives [4,6,8] tail [6,8] -- gives [8] tail [8]-- gives []

Copyright © 2006 The McGraw-Hill Companies, Inc. List Transforming Functions The operator ++ concatenates two lists. E.g., [2, 4]++[6, 8] gives the list [2, 4, 6, 8]. Here are some more functions on lists: null [] -- gives True null evens -- gives False [1,2]==[1,2] -- gives True [1,2]==[2,1]-- gives False 5==[5] -- gives an error (mismatched args) type evens -- gives [Int] (a list of integers)

Copyright © 2006 The McGraw-Hill Companies, Inc Elementary Types and Values Numbers integerstypes Int (finite; like int in C, Java) and Integer (infinitely many) floatstype Float Nmerical Functions abs, acos, atan, ceiling, floor, cos, sin log,logBase, pi, sqrt Booleans type Bool ; values True and False Characterstype Char ; e.g., `a`, `?` Stringstype String = [Char] ; e.g., hello

Copyright © 2006 The McGraw-Hill Companies, Inc Control Flow Conditional if x>=y && x>=z then x else if y>=x && y>=z then y else z Guarded command (used widely in defining functions) | x>=y && x>=z = x | y>=x && y>=z = y | otherwise = z

Copyright © 2006 The McGraw-Hill Companies, Inc Defining Functions A Haskell Function is defined by writing: its prototype (name, domain, and range) on the first line, and its parameters and body (meaning) on the remaining lines. max3 :: Int -> Int -> Int -> Int max3 x y z | x>=y && x>=z = x | y>=x && y>=z = y | otherwise = z Note: if the prototype is omitted, Haskell interpreter will infer it.

Copyright © 2006 The McGraw-Hill Companies, Inc. Functions are polymorphic Omitting the prototype gives the function its broadest possible meaning. E.g., max3 x y z | x>=y && x>=z = x | y>=x && y>=z = y | otherwise = z is now well-defined for any argument types: > max > max3 alpha beta gamma gamma

Copyright © 2006 The McGraw-Hill Companies, Inc Tuples A tuple is a collection of values of different types. Its values are surrounded by parens and separated by commas. E.g., (Bob, ) is a tuple. Tuple types can be defined by the types of their values. E.g., type Entry = (Person, Number) type Person = String type Number = String And lists of tuples be defined as well: type Phonebook = [(Person, Number)]

Copyright © 2006 The McGraw-Hill Companies, Inc. Functions on Tuples Standard functions on tuples (first and second members): fst (Bob, ) returns Bob snd (Bob, ) returns We can also define new functions like find to search a list of tuples: find :: Phonebook -> Person -> Number find pb p = [n | (person, n) <- pb, person == p] For instance, if: pb = [(Bob, ), (Allen, ), (Bob, )] then the call find pb Bob returns all of Bobs phone numbers: [ , ]

Copyright © 2006 The McGraw-Hill Companies, Inc. Functions as arguments Here is a function that applies another function to every member of a list, returning another list. maphead :: (a -> b) -> [a] -> [b] maphead fun alist = [ fun x | x <- alist ] E.g., if square x = x*x then maphead square [2,3,5,7,9] returns [4,9,25,49,81] F