En este manual se presentan ejemplos de las funciones de básicas de Haskell que se usan en los temas de introducción a la programación funcional con Haskell y en el libro de ejercicios Piensa en Haskell.

1 Funciones predefinidas de Haskell (Prelude)

(+), (-), (*), (/), (^), (^^), (**), (==), (/=), (<), (<=), (>), (>=), (&&), (||), (:), (++), (!!), (.), abs, all, and, any, ceiling, concat, concatMap, curry, div, divMod, drop, dropWhile, elem, even, filter, flip, floor, foldl, foldl1, foldr, fromIntegral, fst, gcd, head, init, iterate, last, lcm, length, map, max, maximum, min, minimum, mod, not, notElem, null, odd, or, product, quot, quotRem, read, rem, repeat, replicate, reverse, round, scanl, scanl1, scanr, show, signum, snd, splitAt, sqrt, sum, tail, take, takeWhile, truncate, uncurry, until, zip, zip3, zipWith

λ> 2+3+1
6
λ> 2.4+5
7.4
λ> 7-2-1
4
λ> (7-2)-1
4
λ> 7-(2-1)
6
λ> 5-2.4
2.6
λ> 2*3
6
λ> 2.3*4
9.2
λ> 70/10/2
3.5
λ> (70/10)/2
3.5
λ> 70/(10/2)
14.0
λ> 7.6/2
3.8
λ> 2^3
8
λ> 10^2^3
100000000
λ> 10^(2^3)
100000000
λ> (10^2)^3
1000000
λ> 4.3^2
18.49
λ> 5^^2
25.0
λ> 5^^(-1)
0.2
λ> 2.5^^(-2)
0.16
λ> 5**2
25.0
λ> 5**(-1)
0.2
λ> 16**(1/2)
4.0
λ> 0.0081**0.25
0.3
λ> 2+3 == 1+4
True
λ> 2+3 == 1+0
False
λ> [2,3,5] == 2 : [3,5]
True
λ> [2,3,5] == [3,5,2]
False
λ> "Salamanca" == "Sala" ++ "manca"
True
λ> "Salamanca" == "sala" ++ "manca"
False
λ> 2+3 /= 1+0
True
λ> 2+3 /= 1+4
False
λ> 2+3 < 7
True
λ> 2+3 < 5
False
λ> [2,3] < [5,0,4]
True
λ> [2,3] < [1,0,4]
False
λ> "ayer" < "hoy"
True
λ> 2+3 <= 7
True
λ> 2+3 <= 5
True
λ> 2+3 <= 4
False
λ> 2+3 > 4
True
λ> 2+3 > 5
False
λ> 2+3 >= 4
True
λ> 2+3 >= 5
True
λ> 2+3 >= 7
False
λ> 1 < 2+3 && 3+4 <= 9
True
λ> 1 < 2+3 && 3+7 <= 9
False
λ> 1 < 2+3 || 3+7 <= 9
True
λ> 6 < 2+3 || 3+7 <= 9
False
λ> 2:[5,3]
[2,5,3]
λ> 2:[]
[2]
λ> 'B':"etis"
"Betis"
λ> [2,5] ++ [3,7,6]
[2,5,3,7,6]
λ> "Sala" ++ "manca"
"Salamanca"
λ> [7,9,6,5] !! 2
6
λ> "Sevilla" !! 3
'i'
λ> ((+2) . (*3)) 5
17
λ> ((*3) . (+2)) 5
21
λ> abs (-7)
7
λ> abs 3.4
3.4
λ> all even [2,6,8]
True
λ> all even [2,5,8]
False
λ> and [1 < 2+3, 3+4 <= 9, 4 == 7-3]
True
λ> and [1 < 2+3, 3+4 <= 9, 5 == 7-3]
False
λ> any even [3,2,5]
True
λ> any even [3,1,5]
False
λ> ceiling 4.2
5
λ> ceiling (-4.2)
-4
λ> concat [[2,5],[7],[4,9,6]]
[2,5,7,4,9,6]
λ> concat ["Sal","a","manca"]
"Salamanca"
λ> concatMap (replicate 2) [3,5,4]
[3,3,5,5,4,4]
λ> concatMap (replicate 2) "abc"
"aabbcc"
λ> let f (x,y) = x+y
λ> f (2,3)
5
λ> (curry f) 2 3
5
λ> div 7 2
3
λ> 7 `div` 2
3
λ> (-7) `div` 2
-4
λ> 7 `div` (-2)
-4
λ> (-7) `div` (-2)
3
λ> divMod 7 2
(3,1)
λ> divMod (-7) 2
(-4,1)
λ> divMod 7 (-2)
(-4,-1)
λ> divMod (-7) (-2)
(3,-1)
λ> drop 2 [7,5,9,6,8]
[9,6,8]
λ> drop 9 [7,5,9,6,8]
[]
λ> drop (-4) [7,5,9,6,8]
[7,5,9,6,8]
λ> dropWhile even [4,8,6,5,0,2,7]
[5,0,2,7]
λ> dropWhile (<7) [4,3,9,1,0,2,7]
[9,1,0,2,7]
λ> elem 3 [5,3,7]
True
λ> 3 `elem` [5,3,7]
True
λ> 4 `elem` [5,3,7]
False
λ> even 6
True
λ> even 7
False
λ> filter even [3,4,6,7,5,0] 
[4,6,0]
λ> filter (<6) [3,4,6,7,5,0] 
[3,4,5,0]
λ> let f x y = x-y
λ> (flip f) 3 7
4
λ> floor 4.2
4
λ> floor 4.7
4
λ> floor (-4.2)
-5
λ> floor (-4.7)
-5
λ> foldl (-) 20 [2,5,3]
10
λ> ((20-2)-5)-3
10
λ> foldl (-) 20 [2,5,3,4]
6
λ> (((20-2)-5)-3)-4
6
λ> foldl1 (-) [20,2,5,3]
10
λ> ((20-2)-5)-3
10
λ> foldr (-) 20 [2,5,3]
-20
λ> 2-(5-(3-20))
-20
λ> foldr (-) 20 [2,5,3,4]
16
λ> 2-(5-(3-(4-20)))
16
λ> fromIntegral 23 :: Float
23.0
λ> fst (5,2)
5
λ> gcd 12 30
6
λ> head [3,2,5]
3
λ> head "Betis"
'B'
λ> init [3,7,2]
[3,7]
λ> init "colas"
"cola"
λ> take 10 (iterate (*2) 1)
[1,2,4,8,16,32,64,128,256,512]
λ> last [3,2,5]
5
λ> last "colas"
's'
λ> lcm 12 30
60
λ> length [4,2,5]
3
λ> length "Betis"
5
λ> map (^2) [3,10,5]
[9,100,25]
λ> map (2^) [3,10,5]
[8,1024,32]
λ> map even [3,10,5]
[False,True,False]
λ> max 3 7
7
λ> max 9 2
9
λ> maximum [3,5,2,1]
5
λ> min 3 7
3
λ> min 9 2
2
λ> minimum [3,5,2,1]
1
λ> mod 8 3
2
λ> 8 `mod` 3
2
λ> (-8) `mod` 3
1
λ> (-8) `rem` 3
-2
λ> not (2+3 == 5)
False
λ> not (2+3 /= 5)
True
λ> notElem 4 [5,3,7]
True
λ> 4 `notElem` [5,3,7]
True
λ> 3 `notElem` [5,3,7]
False
λ> null (tail [3])
True
λ> null [3]
False
λ> odd 23
True
λ> odd 32
False
λ> or [1 < 2+3, 3+4 <= 9, 5 == 7-3]
True
λ> or [1 > 2+3, 3+4 > 9, 5 == 7-3]
False
λ> product [2,5,3]
30
λ> product []
1
λ> 14/3
4.666666666666667
λ> quot 14 3
4
λ> quot (-14) 3
-4
λ> quotRem 14 3
(4,2)
λ> (read "325") :: Integer
325
λ> (read "325") :: Float
325.0
λ> 4 + read "325"
329
λ> rem 8 3
2
λ> 8 `rem` 3
2
λ> (-8) `rem` 3
-2
λ> 8 `rem` (-3)
2
λ> (-8) `rem` (-3)
-2
λ> take 5 (repeat 2)
[2,2,2,2,2]
λ> take 5 (repeat 'a')
"aaaaa"
λ> replicate 5 2
[2,2,2,2,2]
λ> replicate 5 'a'
"aaaaa"
λ> reverse [3,5,2,4]
[4,2,5,3]
λ> round 4.7
5
λ> round 4.2
4
λ> round 4.5
4
λ> round (-4.7)
-5
λ> round (-4.2)
-4
λ> round (-4.5)
-4
λ> scanl (-) 20 [2,5,3]
[20,18,13,10]
λ> [20,20-2,(20-2)-5,((20-2)-5)-3]
[20,18,13,10]
λ> scanl1 (-) [2,5,3]
[2,-3,-6]
λ> [2,2-5,(2-5)-3]
[2,-3,-6]
λ> scanr (-) 20 [2,5,4]
[-19,21,-16,20]
λ> [2-(5-(4-20)),5-(4-20),4-20,20]
[-19,21,-16,20]
λ> show 325
"325"
λ> signum 32
1
λ> signum 0
0
λ> signum (-7)
-1
λ> snd (3,5)
5
λ> splitAt 2 [5,7,9,1,4]
([5,7],[9,1,4])
λ> splitAt 8 [5,7,9,1,4]
([5,7,9,1,4],[])
λ> splitAt (-2) [5,7,9,1,4]
([],[5,7,9,1,4])
λ> sqrt 16
4.0
λ> sum [3,2,5]
10
λ> sum []
0
λ> tail [3,2,5]
[2,5]
λ> tail [3]
[]
λ> take 2 [5,7,9,6]
[5,7]
λ> take 5 [1..]
[1,2,3,4,5]
λ> take 5 [1,3..]
[1,3,5,7,9]
λ> takeWhile even [4,8,6,5,0,2,7]
[4,8,6]
λ> takeWhile (<7) [4,3,9,1,0,2,7]
[4,3]
λ> truncate 34.2
34
λ> truncate 34.7
34
λ> truncate (-34.2)
-34
λ> truncate (-34.7)
-34
λ> let f x y = x+y
λ> f 2 3
5
λ> (uncurry f) (2,3)
5
λ> until (>1000) (*2) 1
1024
λ> zip [3,5,2] [4,7]
[(3,4),(5,7)]
λ> zip [3,5] [4,7,2]
[(3,4),(5,7)]
λ> zip [3,5] "abc"
[(3,'a'),(5,'b')]
λ> zip3 [3,5,7,9] [2,4,6] [1,0,6,8]
[(3,2,1),(5,4,0),(7,6,6)]
λ> zip3 [3,5,7,9] [2,4] [1,0,6,8]
[(3,2,1),(5,4,0)]
λ> zipWith (+) [3,5,2] [4,1]
[7,6]

