Skip to content

Commit 108fb52

Browse files
committed
Modules finished.
1 parent 29bb870 commit 108fb52

File tree

7 files changed

+222
-0
lines changed

7 files changed

+222
-0
lines changed

Geometry/Cube.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Geometry.Cube
2+
( volume
3+
, area
4+
) where
5+
6+
import qualified Geometry.Cuboid as Cuboid
7+
8+
volume :: Float -> Float
9+
volume side = Cuboid.volume side side side
10+
11+
area :: Float -> Float
12+
area side = Cuboid.area side side side

Geometry/Cuboid.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Geometry.Cuboid
2+
( volume
3+
, area
4+
) where
5+
6+
volume :: Float -> Float -> Float -> Float
7+
volume a b c = rectangleArea a b * c
8+
9+
area :: Float -> Float -> Float -> Float
10+
area a b c = rectangleArea a b * 2 + rectangleArea a c * 2 + rectangleArea c b * 2
11+
12+
rectangleArea :: Float -> Float -> Float
13+
rectangleArea a b = a * b

Geometry/Sphere.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Geometry.Sphere
2+
( volume
3+
, area
4+
) where
5+
6+
volume :: Float -> Float
7+
volume radius = (4.0 / 3.0) * pi * (radius ^ 3)
8+
9+
area :: Float -> Float
10+
area radius = 4 * pi * (radius ^ 2)

module-char.hs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Data.List (all, groupBy)
2+
import Data.Function (on)
3+
import Data.Char
4+
5+
pwd1 = all isAlphaNum "gabi123"
6+
pwd2 = all isAlphaNum "gabi.volpe"
7+
8+
-- simulate words function of Data.List
9+
wordsSim = filter (not . any isSpace) . groupBy ((==) `on` isSpace) $ "hey guys its me"
10+
11+
a1 = generalCategory ' '
12+
a2 = generalCategory 'A'
13+
a3 = generalCategory 'a'
14+
a4 = generalCategory '.'
15+
a5 = generalCategory '5'
16+
a6 = map generalCategory " \t\nA9?|"
17+
18+
b1 = map digitToInt "34538"
19+
b2 = map digitToInt "FF85AB"
20+
21+
c1 = intToDigit 15
22+
c2 = intToDigit 5
23+
24+
d1 = ord 'a'
25+
d2 = chr 97
26+
d3 = map ord "abcdefgh"
27+
28+
-- kinda Caesar cipher
29+
encode :: Int -> String -> String
30+
encode shift msg =
31+
let ords = map ord msg
32+
shifted = map (+ shift) ords
33+
in map chr shifted
34+
35+
encodeCompose :: Int -> String -> String
36+
encodeCompose shift msg = map (chr . (+ shift) . ord) msg
37+
38+
decode :: Int -> String -> String
39+
decode shift msg = encode (negate shift) msg

