ch15-Functional Programming
ch15-Functional Programming
Programming
Languages
ISBN 0-321-49362-1
Chapter 15 Topics
• Introduction
• Mathematical Functions
• Fundamentals of Functional Programming Languages
• The First Functional Programming Language: Lisp
• Introduction to Scheme
• Haskell
• Support for Functional Programming in Primarily Imperative
Languages
• Comparison of Functional and Imperative Languages
e.g., (+ 5 2) yields 7
(- 10 5 2 6) yields -3
(* 2 3 5) yields 30
(/ 30 2) yields 15
(ABS -5) yields 5
(SQRT 64) yields 8
(REMAINDER 5 2) yields 1
(MIN 5 2) yields 2
(MAX 5 2) yields 5
• Lambda Expressions
– Form is based on notation
e.g., (LAMBDA (x) (* x x))
x is called a bound variable
( COND
(predicate_1 clause_1)
(predicate_2 clause_2)
…
(predicate_n clause_n)
(else clause_else))
• Examples:
(CAR ′((A B) C D)) returns (A B)
(CAR ′A) is an error
(CDR ′((A B) C D)) returns (C D)
(CDR ′A) is an error
(CDR ′(A)) returns ()
(CONS ′A ′(B C)) returns (A B C)
(CONS ′() ′(A B)) returns (() A B)
(CONS ′(A B) ′(C D)) returns ((A B) C D)
(CONS ′A ′B) returns (A . B) (a dotted pair - a cell
has two atoms)
Copyright © 2015 Pearson. All rights reserved. 1-28
List Functions (continued)
(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)))
(LIST (+ minus_b_over_2a root_part_over_2a))
(- minus_b_over_2a root_part_over_2a))
))
Tail recursive:
(DEFINE (facthelper n factpartial)
(IF (<= n 0)
factpartial
(facthelper(- n 1) (* n factpartial)))
))
(DEFINE (factorial n)
(facthelper n 1))
• Composition
– If h is the composition of f and g, h(x) = f(g(x))
(DEFINE (g x) (* 3 x))
(DEFINE (f x) (+ 2 x))
(DEFINE h x) (+ 2 (* 3 x))) (The composition)
Examples:
((compose CAR CDR) '((a b) c d)) yields c [ CADR]
1) (a b)
2) ((a))
3) (a ((b) c))
1) (a b)
2) ((a))
3) (a ((b) c))
1) (a b x)
2) (((x)))
3) (a (b (c x)))
4) (a (b (c)) x)
(define (length_tr ls n)
( if (null? ls) n
(length_tr (cdr ls) (+ 1 n))
))
– Function definition
• Eg. increment x=x+1
• Eg. and1 a b = if a == b then a else False
• fact 0 = 1
fact 1 = 1
fact n = n * fact (n – 1)
• fib 0 = 1
fib 1 = 1
fib (n + 2) = fib (n + 1) + fib n
sub n
| n < 10 = 0
| n > 100 = 2
| otherwise = 1
square x = x * x
roots2 (a,b,c)=
let e= -b/(2*a)
d= (sqrt (b*b -4*a*c))/(2*a)
in (e+d,e-d)
main= do
print(roots2 (1, 5, 6)) (-2.0, -3.0)
• Catenation is with ++
e.g., [1, 3] ++ [5, 7] results in [1, 3, 5, 7]
• Pattern Parameters
Using : (as CAR) and pattern matching, we can define a
simple function to compute the product of a given list of
numbers
product [] = 1
product (a:x) = a * product x
– Factorial:
fact n = product [1..n]
– Length:
len [] = 0
len (a:x) = len(x) + 1
• List Comprehensions
[n * n * n | n <- [1..50]]
The set of list elements that are smaller or equal to the list head
are sorted and catenated with the head element, then the set
of elements that are greater than the list head are sorted and
catenated onto the previous result.
sort [] = []
sort (h:t) =
sort [b | b <- t, b <= h]
++ [h] ++
sort [b | b <- t, b > h]
• Positive numbers
positives = [0..]
evens=[2,4..]
member [] b = False
member(a:x) b = (a == b)||member x b
squares = [n * n | n ← [0..]]
member squares 16
• Python: lambda a, b : 2 * a - b
fun=lambda x,f:x+f(x)
h=lambda x: x*x 30
fun(5,h)
def map1(ls, f):
sq=[]
for x in ls:
sq.append(f(x))
return sq
• Ruby Blocks
– Are effectively subprograms that are sent to methods,
which makes the method a higher-order subprogram
– A block can be converted to a subprogram object with
lambda
times = lambda {|a, b| a * b}
Use: x = times.(3, 4) (sets x to 12)
• Functional Languages:
– Simple semantics
– Simple syntax
– Less efficient execution
– Programs can automatically be made concurrent