haskell 6 module Reference Link Data.List Data.Char Data.Map Data.Set Making our own modules

Taking Notes from http://learnyouahaskell.com

1
2
3
4
import Data.List
:: (Eq a) => [a] -> Int
= length . nub
1
ghci > :m Data.List Data.Map Data.Set
1
2
3
4
5
6
7
8
9
10
11
// Explicit Include
import Data.List (nubsort)
// Exclude
import Data.List hiding (nub)
// Qualified: must be used as Data.Map.filter
import qualified Data.Map
// Alias: will be: M.filter
import qualified Data.Map as M

Reference Link

Hoogle

Data.List

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
ghci> intersperse '.' "MONKEY"
"M.O.N.K.E.Y"
# intercalate
ghci> intercalate " " ["hey","there","guys"]
"hey there guys"
# transpose
ghci> transpose [[1,2,3],[4,5,6],[7,8,9]]
[[1,4,7],[2,5,8],[3,6,9]]
# concat
ghci> concat [[3,4,5],[2,3,4],[2,1,1]]
[3,4,5,2,3,4,2,1,1]
# concatMap
ghci> concatMap (replicate 4) [1..3]
[1,1,1,1,2,2,2,2,3,3,3,3]
# and
ghci> and $ map (>4) [5,6,7,8]
True
# or
ghci> or $ map (==4) [2,3,4,5,6,1]
True
# any and all
# iterate
ghci> take 10 $ iterate (*2) 1
[1,2,4,8,16,32,64,128,256,512]
# splitAt
ghci> splitAt 3 "heyman"
("hey","man")
# takeWhile
# dropWhile
# span
ghci> let (fw,rest) = span (/=' ') "This is a sentence" in "First word:" ++ fw ++ ",the rest:" ++ rest
"First word: This,the rest: is a sentence"
# break
ghci> break (==4) [1,2,3,4,5,6,7]
([1,2,3],[4,5,6,7])
ghci> span (/=4) [1,2,3,4,5,6,7]
([1,2,3],[4,5,6,7])
# sort
ghci> sort [8,5,3,2,1,6,4,2]
[1,2,2,3,4,5,6,8]
# group
ghci> group [1,1,1,1,2,2,2,2,3,3,2,2,2,5,6,7]
[[1,1,1,1],[2,2,2,2],[3,3],[2,2,2],[5],[6],[7]]
# inits inits
ghci> inits "w00t"
["","w","w0","w00","w00t"]
ghci> tails "w00t"
["w00t","00t","0t","t",""]
# isIndexOf isInfixOf isPrefixOf isSuffixOf
partition
elem
notElem
elemIndex
elemIndices
find
findIndex
findIndices
lines
unlines
words
unwords
nub
delete
\
ghci> [1..10] \ [2,5,9]
[1,3,4,6,7,8,10]
union
intersection
insert
ghci> insert 4 [1,2,3,5,6,7]
[1,2,3,4,5,6,7]
ghci> let values = [-4.3,-2.4,-1.2,0.4,2.3,5.9,10.5,29.1,5.3,-2.4,-14.5,2.9,2.3]
ghci> groupBy (x y -> (x > 0) == (y > 0)) values
[[-4.3,-2.4,-1.2],[0.4,2.3,5.9,10.5,29.1,5.3],[-2.4,-14.5],[2.9,2.3]]
on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
f `on` g = x y -> f (g x) (g y)
ghci> groupBy ((==) `on` (> 0)) values
[[-4.3,-2.4,-1.2],[0.4,2.3,5.9,10.5,29.1,5.3],[-2.4,-14.5],[2.9,2.3]]

Data.Char

isControl checks whether a character is a control character.
isSpace checks whether a character is a white-space characters. That includes spaces, tab characters, newlines, etc.
isLower checks whether a character is lower-cased.
isUpper checks whether a character is upper-cased.
isAlpha checks whether a character is a letter.
isAlphaNum checks whether a character is a letter or a number.
isPrint checks whether a character is printable. Control characters, for instance, are not printable.
isDigit checks whether a character is a digit.
isOctDigit checks whether a character is an octal digit.
isHexDigit checks whether a character is a hex digit.
isLetter checks whether a character is a letter.
isMark checks for Unicode mark characters. Those are characters that combine with preceding letters to form latters with accents. Use this if you are French.
isNumber checks whether a character is numeric.
isPunctuation checks whether a character is punctuation.
isSymbol checks whether a character is a fancy mathematical or currency symbol.
isSeparator checks for Unicode spaces and separators.
isAscii checks whether a character falls into the first 128 characters of the Unicode character set.
isLatin1 checks whether a character falls into the first 256 characters of Unicode.
isAsciiUpper checks whether a character is ASCII and upper-case.
isAsciiLower checks whether a character is ASCII and lower-case.

Data.Map

1
2
3
4
5
6
phoneBook = [("betty","555-2938") ,
("bonnie","452-2928") ,
("patsy","493-2928") ,
("lucille","205-2928") ,
("wendy","939-8282") ,
("penny","853-2492") ]

Data.Set

1
2
3
4
5
6
7
8
import qualified Data.Set as Set
ghci> let set1 = Set.fromList text1
ghci> let set2 = Set.fromList text2
ghci> set1
fromList " .?AIRadefhijlmnorstuy"
ghci> set2
fromList " !Tabcdefghilmnorstuvwy"

intersection
difference
null,size,member,empty,singleton,insert,delete

1
2
3
4
5
6
7
8
ghci> Set.fromList [2,3,4] `Set.isSubsetOf` Set.fromList [1,2,3,4,5]
True
ghci> Set.fromList [1,2,3,4,5] `Set.isSubsetOf` Set.fromList [1,2,3,4,5]
True
ghci> Set.fromList [1,2,3,4,5] `Set.isProperSubsetOf` Set.fromList [1,2,3,4,5]
False
ghci> Set.fromList [2,3,4,8] `Set.isSubsetOf` Set.fromList [1,2,3,4,5]
False
1
2
3
4
ghci> Set.filter odd $ Set.fromList [3,4,5,6,7,2,3,4]
fromList [3,5,7]
ghci> Set.map (+1) $ Set.fromList [3,4,5,6,7,2,3,4]
fromList [3,4,5,6,7,8]

Making our own modules

Geometry
|———— Sphere.hs
|———— Cuboid.hs
`———— Cube.hs

1
2
3
4
5
6
7
8
9
10
11
module Geometry.Sphere
( volume
area
) where
volume :: Float -> Float
volume radius = (4.0 / 3.0) * pi * (radius ^ 3)
area :: Float -> Float
area radius = 4 * pi * (radius ^ 2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# cuboid.hs
module Geometry.Cuboid
( volume
area
) where
volume :: Float -> Float -> Float -> Float
volume a b c = rectangleArea a b * c
area :: Float -> Float -> Float -> Float
area a b c = rectangleArea a b * 2 + rectangleArea a c * 2 + rectangleArea c b * 2
# won't export this. internal use
rectangleArea :: Float -> Float -> Float
rectangleArea a b = a * b
1
2
3
4
5
6
7
8
9
10
11
12
13
# cube.hs
module Geometry.Cube
( volume
area
) where
import qualified Geometry.Cuboid as Cuboid
volume :: Float -> Float
volume side = Cuboid.volume side side side
area :: Float -> Float
area side = Cuboid.area side side side