2 Librerías de Haskell

2.1 Listas (Data.List)

(\\), delete, find, foldl', genericLength, group, inits, intersect, isInfixOf, isPrefixOf, isSuffixOf, nub, partition, permutations, sort, span, subsequences, tails, transpose, union

λ> [3,2,5,7] \\ [5,6,3]
[2,7]
λ> delete 3 [5,3,7,3,4]
[5,7,3,4]
λ> delete 9 [5,3,7,3,4]
[5,3,7,3,4]
λ> find even [7,2,5,6]
Just 2
λ> find even [7,1,5,3]
Nothing
λ> :set +s
λ> foldl (+) 0 [1..1000000]
500000500000
(2.86 secs, 141364260 bytes)
λ> foldl' (+) 0 [1..1000000]
500000500000
(0.64 secs, 105017508 bytes)
λ> :unset +s
λ> genericLength [4,2,5]
3
λ> length [4,2,5]
3
λ> :type length [4,2,5]
length [4,2,5] :: Int
λ> :type genericLength [4,2,5]
genericLength [4,2,5] :: Num i => i
λ> genericLength [1..9] / 2
4.5
λ> length [1..9] / 2

<interactive>:14:15:
    No instance for (Fractional Int) arising from a use of `/'
    Possible fix: add an instance declaration for (Fractional Int)
    In the expression: length [1 .. 9] / 2
    In an equation for `it': it = length [1 .. 9] / 2
