Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
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 14.1 Functions and the Lambda Calculus 14.2 Scheme 14.3 Haskell 14.3.1 Introduction 14.3.2 Expressions 14.3.3 Lists and List Comprehensions 14.3.4 Elementary Types and Values 14.3.5 Control Flow 14.3.6 Defining Functions 14.3.7 Tuples 14.3.8 Example: Semantics of Clite 14.3.9 Example: Symbolic Differentiation 14.3.10 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. 14.1 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. 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 . 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. E.g., (( 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 14.1 Functions and the Lambda Calculus 14.2 Scheme 14.3 Haskell 14.3.1 Introduction 14.3.2 Expressions 14.3.3 Lists and List Comprehensions 14.3.4 Elementary Types and Values 14.3.5 Control Flow 14.3.6 Defining Functions 14.3.7 Tuples 14.3.8 Example: Semantics of Clite 14.3.9 Example: Symbolic Differentiation 14.3.10 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. 14.1 Functions and the Lambda Calculus The function Square has R