Flashcards
Standard Prelude Definitions
Cards in this deck (37)
- 1Decide if a character is a lower-caseisLower :: Char → Bool
isLower c = c ≥ ’a’ ∧ c ≤ ’z’ - 2Decide if a character is alphabeticisAlpha :: Char → Bool
isAlpha c = isLower c ∨ isUpper c - 3Decide if a character is a digitisDigit :: Char → Bool
isDigit c = c ≥ ’0’ ∧ c ≤ ’9’ - 4Decide if a character is alpha-numericisAlphaNum :: Char → Bool
isAlphaNum c = isAlpha c ∨ isDigit c - 5Decide if an integer is eveneven :: Integral a ⇒ a → Bool
even n = n ‘mod‘ 2 == 0 - 6Second Select the second component of a pairsnd :: (a,b) → b
snd (x,y) = y - 7Decide if a list is emptynull :: [a] → Bool
null [ ] = True
null ( : ) = False - 8Decide if a value is an element of a list:elem :: Eq a ⇒ a → [a] → Bool
elem x xs = any (== x) xs - 9Decide if all logical values in a list are Trueand :: [Bool] → Bool
and = foldr (∧) True - 10Select the first element of a non-empty listhead :: [a] → a
head (x : ) = x - 11Select the last element of a non-empty listlast :: [a] → a
last [x ] = x
last ( : xs) = last xs - 12Select the nth element of a non-empty list(!!) :: [a] → Int → a
(x : ) !! 0 = x
( : xs) !! (n +1) = xs !! n - 13Select the first n elements of a listtake :: Int → [a] → [a]
take 0 = [ ]
take (n +1) [ ] = [ ]
take (n +1) (x : xs) = x :take n xs - 14Select all elements of a list that satisfy a predicatefilter :: (a → Bool) → [a] → [a]
filter p xs = [x | x ← xs,p x ] - 15Select elements of a list while they satisfy a predicatetakeWhile :: (a → Bool) → [a] → [a]
takeWhile [ ] = [ ]
takeWhile p (x : xs)
| p x = x :takeWhile p xs
| otherwise = [ ] - 16Remove the first element from a non-empty listtail :: [a] → [a]
tail ( : xs) = xs - 17Remove the last element from a non-empty listinit :: [a] → [a]
init [ ] = [ ]
init (x : xs) = x :init xs - 18Drop (Removes the first n elements from a list)drop :: Int → [a] → [a]
drop 0 xs = xs
drop (n +1) [ ] = [ ]
drop (n +1) ( : xs) = drop n xs
drop:: Int → [a] → [a]
drop 0 xs = xs
drop (n+1) [ ] = [ ]
drop (n+1) (_:xs) = drop n xs - 19Remove elements from a list while they satisfy a predicatedropWhile :: (a → Bool) → [a] → [a]
dropWhile [ ] = [ ]
dropWhile p (x : xs)
| p x = dropWhile p xs
| otherwise = x : xs - 20Split a list at the nth elementsplitAt :: Int → [a] → ([a],[a])
splitAt n xs = (take n xs,drop n xs) - 21Split a list using a predicatespan :: (a → Bool) → [a] → ([a],[a])
span p xs = (takeWhile p xs,dropWhile p xs) - 22Process a list using an operator that associates to the rightfoldr :: (a → b → b) → b → [a] → b
foldr v [ ] = v
foldr f v (x : xs) = f x (foldr f v xs) - 23Process a non-empty list using an operator that associates to the rightfoldr1 :: (a → a → a) → [a] → a
foldr1 [x ] = x
foldr1 f (x : xs) = f x (foldr1 f xs) - 24Process a list using an operator that associates to the leftfoldl :: (a → b → a) → a → [b] → a
foldl v [ ] = v
foldl f v (x : xs) = foldl f (fvx) xs - 25Process a non-empty list using an operator that associates to the leftfoldl1 :: (a → a → a) → [a] → a
foldl1 f (x : xs) = foldl f x xs - 26Produce an infinite list of identical elementsrepeat :: a → [a]
repeat x = xs where xs = x : xs - 27Zip (Produces a list of pairs from a pair of lists)zip :: [a] → [b] → [ (a,b) ]
zip [ ] = [ ]
zip [ ] = [ ]
zip (x : xs) (y : ys) = (x,y) : zip xs ys
zip:: [a] → [b] → [(a,b)]
zip [ ] _ = []
zip _ [ ] = [ ]
zip (x:xs) (y:ys) = (x,y) : zip xs ys - 28Lengthlength :: [a] → Int
length = foldl (λn → n +1) 0
length :: [a] → Int
length [ ] = 0
length (_:xs) = 1 + length xs - 29Sumsum :: Num a ⇒ [a] → a
sum = foldl (+) 0
sum :: Num a => [a] -> a
sum [] = 0
sum (n:ns) = n + sum ns - 30Productproduct :: Num a ⇒ [a] → a
product = foldl (∗) 1
product:: [Int] → Int
product [] = 1
product (n:ns) = n * product ns - 31Minimumminimum :: Ord a ⇒ [a] → a
minimum = foldl1 min
maximum' :: (Ord a) => [a] -> a
maximum' [ ] = [ ]
maximum' [x] = x
maximum' (x:xs) = max x (maximum' xs) - 32Append(++) :: [a] → [a] → [a]
[ ]++ys = ys
(x : xs)++ys = x : (xs ++ys)
(++):: [a] → [a] → [a]
[ ] ++ ys = ys
(x:xs) ++ ys = x : (xs ++ ys) - 33Concatenate a list of lists *************************concat :: [ [a] ] → [a]
concat = foldr (++) [] - 34Reversereverse :: [a] → [a]
reverse = foldl (λxs x → x : xs) [ ]
reverse:: [a] → [a]
reverse [ ] = [ ]
reverse (x:xs) = reverse xs ++ [x] - 35Map (Apply a function to all elements of a list)map :: (a → b) → [a] → [b]
map f xs = [f x | x ← xs ]
map :: (a -> b) -> [a] -> [b]
map f [ ] = [ ]
map f (x:xs) = f x : map f xs - 36Identity functionid :: a → a
id = λx → x - 37Factorialfactorial :: Int → Int
factorial 0 = 1
factorial (n+1) = (n+1) * factorial n
Information
- Description
- Functional Programming Flashcards on Standard Prelude Definitions, created by prxd33 on 11/04/2014.
- Created by
- prxd33 · Apr 2014
- Updated
- Jul 20, 2016
- Views
- 11
- Rating