Skip to content

Instantly share code, notes, and snippets.

@jordi-petit
Last active March 20, 2018 09:14
Show Gist options
  • Save jordi-petit/648c8e3b2b35d10d9cb89ca12a9ce388 to your computer and use it in GitHub Desktop.
Save jordi-petit/648c8e3b2b35d10d9cb89ca12a9ce388 to your computer and use it in GitHub Desktop.
Useful high order functions in Haskell

Useful high order functions in Haskell

flip

  • Header:

    flip :: (a -> b -> c) -> (b -> a -> c)
  • Description:

    flip f returns the f function but with its two parameters inverted. It is defined by

    flip f x y = f y x
  • Examples:

    half = flip div 2
    
    half 10
    πŸ‘‰ 5

composition (.)

  • Header:

    (.) :: (b -> c) -> (a -> b) -> a -> c
  • Description:

    f . g is the composition of functions f i g.

  • Examples:

    bigger3 = take 3 . reverse . sort
    
    bigger3 [3, 1, 2, 6, 7]
    πŸ‘‰ [7, 6, 3]

Β  Β ```

aplication ($)

  • Header:

    ($) :: (a -> b) -> a -> b
  • Description:

    f $ xis the same as f x. Looks like unnecessay, but because of its low priority, it can save us from closing lots of parameters!

  • Examples:

    tail (tail (tail (tail "Jordi")))
    πŸ‘‰ "i"
    tail $ tail $ tail $ tail "Jordi"
    πŸ‘‰ "i"

map

  • Header:

    map :: (a -> b) -> [a] -> [b]
  • Description:

    map f xs is the list obtained when applying f to each element of the xs list, so that map f [x1, x2, ..., xn] Γ©s [f x1, f x2, ..., f xn].

  • Examples:

    map even [2, 4, 6, 7]
    πŸ‘‰ [True, True, True, False]
    
    map (*2) [2, 4, 6, 7]
    πŸ‘‰ [4, 8, 12, 14]

filter

  • Header:

    filter :: (a -> Bool) -> [a] -> [a]
  • Description:

    filter p xs is the sublist of elements from xs that satisfy the p predicate.

  • Examples:

    filter even [2, 1, 4, 6, 7]
    πŸ‘‰ [2, 4, 6]

zipWith

  • Header:

    zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
  • Description:

    zipWith op xs ys is the list obtained operating each element of xs with each elements of ys via the op function, from left to right, as long as there are elements.

  • Examples:

    zipWith (+) [1, 2, 3] [5, 1, 8, 9]
    πŸ‘‰ [6, 3, 11]

all

  • Header:

    all :: (a -> Bool) -> [a] -> Bool
  • Description:

    all p xs tells whether all elements in xs satisfy the p predicate.

  • Examples:

    all even [2, 1, 4, 6, 7]
    πŸ‘‰ False
    all even [2, 4, 6]
    πŸ‘‰ True

any

  • Header:

    any :: (a -> Bool) -> [a] -> Bool
  • Description:

    any p xs tells whether some element in xs satisfies the p predicate.

  • Examples:

    any even [2, 1, 4, 6, 7]
    πŸ‘‰ True
    all odd [2, 4, 6]
    πŸ‘‰ False

dropWhile

  • Header:

    dropWhile :: (a -> Bool) -> [a] -> [a]
  • Description:

    dropWhile p xs Γ©s la subllista de xs que elimina els primers elements de xs que compleixen el predicat p (fins al final o al primer qua no la compleix).

  • Examples:

    dropWhile even [2, 4, 6, 7, 8]
    πŸ‘‰ [7, 8]
    dropWhile even [2, 4]
    πŸ‘‰ []

takeWhile

  • Header:

    takeWhile :: (a -> Bool) -> [a] -> [a]
  • Description:

    takeWhile p xs is the sublist of xs that contains the first elements of xs that satisfy the p predicate.

  • Examples:

    takeWhile even [2, 4, 6, 7, 8]
    πŸ‘‰ [2, 4, 6]
    takeWhile even [1, 3]
    πŸ‘‰ []

iterate

  • Header:

    iterate :: (a -> a) -> a -> [a]
  • Description:

    iterate f x returns the infinite list [x, f x, f (f x), f (f (f x)), ...].

  • Examples:

    iterate (*2) 1
    πŸ‘‰ [1, 2, 4, 8, 16, ...]

foldl

  • Header:

    foldl :: (b -> a -> b) -> b -> [a] -> b
  • Description:

    foldl βŠ• x0 xs folds an operator βŠ• from the left, so that foldl βŠ• x0 [x1, x2, ..., xn] Γ©s (((x0 βŠ• x1) βŠ• x2) βŠ• ...) βŠ• xn.

  • Examples:

    foldl (+) 0 [3, 2, (-1)]
    πŸ‘‰ 4

foldr

  • Header:

    foldr :: (a -> b -> b) -> b -> [a] -> b
  • Description:

    foldr βŠ• x0 xs folds an operator βŠ• from the right, so that foldr βŠ• x0 [x1, x2, ..., xn] Γ©s x1 βŠ• (x2 ... βŠ• (xn βŠ• x0))).

  • Examples:

    foldr (+) 0 [3, 2, (-1)]
    πŸ‘‰ 4

scanl

  • Header:

    scanl :: (b -> a -> b) -> b -> [a] -> [b]
  • Description:

    scanl f z xs is just like foldl f z xs but rather than returning the final results, it returns the list with all the partial results.

  • Examples:

    scanl (+) 0 [3, 2, (-1)]
    πŸ‘‰ [0, 3, 5, 4]

scanr

  • Header:

    scanr :: (a -> b -> b) -> b -> [a] -> [b]
  • Description:

    scanr f z xs is just like foldr f z xs but rather than returning the final results, it returns the list with all the partial results.

  • Examples:

    scanr (+) 0 [3, 2, (-1)]
    πŸ‘‰ [4, 1, -1, 0]

const

  • Header:

    const :: a -> b -> a
  • Description:

Β  Β const x is the function tha always returns x, independently of what is applied to it.

  • Examples:

    map (const 42) [1 .. 5]
    πŸ‘‰ [42, 42, 42, 42, 42]

id

  • Header:

    id :: a -> a
  • Description:

Β  Β id Γ©s the identity function. Looks unnecessary but may be useful at some point.

  • Examples:

    map id [1 .. 5]
    πŸ‘‰ [1, 2, 3, 4, 5]
@felixarpa
Copy link

felixarpa commented Nov 5, 2017

A la signatura i a la descripciΓ³ de takeWhile posa dropWhile

@jordi-petit
Copy link
Author

@felixarpa: Arreglat, grΓ cies! πŸ‘

@NVSR-Sande
Copy link

good set of functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment