CSCI-2325 More on Haskell Mohammad T . Irfan 12/4/13 Recap: list - - PDF document

csci 2325 more on haskell
SMART_READER_LITE
LIVE PREVIEW

CSCI-2325 More on Haskell Mohammad T . Irfan 12/4/13 Recap: list - - PDF document

12/11/2013 CSCI-2325 More on Haskell Mohammad T . Irfan 12/4/13 Recap: list examples Prime numbers primes = primeGen' [2 ..] where primeGen' (p:xs) = p : primeGen' [q | q <- xs, mod q p /= 0] Getting the first 10 prime


slide-1
SLIDE 1

12/11/2013 1

CSCI-2325 More on Haskell

Mohammad T . Irfan 12/4/13

Recap: list examples

 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

slide-2
SLIDE 2

12/11/2013 2

How many numbers are crossed off?

 Haskell’s implementation of the take

function and list comprehension/generation

 take n (x:xs) = x : take (n-1) xs

 Lambda calculus

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

slide-3
SLIDE 3

12/11/2013 3

Higher-order functions

 Function as a parameter  Curried function  Examples

 max 10 20  take 10 primes

 To visualize this

 :t max

Haskell Curry

3 most useful built-in functions

  • 1. map function list

 map ( > 0) [2, -50, 100] --  [True, False, True]

 Equivalent: [ x > 0 | x <- [2, -50, 100] ]

 map (`mod` 2) [13, 14, 15] --  [1, 0, 1]

  • 2. filter predicate/condition list

 Sorting function (from last class)

sort [] = [] sort (x:xs) = sort (filter (< x) xs) ++ [x] ++ sort (filter (>= x) xs) sort [b| b<-xs, b<x]

slide-4
SLIDE 4

12/11/2013 4

  • 3. Fold: produces single value from a list

(From Haskell.org)

foldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs

foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs)

 Example

 anyTrue = foldr (||) False [True, True ..]  anyTrue = foldl (||) False [True, True ..]

f does not take a list as parameter

Problem: Evaluate Polish prefix expression

 10 + (15 – 5) * 4

 + 10 * - 15 5 4

 10 + 15 – 5 * 4

 + 10 - 15 * 5 4

 Input: String (list of characters) of Polish

prefix expression

 Output: Value of expression  Solution

 Reverse the expr  Traverse it from left to right  Push and pop using a stack

 Implement stack by a list: TOS is head

foldl

slide-5
SLIDE 5

12/11/2013 5

Solution

import Data.List -- at the top evaluatePrefix expr = head ( foldl f [] ( reverse (words expr) ) ) where f (x:y:ys) "*" = (x*y):ys f (x:y:ys) "+" = (x+y):ys f (x:y:ys) "-" = (x-y):ys f xs str = read str:xs

Problem: Finding the shortest path

 From Learn You a Haskell book

slide-6
SLIDE 6

12/11/2013 6

Solution

data Section = Section { getA :: Int, getB :: Int, getC :: Int } deriving (Show) type RoadSystem = [Section] heathrowToLondon :: RoadSystem heathrowToLondon = [Section 50 10 30, Section 5 90 20, Section 40 2 25, Section 10 8 0] data Label = A | B | C deriving (Show) type Path = [(Label, Int)]

roadStep :: (Path, Path) -> Section -> (Path, Path) roadStep (pathA, pathB) (Section a b c) = let priceA = sum $ map snd pathA priceB = sum $ map snd pathB forwardPriceToA = priceA + a crossPriceToA = priceB + b + c forwardPriceToB = priceB + b crossPriceToB = priceA + a + c newPathToA = if forwardPriceToA <= crossPriceToA then (A,a):pathA else (C,c):(B,b):pathB newPathToB = if forwardPriceToB <= crossPriceToB then (B,b):pathB else (C,c):(A,a):pathA in (newPathToA, newPathToB)

  • - test it: roadStep ([], []) (head heathrowToLondon)
slide-7
SLIDE 7

12/11/2013 7

  • ptimalPath :: RoadSystem -> Path
  • ptimalPath roadSystem =

let (bestAPath, bestBPath) = foldl roadStep ([],[]) roadSystem in if sum (map snd bestAPath) <= sum (map snd bestBPath) then reverse bestAPath else reverse bestBPath

  • - test it: optimalPath heathrowToLondon

Lab Assignment

 Either one of the following two

 Implement a Sudoko solver using Haskell  Research on infinite lists and lazy evaluation in

imperative and object oriented programming languages

 Pointer: Functional thinking: Laziness, Part 1

Exploring lazy evaluation in Java by Neal Ford http://www.ibm.com/developerworks/opensource/lib rary/j-ft18/j-ft18-pdf.pdf