λ> group [4,4,7,7,7,5,4,4,4]
[[4,4],[7,7,7],[5],[4,4,4]]
λ> inits [3,2,5]
[[],[3],[3,2],[3,2,5]]
λ> intersect [3,2,5,4] [4,7,2,6]
[2,4]
λ> intersect [3,2,5,4] [8,7,1,6]
[]
λ> isInfixOf [3,2,5] [3,2,5,7,6]
True
λ> isInfixOf [2,5,7] [3,2,5,7,6]
True
λ> isInfixOf [7,6] [3,2,5,7,6]
True
λ> isInfixOf [3,5] [3,2,5,7,6]
False
λ> isPrefixOf [3,2] [3,2,5,4]
True
λ> isPrefixOf [2,5] [3,2,5,4]
False
λ> isSuffixOf [3,2] [4,5,3,2]
True
λ> isSuffixOf [5,3] [4,5,3,2]
False
λ> nub [3,2,5,2,2,7,5]
[3,2,5,7]
λ> partition even [3,2,4,7,9,6,8]
([2,4,6,8],[3,7,9])
λ> partition even [3,1,5]
([],[3,1,5])
λ> partition odd [3,1,5]
([3,1,5],[])
λ> permutations [3,2,5]
[[3,2,5],[2,3,5],[5,2,3],[2,5,3],[5,3,2],[3,5,2]]
λ> permutations "abc"
["abc","bac","cba","bca","cab","acb"]
λ> span even [2,4,5,6,7,8]
([2,4],[5,6,7,8])
λ> span even [3,2,4,5,6,7,8]
([],[3,2,4,5,6,7,8])
λ> span even [2,4,6,8]
([2,4,6,8],[])
λ> sort [3,2,5,1,7]
[1,2,3,5,7]
λ> sort "Soria"
"Saior"
λ> sort ["en","todo","la","medida"]
["en","la","medida","todo"]
λ> subsequences [3,2,5]
[[],[3],[2],[3,2],[5],[3,5],[2,5],[3,2,5]]
λ> subsequences "abc"
["","a","b","ab","c","ac","bc","abc"]
λ> tails [3,2,5]
[[3,2,5],[2,5],[5],[]]
λ> transpose [[3,2,5],[4,1,6]]
[[3,4],[2,1],[5,6]]
λ> transpose [[3,4],[2,1],[5,6]]
[[3,2,5],[4,1,6]]
λ> union [3,2,5,4] [4,7,2,6]
[3,2,5,4,7,6]

