tailieunhanh - Lecture Programming languages (2/e): Chapter 14c - Tucker, Noonan

Chapter 14 - Functional programming. The main contents of chapter 14c include all of the following: Functions and the lambda calculus; haskell: Introduction, expressions, lists and list comprehensions, elementary types and values, control flow, defining functions, tuples. | 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 Contents Functions and the Lambda Calculus Scheme 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 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. Functions and the Lambda Calculus The function Square has R (the reals) as domain and range. Square : R R Square(n) = n2 A function is total if it is defined for all values of its domain. Otherwise, it is partial. ., Square is total. A lambda expression is a particular way to define a function: LambdaExpression variable | ( M N) | ( variable . M ) M LambdaExpression N LambdaExpression ., ( x . x2 ) represents the Square function. 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. ., (( x . x2)5) = 52 Function Evaluation In pure lambda calculus, expressions like (( x . x2)5) = 52 are uninterpreted. In a functional language, (( x . x2)5) is interpreted normally (25). Lazy evaluation = delaying argument evaluation in a function call until the argument is needed. Advantage: | 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 Contents Functions and the Lambda Calculus Scheme 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 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. Functions and the Lambda Calculus The function Square has R