Download presentation
Presentation is loading. Please wait.
1
Functional Programming Languages
Chapter 15 Functional Programming Languages
2
Chapter 15 Topics Introduction Mathematical Functions
Fundamentals of Functional Programming Languages The First Functional Programming Language: LISP Introduction to Scheme COMMON LISP ML & Haskell – not covered Closures – Added Topic Applications of Functional Languages Comparison of Functional and Imperative Languages Copyright © 2006 Addison-Wesley. All rights reserved.
3
Why study functional languages?
Without understanding functional programming, you can’t invent MapReduce, the algorithm that makes Google so massively scalable. —Joel Spolsky, The Perils of Java Schools Copyright © 2006 Addison-Wesley. All rights reserved.
4
Introduction The design of the imperative languages is based directly on the von Neumann architecture Efficiency is the primary concern, rather than the suitability of the language for software development The design of the functional languages is based on mathematical functions A solid theoretical basis that is also closer to the user, but relatively unconcerned with the architecture of the machines on which programs will run Copyright © 2006 Addison-Wesley. All rights reserved.
5
Backus – 1978 Turing Award lecture
Made a case for pure functional languages more readable, more reliable, more likely to be correct meaning of expressions are independent of their context (no side effects) Proposed pure functional language FP Copyright © 2006 Addison-Wesley. All rights reserved.
6
Fundamentals of Functional Programming Languages
The objective of the design of a FPL is to mimic mathematical functions to the greatest extent possible The basic process of computation is fundamentally different in a FPL than in an imperative language In an imperative language, operations are done and the results are stored in variables for later use Management of variables is a constant concern and source of complexity for imperative programming In an FPL, variables are not necessary*, as is the case in mathematics * Scheme not pure – we will use some variables, but minimal compared to imperative Copyright © 2006 Addison-Wesley. All rights reserved.
7
Referential Transparency
In an FPL, the evaluation of a function always produces the same result given the same parameters Means the expression can be replaced with its result without changing the meaning of the program (true because no side effects; must be determinate) Semantics of purely functional languages are therefore simpler than imperative languages Copyright © 2006 Addison-Wesley. All rights reserved.
8
Mathematical Functions
A mathematical function is a mapping of members of one set, called the domain set, to another set, called the range set Mapping is described by an expression or in some cases a table Evaluation order is controlled by recursion and conditional expressions rather than by sequencing and iteration A math function defines a value rather than a sequence of operations on values in memory. No variables, no side effects. Copyright © 2006 Addison-Wesley. All rights reserved.
9
Lambda Expression A lambda expression specifies the parameter(s) and the mapping of a function in the following form (x) x * x * x for the function cube (x) = x * x * x Lambda expressions describe nameless functions Copyright © 2006 Addison-Wesley. All rights reserved.
10
Lambda Expressions Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expression e.g., ((x) x * x * x)(2) which evaluates to 8 Lambda expression may have more than one parameter Copyright © 2006 Addison-Wesley. All rights reserved.
11
Functional Forms A higher-order function, or functional form, is one that: takes functions as parameters or yields a function as its result, or both Example outside programming: derivative in calculus Great for AI! Copyright © 2006 Addison-Wesley. All rights reserved.
12
Function Composition A functional form that takes two functions as parameters and yields a function whose value is the first actual parameter function applied to the application of the second Form: h f ° g which means h (x) f ( g ( x)) Example: f (x) x + 2 and g (x) 3 * x, h f ° g yields (3 * x)+ 2 Copyright © 2006 Addison-Wesley. All rights reserved.
13
Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained by applying the given function to each element of a list of parameters Form: For h (x) x * x ( h, (2, 3, 4)) yields (4, 9, 16) Copyright © 2006 Addison-Wesley. All rights reserved.
14
LISP Data Types and Structures
Originally, LISP was a typeless language Data object types: originally only atoms and lists Atoms are either symbols (e.g., ‘count) or numbers (e.g., 1, 2.5) List form: parenthesized collections of sublists and/or atoms ‘(A B (C D) E) ‘(1 2 5) Notice single quote, so list is not evaluated Notice no commas: not (1, 2, 5) Copyright © 2006 Addison-Wesley. All rights reserved.
15
LISP Lists LISP lists are stored internally as single-linked lists
(A B C D) A B C D (A (B C) D (E (F G)) A D B C E F G Copyright © 2006 Addison-Wesley. All rights reserved.
16
LISP Interpretation Lambda notation is used to specify functions and function definitions. Function applications and data have the same form. If the list (A B C) is interpreted as data it is a simple list of three atoms, A, B, and C If it is interpreted as a function application, it means that the function named A is applied to the two parameters, B and C Functions are first-class entities They can be the values of expressions and elements of lists They can be assigned to variables and passed as parameters Cool… very useful for AI! Copyright © 2006 Addison-Wesley. All rights reserved.
17
Intro to Scheme We’ll use DrScheme (modern LISP)
Language Pack: Swindle Function definitions go here Interpreter – try it! Copyright © 2006 Addison-Wesley. All rights reserved.
18
Scheme Interpreter Interpreter is read-evaluate-write infinite loop
Expressions evaluated by EVAL function Parameters are evaluated, in no particular order (literals EVAL to themselves) The values of the parameters are substituted into the function body The function body is evaluated The value of the last expression in the body is the value of the function (cube (+ 1 2)) Parameter (+ 1 2) is evaluated, passed to cube. Result (9) is returned & displayed Copyright © 2006 Addison-Wesley. All rights reserved.
19
Primitive Functions REMEMBER THE ( ) around function calls!
Learn to read error messages, they will tell you what the evaluator was attempting when the error occurred. Copyright © 2006 Addison-Wesley. All rights reserved.
20
Function Definition: LAMBDA
A Scheme program is collection of function definitions Pure form: Lambda Expressions Form is based on notation e.g., (lambda (x) (* x x) x is called a bound variable Lambda expressions can be applied e.g., ((lambda (x) (* x x)) 7) Copyright © 2006 Addison-Wesley. All rights reserved.
21
Special Form Function: DEFINE
A Function for Constructing Functions DEFINE - two forms: To bind a symbol to an expression (a constant) e.g., (define pi ) Example use: (* pi 5 5) To bind names to lambda expressions e.g., (define (square x) (* x x)) Example use: (square 5) Names are case-sensitive, include letters, digits and special characters Copyright © 2006 Addison-Wesley. All rights reserved.
22
Defining values & functions
Must press Run (like compiler, not execution) Type definitions in upper window System loudly reminds you to press Run! Copyright © 2006 Addison-Wesley. All rights reserved.
23
Scheme Run (compile) – an Error
indicates edited here’s the syntax error Copyright © 2006 Addison-Wesley. All rights reserved.
24
Scheme Run – still an error!
not very informative Remember: we call functions (fn-name param). For example, (square 5). SO, we need a ( in front of square, with a closing ) of course! Copyright © 2006 Addison-Wesley. All rights reserved.
25
Scheme – old habits die hard…
No no, the call is (square 5) NOT (square (5))…. Copyright © 2006 Addison-Wesley. All rights reserved.
26
Scheme – we got it right! use ctrl-UpArrow to repeat a command
Copyright © 2006 Addison-Wesley. All rights reserved.
27
List Functions: CAR and CDR
CAR takes a list parameter; returns the first element of that list FIRST is equivalent to CAR CDR takes a list parameter; returns the list after removing its first element REST = CDR must test for ‘() before doing car recursion using cdr eventually ends up with ‘() common errors: 1) treat an atom like a list or 2) forget quote Copyright © 2006 Addison-Wesley. All rights reserved.
28
Creating Lists cons append list
Copyright © 2006 Addison-Wesley. All rights reserved.
29
List Functions: CONS cons has two forms:
cons (atom list) cons (list list) result is a new list that includes the first parameter as its first element and the second parameter as the remainder e.g., (cons 'A '(B C)) returns (A B C) Applying CONS to two atoms results in a dotted list (cell with two atoms, no pointer), e.g., (cons 'A 'B) returns (A . B) cons creates node Copyright © 2006 Addison-Wesley. All rights reserved. A B
30
Cons Example (cons atom list) (cons atom atom) (cons list list)
Notice (C D) is no longer a list C D A B A B C D Copyright © 2006 Addison-Wesley. All rights reserved.
31
List Functions: List list takes any number of parameters; returns a list with the parameters as elements Repeated for comparison Copyright © 2006 Addison-Wesley. All rights reserved.
32
Example Scheme Function: append
append has one form: append(list list) result is a new list containing the elements of the first parameter followed by the elements of the second parameter Example: (append '((A B) C) '(D (E F))) returns ((A B) C D (E F)) Copyright © 2006 Addison-Wesley. All rights reserved.
33
Append Example Repeated for comparison
Copyright © 2006 Addison-Wesley. All rights reserved.
34
Scheme Functional Forms
Composition (cdr (cdr ‘(A B C))) returns ( C ) (car (car '((A B) B C))) returns A (cdr (car '((A B C) D))) returns (B C) (cons (car '(A B))(cdr '(A B)))returns(A B) (caar x) is equivalent to (car (car x)) (cadr x) is equivalent to (car (cdr x)) (caddar x is equivalent to (car (cdr (cdr (car x)))) Copyright © 2006 Addison-Wesley. All rights reserved.
35
Scheme Exercises Now you try it. Explore the Scheme environment. Do exercises 1-3 in the Scheme Exercises. Review the common errors. Feel free to read more lecture notes and work ahead. NOTE: In case you need to comment out some code, comments in Scheme are indicated by ; Copyright © 2006 Addison-Wesley. All rights reserved.
36
Predicate Functions: LIST? and NULL?
list? takes one parameter; it returns #T if the parameter is a list; otherwise() list? returns #T for the null list ‘() null? takes one parameter; it returns #T if the parameter is the empty list; otherwise() Note that null? returns #T if the parameter is() Copyright © 2006 Addison-Wesley. All rights reserved.
37
Control Flow: IF Selection- the special form, IF (if predicate
true_exp false_exp) Example: (if (<> count 0) (/ sum count) 0) Copyright © 2006 Addison-Wesley. All rights reserved.
38
Output Functions (DISPLAY expression) (NEWLINE) Notice statements
performed in sequence Copyright © 2006 Addison-Wesley. All rights reserved.
39
If and recursion empty list is base case compound else closes (begin
call with remainder of list recursive call Copyright © 2006 Addison-Wesley. All rights reserved.
40
Control Flow: COND Multiple Selection - the special form, COND
General form: (COND (predicate_1 expr {expr}) (predicate_2 expr {expr}) ... (predicate_n expr {expr}) (ELSE expr {expr})) Returns the value of the last expr in the first pair whose predicate evaluates to true. Based on guarded expressions (chapter 9) Predicate can be compound (using and, or) Copyright © 2006 Addison-Wesley. All rights reserved.
41
COND example Notice () around entire clause Notice () around
close cond, Could have used else here close define Copyright © 2006 Addison-Wesley. All rights reserved.
42
Recursion – two ways to build list
Copyright © 2006 Addison-Wesley. All rights reserved.
43
Numeric Predicate Functions
#t (or non-null list) is true #f or () is false =, <>, >, <, >=, <= even?, odd?, zero?, negative? Copyright © 2006 Addison-Wesley. All rights reserved.
44
List Functions: Quote QUOTE - takes one parameter; returns the parameter without evaluation QUOTE is required because the Scheme interpreter, EVAL, always evaluates parameters before applying the function. QUOTE is used to avoid parameter evaluation when it is not appropriate QUOTE can be abbreviated with the apostrophe prefix operator Copyright © 2006 Addison-Wesley. All rights reserved.
45
Example Scheme Function: member
member takes an atom and a simple list; returns a sublist containing the atom if it is in the list; #f otherwise Copyright © 2006 Addison-Wesley. All rights reserved.
46
Calling the Scheme eval function
It is possible to call eval directly eval takes a list and evaluates it. The first item in the list is typically a function. (eval '(+ 3 4)) => 7 (eval '(square 6)) => 36 Copyright © 2006 Addison-Wesley. All rights reserved.
47
Functions That Build Code
It is possible in Scheme to define a function that builds Scheme code and requests its interpretation This is possible because the interpreter is a user-available function, eval + quoted to prevent evaluation cons inserts + into list Copyright © 2006 Addison-Wesley. All rights reserved.
48
Function to applies a function
Map – applies (maps) a function (map list? ‘(a (b c) d) => (#f #t #f) (define switchView (lambda (person) (cond ((equal? person 'i) 'you) ((equal? person 'me) 'you) ((equal? person 'you) 'me) ((equal? person 'your) 'my) ((equal? person 'yours) 'mine) ((equal? person 'am) 'are) (#t person) ))) (map switchView ‘(i can help you)) => (you can help me) Copyright © 2006 Addison-Wesley. All rights reserved.
49
Apply (apply FN ARG-LIST)
Returns the result of applying FN to the list of arguments Assume have function: (define (isVerb word) (member word '(go have try eat take))) Can use with apply: (apply isVerb ‘(eat)) => ‘(eat take) (apply isVerb ‘(jump)) => #f Copyright © 2006 Addison-Wesley. All rights reserved.
50
Apply Errors Copyright © 2006 Addison-Wesley. All rights reserved.
51
Example Scheme Function: LET
General form: (let ( (name_1 expression_1) (name_2 expression_2) ... (name_n expression_n)) body ) Evaluate all expressions, then bind the values to the names; evaluate the body. Only local. Often used to factor out common subexpressions Copyright © 2006 Addison-Wesley. All rights reserved.
52
LET Example (define (quadratic_roots a b c) (let ( (root_part_over_2a
(/ (sqrt (- (* b b) (* 4 a c)))(* 2 a))) (minus_b_over_2a (/ (- 0 b) (* 2 a)))) (begin (display (+ minus_b_over_2a root_part_over_2a)) (newline) (display (- minus_b_over_2a root_part_over_2a)) ) Copyright © 2006 Addison-Wesley. All rights reserved.
53
Let* Similar to let Evaluates the exprs one by one, creating a location for each id as soon as the value is available. (let* ((x 1) (y (+ x 1))) (list y x)) => ‘(2 1) Copyright © 2006 Addison-Wesley. All rights reserved.
54
Exercises Do Exercises 4-12
Do converter exercise – to submit, individual Copyright © 2006 Addison-Wesley. All rights reserved.
55
Predicate Function: eq?
eq? takes two symbolic parameters; it returns #t if both parameters are atoms and the two are the same e.g., (eq? 'A 'A) yields #t (eq? 'A 'B) yields #f Note that if eq? is called with list parameters, the result is not reliable Also eq? does not work for numeric atoms, should use = [appears to work???] eqv? works on both symbols and numbers we’ll create function in two slides Copyright © 2006 Addison-Wesley. All rights reserved.
56
Our own list_member (for practice)
(define (list_member atm lis) (cond ((null? lis) #f) ((eq? atm (car lis)) #t) (else (list_member atm (cdr lis))) ) State recursively: an element is a member of the list if: - the element matches the first item in the list OR - the element is a member of the rest of the list How would you modify the above to search sublists? Copyright © 2006 Addison-Wesley. All rights reserved.
57
Example Scheme Function: equalsimp
equalsimp takes two simple lists as parameters; returns #T if the two simple lists are equal; () otherwise. Won’t handle list such as ‘(a (b c)) (define (equalsimp lis1 lis2) (cond ; if lis1 is null, #t is lis2 is null ((null? lis1) (null? lis2)) ; if lis2 is null, lis1 is not null, so ‘() ((null? lis2) '()) ((eq? (car lis1) (car lis2)) (equalsimp(cdr lis1)(cdr lis2))) (else '()) )) State recursively: two lists are equal if: - the first elements of each list match AND - the remaining lists (minus first elements) are equal) Copyright © 2006 Addison-Wesley. All rights reserved.
58
Example Scheme Function: equal
equal takes two general lists as parameters; returns #T if the two lists are equal; ()otherwise (define (equal lis1 lis2) (cond ; if lis1 not a list, checking items in sublist ((not (list? lis1))(eq? lis1 lis2)) ; lis1 is a list but lis2 is not ((not (list? lis2)) '()) ; rest of code is like equal ((null? lis1) (null? lis2)) ((null? lis2) '()) ((equal (car lis1) (car lis2)) (equal (cdr lis1) (cdr lis2))) (else '()) )) This one can handle ‘(a (b c)) – see trace Copyright © 2006 Addison-Wesley. All rights reserved.
59
Equal Example Copyright © 2006 Addison-Wesley. All rights reserved.
60
Equal Trace (a (b c)) (a (b c))
lis1: (a (b c)) lis2: (a (b c)) lis1: a lis2: a lis1 is not a list, checking one item, returning #t cars are equal, check cdrs lis1: ((b c)) lis2: ((b c)) lis1: (b c) lis2: (b c) lis1: b lis2: b lis1: (c) lis2: (c) lis1: c lis2: c lis1: () lis2: () lis1 is null, return null? lis2 #t Copyright © 2006 Addison-Wesley. All rights reserved.
61
Equal Trace #2 (a (b c)) (a (d c))
lis1: (a (b c)) lis2: (a (d c)) lis1: a lis2: a lis1 is not a list, checking one item, returning #t cars are equal, check cdrs lis1: ((b c)) lis2: ((d c)) lis1: (b c) lis2: (d c) lis1: b lis2: d lis1 is not a list, checking one item, returning #f #f Copyright © 2006 Addison-Wesley. All rights reserved.
62
More Scheme Errors Copyright © 2006 Addison-Wesley. All rights reserved.
63
Scheme errors continued
Extra parenthesis, this is the action: (display result) (evenSquareErr2…) so scheme doesn’t know what to do with: ((display result)(evenSquareErr2…) Don’t try to use ( .. ) to indicate a block!! Copyright © 2006 Addison-Wesley. All rights reserved.
64
Scheme errors continued…
You would expect this to be a syntax error or something! But it just “falls through” when it hits the else condition. Copyright © 2006 Addison-Wesley. All rights reserved.
65
Scheme debugging tip watch recursive calls see which conditions
matched Copyright © 2006 Addison-Wesley. All rights reserved.
66
Scheme Debugging Press Debug current step
Copyright © 2006 Addison-Wesley. All rights reserved.
67
Scheme Exercises Continue with all exercises
Copyright © 2006 Addison-Wesley. All rights reserved.
68
Closures From Wikipedia:
a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables. Can also be expressed as: a closure is a function that captures the lexical environment in which it was created. Constructs such as objects in other languages can also be modeled with closures (e.g., instance variables equivalent to bound variables) Copyright © 2006 Addison-Wesley. All rights reserved.
69
Closures, continued A closure can be used to associate a function with a set of "private" variables, which persist over several invocations of the function. The scope of the variable encompasses only the closed-over function, so it cannot be accessed from other program code. However, the variable is of indefinite extent, so a value established in one invocation remains available in the next. The concept of closures was developed in the 60’s and was first fully implemented as a language feature in the programming language Scheme. Since then, many languages have been designed to support closures. similar to static Copyright © 2006 Addison-Wesley. All rights reserved.
70
Closures and first-class values
Closures typically appear in languages in which functions are first-class values - in other words, such languages allow functions to be passed as arguments, returned from function calls, bound to variable names, etc., just like simpler types such as strings and integers ; Copyright © 2006 Addison-Wesley. All rights reserved.
71
Closure in Scheme Return a list of all books with at least
; THRESHOLD copies sold. (define (best-selling-books threshold) (let ((book-list ‘'(((Gone With the Wind) 1000) ((Freakonomics) 1500) ((Cryptonomicon) 500)))) (filter (lambda (book) (>= (book-sales book) threshold)) book-list)) passed to closure for lambda code & threshold created book-list in lexical environment Copyright © 2006 Addison-Wesley. All rights reserved.
72
Closure in another language
ECMAScript – uses function keyword instead of lambda, creates closure with Array.filter // Return a list of all books with at least // THRESHOLD copies sold. function bestSellingBooks(threshold) { return bookList.filter( function(book) return book.sales >= threshold; } ); filter is higher-order function - takes a function as a parameter Copyright © 2006 Addison-Wesley. All rights reserved.
73
Another closure in JavaScript
Nameless function, inherits environment in which it is defined. In this case environment includes xhr object and document object. xhr.onreadystatechange = function() { if (xhr.readyState == 4) { var result = xhr.responseText; var place = result.split(‘,’_; document.getElementById(“city”).value = place[0]; document.getElementById(“state”).value = place[1]; } xhr object part of lexical environment Copyright © 2006 Addison-Wesley. All rights reserved.
74
Closures in Ruby fold_coder = lambda { |default_value, binary_function| fold_with_acc = lambda { |acc, list| if list.empty? acc else fold_with_acc.call(binary_function.call(acc, list.first), list[1..-1]) end } lambda { |list| fold_with_acc.call(default_value, list) } } ; create closure adder = fold_coder.call(0, lambda { |a, b| a + b }) ; call it adder.call([1, 2, 3, 4, 5]) adder.call([2, 4, 6, 8, 10]) * From Copyright © 2006 Addison-Wesley. All rights reserved.
75
Lexical Closure – from Wiki
"A lexical closure is, first of all, an object- …with special notation... That notation specifies an anonymous piece of code that can take arguments, and is in fact an anonymous function. The anonymous function is a component of the closure object, but not the only component. The other component of the object is the lexical environment: a snapshot of all of the current bindings of all of the local variables which are visible at that point." The bindings of the variables are the values of the variables, not the addresses that the names point to. Copyright © 2006 Addison-Wesley. All rights reserved. if addresses, would be just like globals
76
More Terminology Within some scope a free variable is a variable that is not bound within that scope, i.e. that within some scope there is no declaration of that variable. The name "Closure" comes from the terminology that the function-procedure "closes-over" its environment, capturing the current bindings of its free variables. Copyright © 2006 Addison-Wesley. All rights reserved.
77
Example: Callback with Closures
;; In the library: (define (register-callback event-type handler-proc) ...) ;; In the application: (define (make-handler event-type user-data) (lambda () ... <code referencing event-type and user-data> ...)) (register-callback event-type (make-handler event-type ...)) library sees handler-proc as procedure with no arguments handler procedure has used closure to capture environment – event type and user data Copyright © 2006 Addison-Wesley. All rights reserved.
78
Closure Implementation
Closures are typically implemented with a special data structure that contains a pointer to the function code, plus a representation of the function's lexical environment (e.g., the set of available variables and their values) at the time when the closure was created. A language implementation cannot easily support full closures if its run-time memory model allocates all local variables on a linear stack. In such languages, a function's local variables are deallocated when the function returns. However, a closure requires that the free variables it references survive the enclosing function's execution. Therefore those variables must be allocated so that they persist until no longer needed. This explains why typically languages that natively support closures use garbage collection. Copyright © 2006 Addison-Wesley. All rights reserved.
79
Closure Implementation (continued)
A typical modern Scheme implementation allocates local variables that might be used by closures dynamically and stores all other local variables on the stack. Copyright © 2006 Addison-Wesley. All rights reserved.
80
Interesting Perspectives
of-closures.html Copyright © 2006 Addison-Wesley. All rights reserved.
81
Applications of Functional Languages
APL is used for throw-away programs LISP is used for artificial intelligence Knowledge representation/Expert systems Machine learning Natural language processing Modeling of speech and vision Emacs editor Scheme is used to teach introductory programming at a significant number of universities Haskell and ML in research labs Copyright © 2006 Addison-Wesley. All rights reserved.
82
Comparing Functional and Imperative Languages
Efficient execution Complex semantics Complex syntax Concurrency is programmer designed Functional Languages: Simple semantics (like math functions, may or may not feel “natural” to programmers) Simple syntax Inefficient execution (not as bad if compiled) Programs can automatically be made concurrent Copyright © 2006 Addison-Wesley. All rights reserved.
83
Summary Functional programming languages use function application, conditional expressions, recursion, and functional forms to control program execution instead of imperative features such as variables and assignments LISP began as a purely functional language and later included imperative features Scheme is a relatively simple dialect of LISP that uses static scoping exclusively COMMON LISP is a large LISP-based language ML is a static-scoped and strongly typed functional language which includes type inference, exception handling, and a variety of data structures and abstract data types Haskell is a lazy functional language supporting infinite lists and set comprehension. Purely functional languages have advantages over imperative alternatives, but their lower efficiency on existing machine architectures has prevented them from enjoying widespread use Copyright © 2006 Addison-Wesley. All rights reserved.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.