2.2 Caracteres y cadenas (Data.Char)

chr, digitToInt, isAlpha, isAlphaNum, isDigit, isLower, isSpace, isUpper, ord, toLower, toUpper

λ> chr 97
'a'
λ> chr 98
'b'
λ> digitToInt '3'
3
λ> isSpace ' '
True
λ> isSpace 'a'
False
λ> isUpper 'B'
True
λ> isUpper 'b'
False
λ> isLower 'b'
True
λ> isLower 'B'
False
λ> isAlpha 'b'
True
λ> isAlpha 'B'
True
λ> isAlpha '7'
False
λ> isDigit '7'
True
λ> isDigit 'b'
False
λ> isAlphaNum 'b'
True
λ> isAlphaNum 'B'
True
λ> isAlphaNum '7'
True
λ> isAlphaNum ' '
False
λ> ord 'a'
97
λ> ord 'b'
98
λ> toLower 'B'
'b'
λ> toLower '3'
'3'
λ> toUpper 'b'
'B'
λ> toUpper '3'
'3'

2.3 Índices (Data.Ix)

inRange, index, range, rangeSize

λ> range (3,9)
[3,4,5,6,7,8,9]
λ> range ((1,1),(2,3))
[(1,1),(1,2),(1,3),(2,1),(2,2),(2,3)]
λ> index (3,9) 7
4
λ> index ((1,1),(2,3)) (2,2)
4
λ> inRange (3,7) 5
True
λ> inRange (3,7) 9
False
λ> inRange ((1,2),(5,9)) (3,6)
True
λ> inRange ((1,2),(5,9)) (0,6)
False
λ> rangeSize (3,7)
5
λ> rangeSize ((1,2),(5,9))
40

