CSC321: Programming Languages 14-1 Programming Languages Programming Languages Tucker and Noonan Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2 Scheme 14.2.1 Expressions 14.2.2 Expression Evaluation 14.2.3 Lists 14.2.4 Elementary Values 14.2.5 Control Flow 14.2.6 Defining Functions 14.2.7 Let Expressions 14.2.8 Example: Semantics of Clite 14.2.9 Example: Symbolic Differentiation 14.2.10 Example: Eight Queens
27
Embed
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CSC321: Programming Languages 14-1
Programming LanguagesProgramming LanguagesTucker and NoonanTucker and Noonan
Chapter 14: Functional Programming
14.1 Functions and the Lambda Calculus14.2 Scheme
14.2.1 Expressions14.2.2 Expression Evaluation14.2.3 Lists14.2.4 Elementary Values14.2.5 Control Flow14.2.6 Defining Functions14.2.7 Let Expressions14.2.8 Example: Semantics of Clite14.2.9 Example: Symbolic Differentiation14.2.10 Example: Eight Queens
CSC321: Programming Languages 14-2
Overview of Functional LanguagesOverview 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 Languages 14-3
Functions and the Lambda CalculusFunctions 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.
CSC321: Programming Languages 14-4
Properties of Lambda ExpressionsProperties 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
CSC321: Programming Languages 14-5
Function EvaluationFunction 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: flexibility
• Eager evaluation = evaluating arguments at the beginning of the call.– Advantage: efficiency
CSC321: Programming Languages 14-6
Status of FunctionsStatus 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 Languages 14-7
SchemeScheme
• 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 Languages 14-8
ExpressionsExpressions
• 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 Languages 14-9
Expression EvaluationExpression 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 Languages 14-10
ListsLists• A list is a series of expressions enclosed in
parentheses.– Lists represent both functions and data.– The empty list is written (). – E.g., (0 2 4 6 8) is a list of even numbers. Here’s how
it’s stored:
CSC321: Programming Languages 14-11
List Transforming FunctionsList Transforming Functions
• Suppose we define the list evens to be (0 2 4 6 8). I.e., we write (define evens ‘(0 2 4 6 8)). Then:
(car evens) ; gives 0
(cdr evens) ; gives (2 4 6 8)
(cons 1 (cdr evens)) ; gives (1 2 4 6 8)
(null? ‘()) ; gives #t, or true
(equal? 5 ‘(5)) ; gives #f, or false
(append ‘(1 3 5) evens) ; gives (1 3 5 0 2 4 6 8)
(list ‘(1 3 5) evens) ; gives ((1 3 5) (0 2 4 6 8))• Note: the last two lists are different!
• 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 Languages 14-23
Checking for a Valid MoveChecking for a Valid Move
Note: the and encodes the three rules listed on the previous slide.
CSC321: Programming Languages 14-24
Representing the Developing SolutionRepresenting 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))