Functional ProgrammingFunctional Programming
Universitatea Politehnica Bucuresti2007-2008
Adina Magda Florea
http://turing.cs.pub.ro/fp_08
sigla_UPB
L1 – Course content
Introduction, programming paradigms, basicconcepts
Introduction to Scheme
Expressions, types, and functions
Lists
Programming techniques
Name binding, recursion, iterations, andcontinuations
Introduction to Haskell
Problem solving paradigms in FP
Course materials
Scheme resources
Almost all you want to know and find about Scheme
Development and execution environment
Books and research articles on Scheme
R. Kent DybvigThe Scheme Programming Language, Second Edition – online book
Haskell
Haskell language design
Requirements
Laboratory: min 6
Laboratory work
Homework
Final exam
Grading
Laboratory assignments and work 30%
Homework 20%Final exam 50%
Lecture No. 1
Introduction to FP
Mathematical functions
LISP
Introduction to Scheme
1. Introduction to FP
The design of the imperative languages is baseddirectly on the von Neumann architecture
Efficiency is the primary concern, rather than thesuitability of the language for software development
The design of the functional languages is basedon mathematical functions
A solid theoretical basis that is also closer to theuser, but relatively unconcerned with thearchitecture of the machines on which programswill run
1.1 Principles of FP
treats computation as evaluation of mathematicalfunctions (and avoids state)
data and programs are represented in the sameway
functions as first-class values
– higher-order functions: functions that operate on, orcreate, other functions
– functions as components of data structures
lamda calculus provides a theoretical frameworkfor describing functions and their evaluation
it is a mathematical abstraction rather than aprogramming language
1.2 History
lambda calculus (Church, 1932)
simply typed lambda calculus (Church, 1940)
lambda calculus as prog. lang. (McCarthy(?), 1960,Landin 1965)
polymorphic types (Girard, Reynolds, early 70s)
algebraic types ( Burstall & Landin, 1969)
type inference (Hindley, 1969, Milner, mid 70s)
lazy evaluation (Wadsworth, early 70s)
Equational definitions Miranda 80s
Type classes Haskell 1990s
1.3 Varieties of FP languages
typed (ML, Haskell) vs untyped (Scheme,Erlang)
Pure vs Impure
impure have state and imperative features
pure have no side effects, “referentialtransparency”
Strict vs Lazy evaluation
1.4 Declarative style of programming
Declarative Style of programming - emphasis isplaced on describing what a program should do ratherthan prescribing how it should do it.
Functional programming - good illustration of thedeclarative style of programming.
A program is viewed as a function from input tooutput.
Logic programming – another paradigm
A program is viewed as a collection of logical rulesand facts (a knowledge-based system). Using logicalreasoning, the computer system can derive new factsfrom existing ones.
1.5 Functional style of programming
computing system is viewed as a functionwhich takes input and delivers output.
The function transforms the input into output .
Functions are the basic building blocks fromwhich programs are constructed.
The definition of each function specifies whatthe function does.
It describes the relationship between the inputand the output of the function.
Examples
Describing a game as a function
Text processing
Text processing: translation
Compiler
1.6 Why functional programming
Functional programming languages are carefullydesigned to support problem solving.
There are many features in these languages which helpthe user to design clear, concise, abstract, modular,correct and reusable solutions to problems.
The functional Style of Programming allows theformulation of solutions to problems to be as easy,clear, and intuitive as possible.
Since any functional program is typically built bycombining well understood simpler functions, thefunctional style naturally enforces modularity.
Why functional programming
Programs are easy to write because the system relieves theuser from dealing with many tedious implementationconsiderations such as memory management, variabledeclaration, etc .
Programs are concise (typically about 1/10 of the size of aprogram in non-FPL)
Programs are easy to understand because functionalprograms have nice mathematical properties (unlike imperativeprograms) .
Functional programs are referentially transparent , that is, if avariable is set to be a certain value in a program; this valuecannot be changed again. That is, there is no assignment butonly a true mathematical equality.
Why functional programming
Programs are easy to reason about becausefunctional programs are just mathematicalfunctions;
hence, we can prove or disprove claims aboutour programs using familiar mathematicalmethods and ordinary proof techniques (such asthose encountered in high school Algebra).
For example we can always replace the lefthand side of a function definition by thecorresponding right hand side.
1.7 Examples of FP languages
Lisp (1960, the first functional language….dinosaur, has notype system)
Hope (1970s an equational fp language)
ML (1970s introduced Polymorphic typing systems)
Scheme (1975, static scoping)
Miranda (1980s equational definitions, polymorphic typing
Haskell (introduced in 1990, all the benefits of above +facilities for programming in the large.)
Erlang (1995 - a general-purpose concurrent programminglanguage and runtime system, introduced by  Ericsson)
The sequential subset of Erlang is a functional language, withdynamic typing.
2. Mathematical functions
Def: A mathematical function is a mapping ofmembers of one set, called the domain set, toanother set, called the range set
lambda expression specifies the parameter(s)and the mapping of a function in the followingform
        (x) x * x * x
   for the function  cube (x) = x * x * x
Mathematical functions
Lambda expressions describe namelessfunctions
Lambda expressions are applied to parameter(s)by placing the parameter(s) after the expression
     e.g.   ((x) x * x * x)(3)
    which evaluates to 27
Mathematical functions
Functional Forms
Def: A higher-order function, or functional form, isone that either takes functions as parameters oryields a function as its result, or both
Functional forms
1. Function Composition
A functional form that takes two functions asparameters and yields a function whose value is thefirst actual parameter function applied to theapplication of the second
       Form: h  f ° g
        which means h (x)  f ( g ( x))
        For f (x)  x * x * x  and  g (x)  x + 3,
        h  f ° g yields (x + 3)* (x + 3)* (x + 3)
Functional forms
2. Construction
A functional form that takes a list of functions asparameters and yields a list of the results ofapplying each of its parameter functions to a givenparameter
      Form: [f, g]
       For f (x)  x * x * x  and  g (x)  x + 3,
       [f, g] (4)  yields  (64, 7)
Functional forms
3. Apply-to-all
A functional form that takes a single function as aparameter and yields a list of values obtained byapplying the given function to each element of a listof parameters
      Form: 
      For h (x)  x * x * x
      ( h, (3, 2, 4))  yields  (27, 8, 64)
3. LISP
Lambda notation is used to specify functions andfunction definitions. Function applications and datahave the same form.
    e.g., 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
The first LISP interpreter appeared only as ademonstration of the universality of the computationalcapabilities of the notation
4. Introduction to Scheme
A mid-1970s dialect of LISP, designed to be acleaner, more modern, and simpler version thanthe contemporary dialects of LISP
Invented by Guy Lewis Steele Jr. and GeraldJay Sussman
Emerged from MIT
Originally called Schemer
Shortened to Scheme because of a 6 character limitationon file names
Introduction to Scheme
Designed to have very few regular constructswhich compose well to support a variety ofprogramming styles
Functional, object-oriented, and imperative
All data type are equal
What one can do to one data type, one can do to alldata types
Introduction to Scheme
Uses only static scoping
Functions are first-class entities
They can be the values of expressions and elementsof lists
They can be assigned to variables and passed asparameters
Introduction to Scheme
Primitive Functions
1.  Arithmetic: +, -, *, /, ABS,SQRT, REMAINDER, MIN, MAX
e.g., (+ 5 2) yields 7
Introduction to Scheme
2. QUOTE -takes one parameter; returns theparameter 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 evaluation whenit is not appropriate
QUOTE can be abbreviated with the apostropheprefix operator
   e.g., '(A B) is equivalent to (QUOTE (A B))
Introduction to Scheme
3. CAR  takes a list parameter; returns the firstelement of that list
         e.g., (CAR '(A B C)) yields A
                 (CAR '((A B) C D)) yields (A B)
4. CDR  takes a list parameter; returns the listafter removing its first element
         e.g., (CDR '(A B C)) yields (B C)
                 (CDR '((A B) C D)) yields (C D)
Introduction to Scheme
5. CONS  takes two parameters, the first of whichcan be either an atom or a list and the second ofwhich is a list; returns a new list that  includesthe first parameter as its first element and thesecond parameter as the remainder of its result
   e.g., (CONS 'A '(B C)) returns (A B C)
Introduction to Scheme
6. LIST - takes any number of parameters;returns a list with the parameters as elements
Introduction to Scheme
Lambda Expressions
Form is based on  notation
     e.g., (LAMBDA (L) (CAR (CAR L)))
     L is called a bound variable
Lambda expressions can be applied
     e.g.,
((LAMBDA (L) (CAR (CAR L))) '((A B) C D))
Introduction to Scheme
A Function for Constructing Functions
   DEFINE - Two forms:
   1. To bind a symbol to an expression
       e.g.,
       (DEFINE pi 3.141593)
       (DEFINE two_pi (* 2 pi))
Introduction to Scheme
2. To bind names to lambda expressions
          e.g.,
         (DEFINE (cube x) (* x x x))
       Example use:
          (cube 4)
Introduction to Scheme
Evaluation process (for normal functions):
1. Parameters are evaluated, in no particular order
2. The values of the parameters are substituted intothe function body
3. The function body is evaluated
4. The value of the last expression in the body is thevalue of the function
(Special forms use a different evaluation process)
Introduction to Scheme
Example:
(DEFINE (square x) (* x x))
 
(DEFINE (hypotenuse side1 side1)
   (SQRT (+ (square side1)
   (square side2)))
)
Introduction to Scheme
Example:
(define
(list-sum lst)
 (cond
  ((null? lst) 0)
  ((pair? (car lst))
  (+(list-sum (car lst)) (list-sum(cdr lst))))
(else
 (+ (car lst) (list-sum (cdrlst))))))
Some conventions
Naming Conventions
predicate is a procedure that always returns aboolean value (#t or #f). By convention,predicates usually have names that end in `?'.
mutation procedure is a procedure that altersa data structure. By convention, mutationprocedures usually have names that end in `!'.
Cases
Uppercase and Lowercase
Scheme doesn't distinguish uppercase andlowercase forms of a letter except withincharacter and string constants; in other words,Scheme is case-insensitive.
For example, Foo is the same identifier asFOO, but 'a' and 'A' are different characters.
Predicate functions
Predicate Functions: (#t is true and #f or()is false)
1. EQ? takes two symbolic parameters; it returns #Tif both parameters are atoms and the two are thesame
         e.g., (EQ? 'A 'A) yields #t
                 (EQ? 'A '(A B)) yields ()
Note that if EQ? is called with list parameters, theresult is not reliable
Also, EQ? does not work for numeric atoms
Predicate functions
Predicate Functions:
2. LIST? takes one parameter; it returns #T if theparameter is a list; otherwise()
3. NULL? takes one parameter; it returns #T if theparameter is the empty list; otherwise()
  Note that NULL? returns #T if the parameter is()
4. Numeric Predicate Functions
    =, <>, >, <, >=, <=, EVEN?, ODD?,ZERO?, NEGATIVE?
Control flow
Control Flow
  1. Selection- the special form, IF
        (IF predicate then_expelse_exp)
     e.g.,
   (IF (<> count 0)
         (/ sum count)
         0
       )
Control flow
Control Flow
2. Multiple Selection - the special form, COND
     General form:
    (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 whose predicate evaluates to true
Example
(DEFINE (compare x y)
   (COND
     ((> x y) (DISPLAY “x is greaterthan y”))
     ((< x y) (DISPLAY “y is greaterthan x”))
     (ELSE (DISPLAY “x and y areequal”))
   )
 )
Example
1. member - takes an atom and a simple list; returns #Tif the atom is in the list; () otherwise
 (DEFINE (member atm lis)
  (COND
    ((NULL? lis) '())
    ((EQ? atm (CAR lis)) #T)
    ((ELSE (member atm (CDR lis)))
  ))
Example
2. equalsimp - takes two simple lists as parameters;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 '())
))
Example
3. equal - takes two general lists as parameters;  returns #T if thetwo 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 '())
))
Example
4. append - takes two lists as parameters; returns thefirst 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)))
))