GoConqr has a new look! Let us know what you think.

Flashcards

Standard Prelude Definitions

prxd33 Functional Programming English 37 cards

Cards in this deck (37)

  1. 1Decide if a character is a lower-caseisLower :: Char → Bool
    isLower c = c ≥ ’a’ ∧ c ≤ ’z’
  2. 2Decide if a character is alphabeticisAlpha :: Char → Bool
    isAlpha c = isLower c ∨ isUpper c
  3. 3Decide if a character is a digitisDigit :: Char → Bool
    isDigit c = c ≥ ’0’ ∧ c ≤ ’9’
  4. 4Decide if a character is alpha-numericisAlphaNum :: Char → Bool
    isAlphaNum c = isAlpha c ∨ isDigit c
  5. 5Decide if an integer is eveneven :: Integral a ⇒ a → Bool
    even n = n ‘mod‘ 2 == 0
  6. 6Second Select the second component of a pairsnd :: (a,b) → b
    snd (x,y) = y
  7. 7Decide if a list is emptynull :: [a] → Bool
    null [ ] = True
    null ( : ) = False
  8. 8Decide if a value is an element of a list:elem :: Eq a ⇒ a → [a] → Bool
    elem x xs = any (== x) xs
  9. 9Decide if all logical values in a list are Trueand :: [Bool] → Bool
    and = foldr (∧) True
  10. 10Select the first element of a non-empty listhead :: [a] → a
    head (x : ) = x
  11. 11Select the last element of a non-empty listlast :: [a] → a
    last [x ] = x
    last ( : xs) = last xs
  12. 12Select the nth element of a non-empty list(!!) :: [a] → Int → a
    (x : ) !! 0 = x
    ( : xs) !! (n +1) = xs !! n
  13. 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
  14. 14Select all elements of a list that satisfy a predicatefilter :: (a → Bool) → [a] → [a]
    filter p xs = [x | x ← xs,p x ]
  15. 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 = [ ]
  16. 16Remove the first element from a non-empty listtail :: [a] → [a]
    tail ( : xs) = xs
  17. 17Remove the last element from a non-empty listinit :: [a] → [a]
    init [ ] = [ ]
    init (x : xs) = x :init xs
  18. 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
  19. 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
  20. 20Split a list at the nth elementsplitAt :: Int → [a] → ([a],[a])
    splitAt n xs = (take n xs,drop n xs)
  21. 21Split a list using a predicatespan :: (a → Bool) → [a] → ([a],[a])
    span p xs = (takeWhile p xs,dropWhile p xs)
  22. 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)
  23. 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)
  24. 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
  25. 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
  26. 26Produce an infinite list of identical elementsrepeat :: a → [a]
    repeat x = xs where xs = x : xs
  27. 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
  28. 28Lengthlength :: [a] → Int
    length = foldl (λn → n +1) 0
    length :: [a] → Int
    length [ ] = 0
    length (_:xs) = 1 + length xs
  29. 29Sumsum :: Num a ⇒ [a] → a
    sum = foldl (+) 0
    sum :: Num a => [a] -> a
    sum [] = 0
    sum (n:ns) = n + sum ns
  30. 30Productproduct :: Num a ⇒ [a] → a
    product = foldl (∗) 1
    product:: [Int] → Int
    product [] = 1
    product (n:ns) = n * product ns
  31. 31Minimumminimum :: Ord a ⇒ [a] → a
    minimum = foldl1 min
    maximum' :: (Ord a) => [a] -> a
    maximum' [ ] = [ ]
    maximum' [x] = x
    maximum' (x:xs) = max x (maximum' xs)
  32. 32Append(++) :: [a] → [a] → [a]
    [ ]++ys = ys
    (x : xs)++ys = x : (xs ++ys)
    (++):: [a] → [a] → [a]
    [ ] ++ ys = ys
    (x:xs) ++ ys = x : (xs ++ ys)
  33. 33Concatenate a list of lists *************************concat :: [ [a] ] → [a]
    concat = foldr (++) []
  34. 34Reversereverse :: [a] → [a]
    reverse = foldl (λxs x → x : xs) [ ]
    reverse:: [a] → [a]
    reverse [ ] = [ ]
    reverse (x:xs) = reverse xs ++ [x]
  35. 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
  36. 36Identity functionid :: a → a
    id = λx → x
  37. 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

Feedback

What is it about?

What do you think of the new look?

How do you like it?