Functional Programmingcse452/Fall2005/Lectures/05... · LISP EVAL: a universal function that could...
Transcript of Functional Programmingcse452/Fall2005/Lectures/05... · LISP EVAL: a universal function that could...
Functional Programming
Organization of Programming Languages-Cheng
Big Picture What we’ve learned so far:
Imperative Programming LanguagesVariables, binding, scoping, reference environment,
etc What’s next:
Functional Programming Languages Semantics of Programming Languages Control Flow Data Types Logic Programming Subroutines, Procedures: Control Abstraction Data Abstraction and Object-Orientation
Organization of Programming Languages-Cheng
Design of Programming Languages
Design of imperative languages is based directlyon the von Neumann architecture Efficiency is the primary concern, rather than
the suitability of the language for softwaredevelopment
Other designs: Functional programming languages
Based on mathematical functions Logic programming languages
Based on formal logic (specifically, predicatecalculus)
Organization of Programming Languages-Cheng
Design View of a Program
If we ignore the details of computation (“how” ofcomputation) and focus on the result beingcomputed (“what” of computation) A program becomes a “black box” for
obtaining output from input
ProgramInput Output
Organization of Programming Languages-Cheng
Functional Programming Language
Functional programming is a style ofprogramming in which the primary method ofcomputation is the application of functions toarguments
f(x)x y
Argumentor variable
Function Output
Organization of Programming Languages-Cheng
Historical Background
1940s:
Alonzo Church and Haskell Currydeveloped the lambda calculus, a simplebut powerful mathematical theory offunctions.
6
Organization of Programming Languages-Cheng
Historical Background
1960s:
John McCarthy developed Lisp, the firstfunctional language. Some influencesfrom the lambda calculus, but stillretained variable assignments.
7
Organization of Programming Languages-Cheng
Historical Background
1978:
John Backus publishes award winningarticle on FP, a functional languagethat emphasizes higher-order functionsand calculating with programs.
8
Organization of Programming Languages-Cheng
Historical Background
Mid 1970s:
Robin Milner develops ML, the first of themodern functional languages, whichintroduced type inference and polymorphictypes.
9
Organization of Programming Languages-Cheng
Historical Background
Late 1970s - 1980s:
David Turner develops a number oflazy functional languages leading upto Miranda, a commercial product.
10
Organization of Programming Languages-Cheng
Historical Background
1988:
A committee of prominentresearchers publishes the firstdefinition of Haskell, a standard lazyfunctional language.
11
Organization of Programming Languages-Cheng
Historical Background
1999:
The committee publishes the definitionof Haskell 98, providing a long-awaitedstable version of the language.
12
Organization of Programming Languages-Cheng
Basic Idea of FunctionalProgramming
Think of programs, procedures and functions as beingrepresented by the mathematical concept of a function
A mathematical function: {elts in domain} → {elts in range} Example: sin(x)
domain set: set of all real numbers range set: set of real numbers from –1 to +1
In mathematics, variables always represent actualvalues
There is no such thing as a variable that models a memorylocation
Since there is no concept of memory location or l-value ofvariables, a statement such as (x = x + 1) makes no sense
A mathematical function defines a value, rather thanspecifying a sequence of operations on values inmemory
Organization of Programming Languages-Cheng
Imperative Programming
Summing the integers 1 to 10 in Java:
total = 0;
for (i = 1; i ≤ 10; ++i)
total = total + i;
The computation method is variable assignment
Each variable is associated with a memory location 14
Organization of Programming Languages-Cheng
Functional Programming
Summing the integers 1 to 10 in Haskell:
sum [1..10]
The computation method is function application.
15
Organization of Programming Languages-Cheng
Functional Programming
Function definition vs function application Function definition
Declaration describing how a function is to becomputed using formal parameters
Function applicationA call to a declared function using actual
parameters or arguments
In mathematics, a distinction is often not clearlymade between a variable and a parameter The term independent variable is often used
for both actual and formal parameters
Organization of Programming Languages-Cheng
Functional ProgrammingLanguage
The objective of the design of a FPL is to mimicmathematical functions to the greatest extentpossible Imperative language: operations are
completed and results are stored in variablesfor later useManagement of variables is source of complexity
for imperative programming FPL: variables are not necessary
Without variables, iterative constructs areimpossible
Repetition must be provided by recursion ratherthan iterative constructs
Organization of Programming Languages-Cheng
Example: GCD
Find the greatest common divisor (GCD)between 12 and 30 Divisors for 12: 1, 2, 3, 4, 6, 12 Divisors for 30: 1, 2, 3, 5, 6, 10, 15, 30
GCD(12, 30) is equal to 6
Algorithm: u=30, v=12 30 mod 12 = 6 u=12, v=6 12 mod 6 = 0 u=6, v=0 since v=0, GCD=u=6
Organization of Programming Languages-Cheng
Computing GCD
int gcd(int u, int v){
int temp;while (v != 0) {
temp = v;v = u mod v;u = temp;
}return u;
}
* Computing GCD usingiterative construct
int gcd(int u, int v){
if (v==0)return u;
elsereturn gcd(v, u % v);
}
* Computing GCD usingrecursion
Organization of Programming Languages-Cheng
Fundamentals of FPLs
Referential transparency: In FPL, the evaluation of a function always
produces the same result given the sameparameters
E.g., a function rand, which returns a (pseudo)random value, cannot be referentiallytransparent since it depends on the state ofthe machine (and previous calls to itself)
Most functional languages are implemented withinterpreters
Organization of Programming Languages-Cheng
Using Imperative PLs to supportFPLs
Limitations Imperative languages often have restrictions
on the types of values returned by the functionExample: in Fortran, only scalar values can be
returnedMost of them cannot return functionsThis limits the kinds of functional forms that can be
provided
Functional side-effectsOccurs when the function changes one of its
parameters or a global variable
Organization of Programming Languages-Cheng
Characteristics of Pure FPL
Pure FP languages tend to Have no side-effects Have no assignment statements Often have no variables! Be built on a small, concise framework Have a simple, uniform syntax Be implemented via interpreters rather than
compilers
Organization of Programming Languages-Cheng
Functional ProgrammingLanguages
FPL provides a set of primitive functions, a set of functional forms to construct complex
functions, a function application operation, some structure to represent data
In functional programming, functions are viewedas values themselves, which can be computedby other functions and can be parameters toother functions Functions are first-class values
Organization of Programming Languages-Cheng
Lambda Expressions
Function definitions are often written as afunction name, followed by list of parameters andmapping expression: Example: cube (x) ≡ x * x * x
A lambda expression provides a method fordefining nameless functions
λ(x) x * x * x Nameless functions are useful, e.g., functions
that are produced for intermediate applicationto a parameter list do not need a name, for itis applied only at the point of its construction
Organization of Programming Languages-Cheng
Lambda Expressions
Function application Lambda expressions are applied to
parameter(s) by placing the parameter(s) afterthe expressione.g. (λ(x) x * x * x)(3) evaluates to 27
Lambda expressions can have more than oneparameter
e.g. λ(x, y) x / y
Organization of Programming Languages-Cheng
Functional Forms A functional form, is a function that either
takes functions as parameters, yields a function as its result, or both
Also known as higher-order function Function Composition:
A functional form that takes two functions asparameters and yields a function whose result is afunction whose value is the first actual parameterfunction applied to the result of the application of thesecond
Example: h ≡ f ° g, which means h (x) ≡ f ( g ( x))If f(x) ≡ x + 2, g(x) ≡ 3 * x, then h(x) ≡ (3 * x) + 2
Organization of Programming Languages-Cheng
Functional Forms Construction: A functional form that takes a list of
functions as parameters and yields a list of the results ofapplying each of its parameter functions to a givenparameter
For f (x) ≡ x * x * x and g (x) ≡ x + 3, [f, g] (4) yields (64, 7)
Apply-to-all: A functional form that takes a single functionas a parameter and yields a list of values obtained byapplying the given function to each element of a list ofparameters
For h (x) ≡ x * x * x, α( h, (3, 2, 4)) yields (27, 8, 64)
Organization of Programming Languages-Cheng
LISP
Stands for LISt Processing Originally, LISP was a typeless language and used for
symbolic processing in AI Two data types:
atoms (consists of symbols and numbers) lists (that consist of atoms and nested lists)
LISP lists are stored internally as single-linked lists Lambda notation is used to specify nameless functions
(function_name (LAMBDA (arg_1 … arg_n)expression))
Organization of Programming Languages-Cheng
LISP
Function definitions, function applications, and data allhave the same form Example:
If the list (A B C) is interpreted as data, it is asimple list of three atoms, A, B, and C
If it is interpreted as a function application, it meansthat the function named A is applied to the twoparameters, B and C
This uniformity of data and programs gives functionallanguages their flexibility and expressive power programs can be manipulated dynamically
Organization of Programming Languages-Cheng
LISP
The first LISP interpreter appeared only as a demonstration of theuniversality of the computational capabilities of the notation
Example (early Lisp):(defun fact (n) (cond ((lessp n 2) 1)(T (times n (fact(sub1 n))))))
The original intent was to have a notation for LISP programs that wouldbe as close to Fortran’s as possible, with additions when necessary This notation was called M-notation, for meta-notation
McCarthy believed that list processing could be used to studycomputability, which at the time was usually studied using Turingmachines McCarthy thought that symbolic lists was a more natural model of
computation than Turing machines Common Lisp is the ANSI standard Lisp specification All LISP structures, including code and data, have uniform structure
Called S-expressions: nil; t; (a.b); (1.(2. (3. nil))) also as (1 2 3);
Organization of Programming Languages-Cheng
LISP
EVAL: a universal function that could evaluateany other functions
LISP 1.5: Awkward—though elegantly uniform—syntax. Dynamic scope rule Inconsistent treatment of functions as
arguments—because of dynamic scoping
Organization of Programming Languages-Cheng
Scheme
A mid-1970s dialect of LISP, designed to becleaner, more modern, and simpler version thanthe contemporary dialects of LISP
Uses only static scoping
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
Organization of Programming Languages-Cheng
Scheme
Arithmetic: +, -, *, /, ABS, SQRT(+ 5 2) yields 7
Most interpreters use prefix notations: (+ 3 5 7) means add 3, 5, and 7 (+ (* 3 5) (- 10 6)) Advantages:
Operator/function can take arbitrary number ofarguments
No ambiguity, because operator is always theleftmost element and the entire combination isdelimited by parentheses
Organization of Programming Languages-Cheng
Scheme
QUOTE takes one parameter; returns the parameter without
evaluation QUOTE is required because the Scheme interpreter,
named EVAL, always evaluates parameters tofunction applications before applying the function.
QUOTE is used to avoid parameter valuation when itis not appropriate
QUOTE can be abbreviated with the apostrophe prefixoperator
'(A B) is equivalent to (QUOTE (A B)) (list a b c) versus (list 'a 'b 'c)
Organization of Programming Languages-Cheng
Scheme LIST takes any number of parameters; returns a list with the
parameters as elements
CAR takes a list parameter; returns the first element of that list (CAR '(A B C)) yields A (CAR '((A B) C D)) yields (A B)
CDR takes a list parameter; returns the list after removing its firstelement (CDR '(A B C)) yields (B C) (CDR '((A B) C D)) yields (C D)
CONS returns a new list that includes the first parameter as its firstelement and the second parameter as remainder of its result (CONS ’(A D) '(B C)) returns ((A D) B C)
-
Organization of Programming Languages-Cheng
Scheme: Predicate Functions #T and () are true and false respectively EQ? returns #T if both parameters are atoms and are the same
(EQ? 'A 'A) yields #T (EQ? 'A '(A B)) yields () Note that if EQ? is called with list parameters, the result is not
reliable Also, EQ? does not work for numeric atoms
LIST? returns #T if the parameter is a list; otherwise ()
NULL? returns #T if the parameter is an empty list; otherwise () Note that NULL? returns #T if the parameter is ()
Numeric Predicate Functions=, <>, >, <, >=, <=, EVEN?, ODD?, ZERO?
Organization of Programming Languages-Cheng
Lambda Expressions Form is based on λ notation
(LAMBDA (L) (CAR (CDR L))) L is called a bound variable Lambda expressions can be applied
((LAMBDA(L) (CAR (CDR L))) '((A B) C D))
Organization of Programming Languages-Cheng
Constructing Functions
DEFINE - Two forms: To bind a symbol to an expression
(DEFINE pi 3.141593)(DEFINE two_pi (* 2 pi))
To bind names to lambda expressions(DEFINE (cube x) (* x x x))
then you can use: (cube x)
Organization of Programming Languages-Cheng
Evaluation Process
Evaluation process (for normal functions): Parameters are evaluated, in no particular
order 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
Organization of Programming Languages-Cheng
Control Flow
Selection- the special form, IF (IF predicate then_expelse_exp)
Examples: (IF (<> count 0) (/ sum count)0 )
(DEFINE (factorial n) ( IF ( = n 0) 1 (* n (factorial(- n 1)) )
))
Organization of Programming Languages-Cheng
Control Flow
Multiple Selection - the special form, COND
(COND (predicate_1 expr {expr}) (predicate_1 expr {expr}) ... (predicate_1 expr {expr}) (ELSE expr {expr}) )
Returns the value of the last expr in the first pair whosepredicate evaluates to true
Organization of Programming Languages-Cheng
Example Scheme Function
member takes an atom and a list; returns #T if atom is in list; () otherwise
(DEFINE (member atm lis) (COND ((NULL? lis) '()) ((EQ? atm (CAR lis)) #T) ((ELSE (member atm (CDR lis))) ))
Organization of Programming Languages-Cheng
Example Scheme Function
equalsimp returns #T if the two simple lists are equal; ()
otherwise
(DEFINE (equalsimp lis1 lis2) (COND ((NULL? lis1) (NULL? lis2)) ((NULL? lis2) '()) ((EQ? (CAR lis1) (CAR lis2)) (equalsimp (CDR lis1) (CDR
lis2))) (ELSE '()) ))
Organization of Programming Languages-Cheng
Example Scheme Function
equal returns #T if the two general lists are equal; ()
otherwise
(DEFINE (equal lis1 lis2) (COND ((NOT (LIST? lis1)) (EQ? lis1 lis2)) ((NOT (LIST? lis2)) '()) ((NULL? lis1) (NULL? lis2)) ((NULL? lis2) '()) ((equal (CAR lis1) (CAR lis2)) (equal (CDR lis1) (CDR lis2))) (ELSE '()) ))
Organization of Programming Languages-Cheng
Example Scheme Function
Append takes two lists as parameters; returns the first
parameter list with the elements of the secondparameter list appended at the end
(DEFINE (append lis1 lis2) (COND ((NULL? lis1) lis2) (ELSE (CONS (CAR lis1) (append (CDR lis1) lis2))) ))
Organization of Programming Languages-Cheng
Scheme Functional Forms
Composition The previous examples have used it
Apply to All one form in Scheme is mapcar Applies the given function to all elements of
the given list; result is a list of the results
(DEFINE mapcar fun lis) (COND ((NULL? lis) '()) (ELSE (CONS (fun (CAR lis)) (mapcar fun (CDRlis))))
))
Organization of Programming Languages-Cheng
EVAL Scheme interpreter itself is a function named EVAL
The EVAL function can also be called directly by Schemeprograms It is possible for Scheme to create expressions and
calls EVAL to evaluate them
Example: adding a list of numbers Cannot apply “+” directly on a list because “+” takes
any number of numeric atoms as arguments (+ 1 2 3 4) is OK (+ (1 2 3 4)) is not
Organization of Programming Languages-Cheng
EVAL
One approach: ((DEFINE (adder lis) (COND ((NULL? lis) 0) (ELSE (+ (CAR lis) (adder (CDRlis))))
Another approach: ((DEFINE (adder lis) (COND ((NULL? lis) 0) (ELSE (EVAL (CONS '+ lis)))))
(adder ‘(3 4 6)) causes adder to build the list (+ 3 4 6) The list is then submitted to EVAL, which invokes +
and returns the result 13
Organization of Programming Languages-Cheng
Scheme: Output Functions
Output Utility Functions: (DISPLAY expression)
Example: (DISPLAY (car ‘(A B C)))
(NEWLINE)
Organization of Programming Languages-Cheng