module-map.hs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import qualified Data.Map as Map
2+
import Data.Char (isUpper)
3+
4+
phoneBook =
5+
[("betty","555-2938")
6+
,("bonnie","452-2928")
7+
,("patsy","493-2928")
8+
,("lucille","205-2928")
9+
,("wendy","939-8282")
10+
,("penny","853-2492")
11+
]
12+
13+
-- all the findKey functions could be replaced by Data.List (lookup)
14+
unsafeFindKey :: (Eq k) => k -> [(k,v)] -> v
15+
unsafeFindKey key xs = snd . head . filter (\(k,v) -> key == k) $ xs
16+
17+
findKeyRecursive :: (Eq k) => k -> [(k,v)] -> Maybe v
18+
findKeyRecursive key [] = Nothing
19+
findKeyRecursive key ((k,v):xs) = if key == k
20+
then Just v
21+
else findKeyRecursive key xs
22+
23+
findKey :: (Eq k) => k -> [(k,v)] -> Maybe v
24+
findKey key = foldr (\(k,v) acc -> if key == k then Just v else acc) Nothing
25+
26+
-- functions from Data.Map that provide association lists
27+
28+
phones = Map.fromList phoneBook
29+
30+
-- duplicated keys are discarded
31+
noDup = Map.fromList [(1,2),(3,4),(3,2),(5,5),(1,3)]
32+
33+
-- create maps
34+
a1 = Map.insert 1 5 Map.empty
35+
a2 = Map.insert 5 600 . Map.insert 4 200 . Map.insert 3 100 $ Map.empty
36+
37+
-- own implementation using foldr
38+
fromList' :: (Ord k) => [(k,v)] -> Map.Map k v
39+
fromList' = foldr (\(k,v) acc -> Map.insert k v acc) Map.empty
40+
41+
-- is map empty?
42+
b1 = Map.null Map.empty
43+
b2 = Map.null $ Map.fromList [(2,3),(5,5)]
44+
45+
--- map size?
46+
c1 = Map.size Map.empty
47+
c2 = Map.size $ Map.fromList [(1,4),(3,2)]
48+
49+
-- one element map
50+
d1 = Map.singleton 3 9
51+
d2 = Map.insert 5 9 $ Map.singleton 3 9
52+
53+
-- lookup on map
54+
e1 = Map.lookup 3 d2
55+
e2 = Map.lookup 1 d2
56+
57+
-- key exists?
58+
f1 = Map.member 3 $ Map.fromList [(3,6),(4,3),(6,9)]
59+
f2 = Map.member 3 $ Map.fromList [(2,5),(4,5)]
60+
61+
-- map and filter
62+
g1 = Map.map (*100) $ Map.fromList [(1,1),(2,4),(3,9)]
63+
g2 = Map.filter isUpper $ Map.fromList [(1,'a'),(2,'A'),(3,'b'),(4,'B')]
64+
65+
-- toList from map
66+
h1 = Map.toList d2
67+
68+
-- keys and elems
69+
i1 = Map.keys d2
70+
i2 = Map.elems d2
71+
72+
-- own implentation of keys and elems
73+
keys' :: Map.Map k v -> [k]
74+
keys' m = map fst . Map.toList $ m
75+
76+
elems' :: Map.Map k v -> [v]
77+
elems' m = map snd . Map.toList $ m
78+
79+
--- repeated keys with different values using fromListWith
80+
81+
phoneBook2 =
82+
[("betty","555-2938")
83+
,("betty","342-2492")
84+
,("bonnie","452-2928")
85+
,("patsy","493-2928")
86+
,("patsy","943-2929")
87+
,("patsy","827-9162")
88+
,("lucille","205-2928")
89+
,("wendy","939-8282")
90+
,("penny","853-2492")
91+
,("penny","555-2111")
92+
]
93+
94+
phoneBookToMap :: (Ord k) => [(k, String)] -> Map.Map k String
95+
phoneBookToMap xs = Map.fromListWith (\number1 number2 -> number1 ++ ", " ++ number2) xs
96+
97+
patsyNumbers = Map.lookup "patsy" $ phoneBookToMap phoneBook2
98+
99+
-- when duplicated values, keep the maximum
100+
keepMaxValue = Map.fromListWith max [(2,3),(2,5),(2,100),(3,29),(3,22),(3,11),(4,22),(4,15)]
101+
102+
-- or sum the duplicated values
103+
sumDupValues = Map.fromListWith (+) [(2,3),(2,5),(2,100),(3,29),(3,22),(3,11),(4,22),(4,15)]
104+
105+
sumDupValuesOnInsert = Map.insertWith (+) 3 100 $ Map.fromList [(3,4),(5,103),(6,339)]

module-set.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import qualified Data.Set as Set
2+
import Data.List (nub)
3+
4+
text1 = "I just had an anime dream. Anime... Reality... Are they so different?"
5+
text2 = "The old man left his garbage can out and now his trash is all over my lawn!"
6+
7+
set1 = Set.fromList text1
8+
set2 = Set.fromList text2
9+
10+
inter = Set.intersection set1 set2
11+
diff = Set.difference set1 set2
12+
union = Set.union set1 set2
13+
14+
-- very similar to map
15+
a1 = Set.null Set.empty
16+
a2 = Set.null $ Set.fromList [3,4,5,5,4,3]
17+
a3 = Set.size $ Set.fromList [3,4,5,3,4,5]
18+
a4 = Set.singleton 9
19+
a5 = Set.insert 4 $ Set.fromList [9,3,8,1]
20+
a6 = Set.insert 8 $ Set.fromList [5..10]
21+
a7 = Set.delete 4 $ Set.fromList [3,4,5,4,3,4,5]
22+
23+
-- subset
24+
b1 = Set.fromList [2,3,4] `Set.isSubsetOf` Set.fromList [1,2,3,4,5]
25+
b2 = Set.fromList [1,2,3,4,5] `Set.isSubsetOf` Set.fromList [1,2,3,4,5]
26+
b3 = Set.fromList [1,2,3,4,5] `Set.isProperSubsetOf` Set.fromList [1,2,3,4,5]
27+
b4 = Set.fromList [2,3,4,8] `Set.isSubsetOf` Set.fromList [1,2,3,4,5]
28+
29+
-- map and filter
30+
c1 = Set.filter odd $ Set.fromList [3,4,5,6,7,2,3,4]
31+
c2 = Set.map (+1) $ Set.fromList [3,4,5,6,7,2,3,4]
32+
33+
-- duplicates
34+
setNub xs = Set.toList $ Set.fromList xs
35+
d1 = setNub "HEY WHATS CRACKALACKIN"
36+
d2 = nub "HEY WHATS CRACKALACKIN"

own-modules.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import qualified Geometry.Sphere as Sphere
2+
import qualified Geometry.Cuboid as Cuboid
3+
import qualified Geometry.Cube as Cube
4+
5+
a = Sphere.volume 2.1
6+
b = Cuboid.area 1.2 3.2 4.8
7+
c = Cube.area 3

0 commit comments

Comments
 (0)