1
Shell CSCE 314 TAMU
Functions continued
CSCE 314: Programming Languages
- Dr. Dylan Shell
Functions continued 1 Shell CSCE 314 TAMU Outline Defining - - PowerPoint PPT Presentation
Shell CSCE 314 TAMU CSCE 314: Programming Languages Dr. Dylan Shell Functions continued 1 Shell CSCE 314 TAMU Outline Defining Functions List Comprehensions Recursion 2 Shell CSCE 314 TAMU A Function without Recursion Many
1
Shell CSCE 314 TAMU
2
Shell CSCE 314 TAMU
3
Shell CSCE 314 TAMU
factorial :: Int → Int factorial n = product [1..n]
factorial maps any integer n to the product of the integers between 1 and n
factorial 4 product [1..4]
product [1,2,3,4]
1*2*3*4
24
4
Shell CSCE 314 TAMU
factorial 0 = 1 factorial n = n * factorial (n-1) factorial maps 0 to 1, and any other positive integer to the product of itself and the factorial of its predecessor. factorial 3 3 * factorial 2
3 * (2 * factorial 1)
3 * (2 * (1 * factorial 0))
3 * (2 * (1 * 1))
3 * (2 * 1)
6 3 * 2
5
Shell CSCE 314 TAMU
> factorial (-1) Error: Control stack overflow
6
Shell CSCE 314 TAMU
⬛ Some functions, such as factorial, are simpler to
⬛ As we shall see, however, many functions can
⬛ Properties of functions defined using recursion can
7
Shell CSCE 314 TAMU
product :: [Int] → Int product [] = 1 product (n:ns) = n * product ns product maps the empty list to 1, and any non-empty list to its head multiplied by the product of its tail. product [2,3,4] 2 * product [3,4]
2 * (3 * product [4])
2 * (3 * (4 * product []))
2 * (3 * (4 * 1))
24
8
Shell CSCE 314 TAMU
length :: [a] → Int length [] = 0 length (_:xs) = 1 + length xs length maps the empty list to 0, and any non-empty list to the successor
length [1,2,3] 1 + length [2,3]
1 + (1 + length [3])
1 + (1 + (1 + length []))
1 + (1 + (1 + 0))
3
9
Shell CSCE 314 TAMU
reverse :: [a] → [a] reverse [] = [] reverse (x:xs) = reverse xs ++ [x] reverse maps the empty list to the empty list, and any non-empty list to the reverse of its tail appended to its head. reverse [1,2,3] reverse [2,3] ++ [1]
(reverse [3] ++ [2]) ++ [1]
((reverse [] ++ [3]) ++ [2]) ++ [1]
(([] ++ [3]) ++ [2]) ++ [1]
[3,2,1]
10
Shell CSCE 314 TAMU
zip :: [a] → [b] → [(a,b)] zip [] _ = [] zip _ [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys drop :: Int → [a] → [a] drop n xs | n <= 0 = xs drop _ [] = [] drop n (_:xs) = drop (n-1) xs
(++) :: [a] → [a] → [a] [] ++ ys = ys (x:xs) ++ ys = x : (xs ++ ys)
11
Shell CSCE 314 TAMU
numberList xs = zip [0..] xs > numberList “abcd” [(0,’a’),(1,’b’),(2,’c’),(3,’d’)]
12
Shell CSCE 314 TAMU
expensiveLen [] = 0 expensiveLen (_:xs) = 1 + expensiveLen xs
stillExpensiveLen ls = len 0 ls where len z [] = z len z (_:xs) = len (z+1) xs cheapLen ls = len 0 ls where len z [] = z len z (_:xs) = let z’ = z+1 in z’ `seq` len z’ xs > expensiveLen [1..10000000] -- takes quite a while > stillExpensiveLen [1..10000000] -- also takes a long time > cheapLen [1..10000000] -- less memory and time
13
Shell CSCE 314 TAMU
14
Shell CSCE 314 TAMU
qsort :: [Int] -> [Int] qsort [] = [] qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger where smaller = [a | a <- xs, a <= x] larger = [b | b <- xs, b > x]
15
Shell CSCE 314 TAMU
16
Shell CSCE 314 TAMU
and :: [Bool] → Bool
concat :: [[a]] → [a]
concat [] = [] concat (xs:xss) = xs ++ concat xss and [] = True and (b:bs) = b && and bs
17
Shell CSCE 314 TAMU
(!!) :: [a] → Int → a
elem :: Eq a ⇒ a → [a] → Bool
replicate :: Int → a → [a]
(!!) (x:_) 0 = x (!!) (_:xs) n = (!!) xs (n-1) elem x [] = False elem x (y:ys) | x==y = True | otherwise = elem x ys replicate 0 _ = [] replicate n x = x : replicate (n-1) x
18
Shell CSCE 314 TAMU
merge :: [Int] → [Int] → [Int]
> merge [2,5,6] [1,3,4] [1,2,3,4,5,6] merge [] ys = ys merge xs [] = xs merge (x:xs) (y:ys) = if x <= y then x: merge xs (y:ys) else y: merge (x:xs) ys
19
Shell CSCE 314 TAMU
msort :: [Int] → [Int]
halves xs = splitAt (length xs `div` 2) xs msort [] = [] msort [x] = [x] msort xs = merge (msort ys) (msort zs) where (ys,zs) = halves xs
20
Shell CSCE 314 TAMU
and :: [Bool] → Bool
concat :: [[a]] → [a]
and [] = True and (b:bs) = b && and bs concat [] = [] concat (xs:xss) = xs ++ concat xss
21
Shell CSCE 314 TAMU
(!!) :: [a] → Int → a
elem :: Eq a ⇒ a → [a] → Bool
replicate :: Int → a → [a]
replicate 0 _ = [] replicate n x = x : replicate (n-1) x (!!) (x:_) 0 = x (!!) (_:xs) n = (!!) xs (n-1) elem x [] = False elem x (y:ys) | x==y = True | otherwise = elem x ys
22
Shell CSCE 314 TAMU
merge :: [Int] → [Int] → [Int]
> merge [2,5,6] [1,3,4] [1,2,3,4,5,6] merge [] ys = ys merge xs [] = xs merge (x:xs) (y:ys) = if x <= y then x: merge xs (y:ys) else y: merge (x:xs) ys
23
Shell CSCE 314 TAMU
msort :: [Int] → [Int]
halves xs = splitAt (length xs `div` 2) xs msort [] = [] msort [x] = [x] msort xs = merge (msort ys) (msort zs) where (ys,zs) = halves xs