2.4 Vectores y matrices (Data.Array)

(!), (//), accumArray, array, assocs, bounds, elems, indices, listArray

λ> array (1,5) [(i,2*i) | i <- [1..5]]
array (1,5) [(1,2),(2,4),(3,6),(4,8),(5,10)]
λ> array ((1,1),(2,3)) [((i,j),10*i+j) | i <- [1..2], j <- [1..3]]
array ((1,1),(2,3)) [((1,1),11),((1,2),12),((1,3),13),
                     ((2,1),21),((2,2),22),((2,3),23)]
λ> array (1,3) [(2,5),(1,7),(3,8)]
array (1,3) [(1,7),(2,5),(3,8)]
λ> array (1,5) (zip [1..] [7,1,4,2,6])
array (1,5) [(1,7),(2,1),(3,4),(4,2),(5,6)]
λ> listArray ((1,1),(2,3)) [1..6]
array ((1,1),(2,3)) [((1,1),1),((1,2),2),((1,3),3),
                     ((2,1),4),((2,2),5),((2,3),6)]
λ> listArray ((1,1),(3,2)) [1..6]
array ((1,1),(3,2)) [((1,1),1),((1,2),2),
                     ((2,1),3),((2,2),4),
                     ((3,1),5),((3,2),6)]
λ> (listArray ((1,1),(2,3)) [1..6]) ! (2,1)
4
λ> (listArray ((1,1),(3,2)) [1..6]) ! (2,1)
3
λ> bounds (listArray ((1,1),(3,2)) [1..6])
((1,1),(3,2))
λ> indices (listArray ((1,1),(3,2)) [1..6])
[(1,1),(1,2),(2,1),(2,2),(3,1),(3,2)]
λ> elems (listArray ((1,1),(3,2)) [1..6])
[1,2,3,4,5,6]
λ> assocs (listArray ((1,1),(3,2)) [1..6])
[((1,1),1),((1,2),2),((2,1),3),((2,2),4),((3,1),5),((3,2),6)]
λ> (listArray ((1,1),(3,2)) [1..6]) // [((2,1),9),((1,2),8)]
array ((1,1),(3,2)) [((1,1),1),((1,2),8),
                     ((2,1),9),((2,2),4),
                     ((3,1),5),((3,2),6)]
λ> accumArray (+) 0 (1,3) [(1,4),(2,5),(1,2)]
array (1,3) [(1,6),(2,5),(3,0)]
λ> accumArray (*) 1 (1,3) [(1,4),(2,5),(1,2)]
array (1,3) [(1,8),(2,5),(3,1)]