12/2/2013 1
CSCI-2325 Functional Programming with Haskell Mohammad T . Irfan - - PDF document
CSCI-2325 Functional Programming with Haskell Mohammad T . Irfan - - PDF document
12/2/2013 CSCI-2325 Functional Programming with Haskell Mohammad T . Irfan 12/2/13 Recap: Functional Programming 1 12/2/2013 Functional Programming Mimic mathematical functions No variables No assignment statements How
12/2/2013 2
Functional Programming
Mimic mathematical functions No variables No assignment statements How about iterative statement?
Alternative?
Context-independent
Referential transparency
Interesting facts: LISP and LISP Machine
John McCarthy LISP(1958) Knight machine (1980s)
12/2/2013 3
Core concept: λ calculus
Alonzo Church (1941) λ expression
Parameters and mapping of a function No name
Example
λ(x) x * x * x Evaluation: (λ(x) x * x * x)(2) produces 8
Haskell
12/2/2013 4
Resources
Installation
http://www.haskell.org/platform/
Haskell (GHCi) commands
http://www.haskell.org/ghc/docs/7.4.1/html/users_gui
de/ghci-commands.html
Learning
Best book: Miran Lipovaca’s Learn You a Haskell
for Great Good!
http://learnyouahaskell.com/ (free online version)
Useful how-to page
http://www.haskell.org/haskellwiki/Category:How_to
Other resources:
http://www.haskell.org/haskellwiki/Learning_Haskell
12/2/2013 5
Haskell Warm-up exercises
Elementary functions
Make a myFunctions.hs file and define the
following functions in it
doubleMe x = x + x addSquares x y = x*x + y*y
Using the terminal go to the folder of that .hs
file
Execute this command: ghci Load the .hs file
:load myFunctions.hs (or, :l myFunctions.hs)
Use your functions
addSquares 5 10
If you change the .hs file => Execute :r to reload
12/2/2013 6
fibonacci n =
if n == 0 then 1 else if n == 1 then 1 else if n > 1 then fibonacci (n-1)+fibonacci (n-2) else 0
fib n
| n == 0 = 1 | n == 1 = 1 | n > 1 = fib (n-1) + fib (n-2) | otherwise = 0
Recursion – Fibonacci numbers
Guard
Recursion – factorial
factorial n
| n == 0 = 1 | n > 0 = n * factorial (n-1)
- - why not factorial n – 1 ?
Try this: factorial 100
Comment
12/2/2013 7
Recursion – sorting
sort [] = [] sort (head:remainingList)
= sort [b | b <- remainingList, b < head] ++ [head] ++ sort [b | b <- remainingList, b >= head]
Empty list List generator
Lists
evens = [0, 2 .. 10]
In terminal (ghci): let evens = [0, 2 .. 10]
evens = [2*x | x <- [0..5] ] Infinite lists
allEvens = [0, 2 ..]
Are these assignment statements?
12/2/2013 8
Anatomy of a list
Two parts
head List of the remaining elements (AKA tail)
Functions head and tail return these
head evens tail evens
Joining head and tail by : operator
0 : [2, 4 .. 10] will give [0, 2, 4, 6, 8, 10]
Reference
http://www.haskell.org/haskellwiki/How_to_work_on_lists
More list examples
Factors of a number n
factors n = [f | f <- [1 .. n], mod n f
== 0] Prime numbers
primes = primeGen' [2 ..]
where primeGen' (p:xs) = p : primeGen' [q | q <- xs, mod q p /= 0] Getting the first 10 prime numbers
take 10 primes Side note: This is not really the sieve of Eratosthenes
http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP .pdf
Lazy evaluation
12/2/2013 9
Recursion vs. iteration
Iterative factorial
factorial n = product [1 .. n]
Iterative sum
sum [1 .. 5]
Recursive sum
recSum [] = 0
recSum (x:xs) = x + recSum xs
recSum is polymorphic (Works with any compatible type)
n-queens problem
queens n = solve n where solve k | k <= 0 = [ [] ] | otherwise = [ h:partial | partial <- solve (k-1), h <- [0..(n-1)], safe h partial] safe h partial = and [ not (checks h partial i) | i <- [0..(length partial - 1)]] checks h partial i = h == partial!!i || abs(h - partial!!i) == i+1