1 / 14

Some functions

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)

ulani
Download Presentation

Some functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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. 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. Tuples The type (t1,t2,…,tn) consists of tuples of values (v1,v2,…,vn) in which v1::t1,…,vn::tn. 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. 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) -> a snd :: (a,b) -> b fst (x,y) = x snd (x,y) = y

  6. curry g x (x,y) g y uncurry f x (x,y) f y Currying and Uncurrying multiply :: Int -> Int -> Int multiply x y = x*y multiplyUC :: (Int,Int) -> Int multiplyUC (x,y) = x*y

  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. 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. 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 <- ex, even 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. 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. (++) :: [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. 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. 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. 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)) ([], [])

More Related