Download presentation
Presentation is loading. Please wait.
Published byStephanie McDonald Modified over 8 years ago
1
© M. Winter COSC 4P41 – Functional Programming 2.12.1 Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b f $ x = f x (.) :: (b -> c) -> (a -> b) -> (a -> c) (f. g) x = f (g x) flip :: (a -> b -> c) -> b -> a -> c flip f x y = f y x
2
© M. Winter COSC 4P41 – Functional Programming 2.22.2 Recursion fac :: Int -> Int fac n | n == 0= 1 | n > 0= fac (n-1) * n fac :: Int -> Int fac n | n == 0= 1 | n > 0= fac (n-1) * n | otherwise= error ”fac only defined on natural numbers”
3
© M. Winter COSC 4P41 – Functional Programming 2.32.3 Primitive Recursion fun :: Int -> Int fun n | n == 0 = … | n > 0 = … fun (n-1) … data Nat = Zero | Succ Nat fun :: Nat -> Nat fun Zero= … fun (Succ n)= … (fun n) …
4
© M. Winter COSC 4P41 – Functional Programming 2.42.4 Tuples The type (t 1,t 2,…,t n ) consists of tuples of values (v 1,v 2,…,v n ) in which v 1 ::t 1,…,v n ::t n. Examples: minAndMax :: Int -> Int -> (Int,Int) minAndMax x y | x >= y = (y,x) | otherwise = (x,y) type ShopItem = (String,Int) A type definition is treated as a shorthand in Haskell – wherever a name like ShopItem is used, it has exactly the same effect as if (String,Int) had been written.
5
© M. Winter COSC 4P41 – Functional Programming 2.52.5 Pattern Matching addPair :: (Int,Int) -> Int addPair (x,y) = x+y shift :: ((Int,Int),Int) -> (Int,(Int,Int)) shift ((x,y),z) = (x,(y,z)) name :: ShopItem -> String price :: ShopItem -> Int name (n,p) = n price (n,p) = p fst :: (a,b) -> asnd :: (a,b) -> b fst (x,y) = xsnd (x,y) = y
6
© M. Winter COSC 4P41 – Functional Programming 2.62.6 Currying and Uncurrying multiply :: Int -> Int -> Int multiply x y = x*y multiplyUC :: (Int,Int) -> Int multiplyUC (x,y) = x*y g (x,y) x y curry g f(x,y) x y uncurry f
7
© M. Winter COSC 4P41 – Functional Programming 2.72.7 Lists For the type t there is a Haskell type [t] of lists from t. [1,2,3,4,1,4] :: [Int] [True] :: [Bool] [’a’,’a’,’b’] :: [Char] ”aab” :: String but type String = [Char] [fac, (+1)] :: [Int -> Int] [] :: [a]
8
© M. Winter COSC 4P41 – Functional Programming 2.82.8 Lists for enumerated types Lists of numbers, characters and other enumerated types [n.. m] is the list [n,n+1,…,m]; if n exceeds m, the list is empty. [2.. 7] = [2,3,4,5,6,7] [3.1.. 7.0] = [3.1,4.1,5.1,6.1] [’a’.. ’m’] = ”abcdefghijklm” [n,p.. m] is the list of numbers whose first two elements are n and p with the numbers ascending in steps p-n up to m, [7,6.. 3] = [7,6,5,4,3] [0.0,0.3.. 1.0] = [0.0,0.3,0.6,0.9] [’a’,’c’.. ’n’] = ”acegikm”
9
© M. Winter COSC 4P41 – Functional Programming 2.92.9 List comprehension Suppose that the list ex is [2,4,7], then the list comprehension [ 2*n | n <- ex ] will be [4,8,14]. Further examples: [ 2*n | n 3] = [8] [ m+n | (m,n) <- [(2,3),(2,1),(7,8)] ] = [5,3,15] [ m+n | n <- ex, even n, m <- ex, odd m] = [9,11] List comprehension is not a new feature of the language. Each expression using list comprehension can be translated into an expression of the core language, i.e., without list comprehension.
10
© M. Winter COSC 4P41 – Functional Programming 2. 10 Some list functions in Prelude.hs -- data [a] = [] | a : [a] head :: [a] -> a head (x:_) = x last :: [a] -> a last [x] = x last (_:xs) = last xs tail :: [a] -> [a] tail (_:xs) = xs init :: [a] -> [a] init [x] = [] init (x:xs) = x : init xs null :: [a] -> Bool null [] = True null (_:_) = False
11
© M. Winter COSC 4P41 – Functional Programming 2. 11 (++) :: [a] -> [a] -> [a] [] ++ ys = ys (x:xs) ++ ys = x : (xs ++ ys) map :: (a -> b) -> [a] -> [b] map f xs = [ f x | x <- xs ] filter :: (a -> Bool) -> [a] -> [a] filter p xs = [ x | x <- xs, p x ] concat :: [[a]] -> [a] concat = foldr (++) [] length :: [a] -> Int length = foldl' (\n _ -> n + 1) 0 (!!) :: [a] -> Int -> a (x:_) !! 0 = x (_:xs) !! n | n>0 = xs !! (n-1) (_:_) !! _ = error "Prelude.!!: negative index" [] !! _ = error "Prelude.!!: index too large"
12
© M. Winter COSC 4P41 – Functional Programming 2. 12 foldl :: (a -> b -> a) -> a -> [b] -> a foldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs foldl1 :: (a -> a -> a) -> [a] -> a foldl1 f (x:xs) = foldl f x xs foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs) foldr1 :: (a -> a -> a) -> [a] -> a foldr1 f [x] = x foldr1 f (x:xs) = f x (foldr1 f xs) iterate :: (a -> a) -> a -> [a] iterate f x = x : iterate f (f x) reverse :: [a] -> [a] reverse = foldl (flip (:)) []
13
© M. Winter COSC 4P41 – Functional Programming 2. 13 take :: Int -> [a] -> [a] take n _ | n <= 0 = [] take _ [] = [] take n (x:xs) = x : take (n-1) xs drop :: Int -> [a] -> [a] drop n xs | n <= 0 = xs drop _ [] = [] drop n (_:xs) = drop (n-1) xs and, or :: [Bool] -> Bool and = foldr (&&) True or = foldr (||) False any, all :: (a -> Bool) -> [a] -> Bool any p = or. map p all p = and. map p elem, notElem :: Eq a => a -> [a] -> Bool elem = any. (==) notElem = all. (/=)
14
© M. Winter COSC 4P41 – Functional Programming 2. 14 sum, product :: Num a => [a] -> a sum = foldl' (+) 0 product = foldl' (*) 1 maximum, minimum :: Ord a => [a] -> a maximum = foldl1 max minimum = foldl1 min zip :: [a] -> [b] -> [(a,b)] zip = zipWith (\a b -> (a,b)) zipWith :: (a->b->c) -> [a]->[b]->[c] zipWith z (a:as) (b:bs) = z a b : zipWith z as bs zipWith _ _ _ = [] unzip :: [(a,b)] -> ([a],[b]) unzip = foldr (\(a,b) ~(as,bs) -> (a:as, b:bs)) ([], [])
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.