Browse Source

Add ch03/02ex09 ch03/02ex10 ch03/02ex11 and ch03/02ex12; fix code formatting

Piotr Orzechowski 3 years ago
parent
commit
8bad512413
12 changed files with 189 additions and 18 deletions
  1. 2
    1
      ch01/ex03.hs
  2. 2
    1
      ch01/ex04.hs
  3. 3
    2
      ch03/02ex01.hs
  4. 3
    2
      ch03/02ex02.hs
  5. 3
    2
      ch03/02ex03.hs
  6. 4
    3
      ch03/02ex05.hs
  7. 8
    6
      ch03/02ex06.hs
  8. 1
    1
      ch03/02ex07.hs
  9. 11
    0
      ch03/02ex09.hs
  10. 20
    0
      ch03/02ex10.hs
  11. 36
    0
      ch03/02ex11.hs
  12. 96
    0
      ch03/02ex12.hs

+ 2
- 1
ch01/ex03.hs View File

@@ -1,4 +1,5 @@
1 1
 -- file: ch01/ex03.hs
2 2
 
3 3
 main = interact wordCount
4
-    where wordCount input = show (length (words input)) ++ "\n"
4
+    where
5
+        wordCount input = show (length (words input)) ++ "\n"

+ 2
- 1
ch01/ex04.hs View File

@@ -1,4 +1,5 @@
1 1
 -- file: ch01/ex04.hs
2 2
 
3 3
 main = interact wordCount
4
-    where wordCount input = show (length (concat (words input))) ++ "\n"
4
+    where
5
+        wordCount input = show (length (concat (words input))) ++ "\n"

+ 3
- 2
ch03/02ex01.hs View File

@@ -2,5 +2,6 @@
2 2
 
3 3
 length' []     = 0
4 4
 length' (x:xs) = len xs 1
5
-    where len [] l     = l
6
-          len (x:xs) l = len xs (l + 1)
5
+    where
6
+        len [] l     = l
7
+        len (x:xs) l = len xs (l + 1)

+ 3
- 2
ch03/02ex02.hs View File

@@ -3,5 +3,6 @@
3 3
 length' :: [a] -> Int
4 4
 length' []     = 0
5 5
 length' (x:xs) = len xs 1
6
-    where len [] l     = l
7
-          len (x:xs) l = len xs (l + 1)
6
+    where
7
+        len [] l     = l
8
+        len (x:xs) l = len xs (l + 1)

+ 3
- 2
ch03/02ex03.hs View File

@@ -4,8 +4,9 @@
4 4
 mean :: Fractional a => [a] -> Maybe a
5 5
 mean []     = Nothing
6 6
 mean (x:xs) = Just (mean' x xs 1)
7
-    where mean' m [] l     = m / l
8
-          mean' m (x:xs) l = mean' (m + x) xs (l + 1)
7
+    where
8
+        mean' m [] l     = m / l
9
+        mean' m (x:xs) l = mean' (m + x) xs (l + 1)
9 10
 
10 11
 -- I'll let libraries do it for me: 
11 12
 --mean :: Fractional a => [a] -> Maybe a

+ 4
- 3
ch03/02ex05.hs View File

@@ -3,6 +3,7 @@
3 3
 isPalindrome :: Eq a => [a] -> Bool
4 4
 isPalindrome [] = True
5 5
 isPalindrome xs = take (half + rest) xs == (reverse $ drop half xs)
6
-    where half = len `div` 2
7
-          rest = len `mod` 2
8
-          len  = length xs
6
+    where
7
+        half = len `div` 2
8
+        rest = len `mod` 2
9
+        len  = length xs

+ 8
- 6
ch03/02ex06.hs View File

@@ -5,9 +5,11 @@ import Data.List (sortBy)
5 5
 -- ascending order
6 6
 sortSubs :: [[a]] -> [[a]]
7 7
 sortSubs xs = sortBy len xs
8
-    where len as bs
9
-              | lenAs > lenBs = GT
10
-              | lenAs < lenBs = LT
11
-              | otherwise     = EQ
12
-              where lenAs = length as
13
-                    lenBs = length bs
8
+    where 
9
+        len as bs
10
+            | lenAs > lenBs = GT
11
+            | lenAs < lenBs = LT
12
+            | otherwise     = EQ
13
+            where
14
+                lenAs = length as
15
+                lenBs = length bs

+ 1
- 1
ch03/02ex07.hs View File

@@ -3,4 +3,4 @@
3 3
 intersperse :: a -> [[a]] -> [a]
4 4
 intersperse _ []     = []
5 5
 intersperse _ (x:[]) = x
6
-intersperse c (x:xs) = x ++ c : intersperse c xs
6
+intersperse s (x:xs) = x ++ s : intersperse s xs

+ 11
- 0
ch03/02ex09.hs View File

@@ -0,0 +1,11 @@
1
+-- file: ch03/02ex09.hs
2
+
3
+type Point
4
+    = (Double, Double)
5
+
6
+
7
+data Direction
8
+    = L -- Left
9
+    | R -- Right
10
+    | S -- Straight
11
+    deriving (Show)

+ 20
- 0
ch03/02ex10.hs View File

@@ -0,0 +1,20 @@
1
+-- file: ch03/02ex10.hs
2
+
3
+type Point
4
+    = (Double, Double)
5
+
6
+
7
+data Direction
8
+    = L -- Left
9
+    | R -- Right
10
+    | S -- Straight
11
+    deriving (Show)
12
+
13
+
14
+turn :: Point -> Point -> Point -> Direction
15
+turn (x1, y1) (x2, y2) (x3, y3)
16
+    | crossProd > 0 = Main.Left
17
+    | crossProd < 0 = Main.Right
18
+    | otherwise     = Main.Straight
19
+        where
20
+            crossProd = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)

+ 36
- 0
ch03/02ex11.hs View File

@@ -0,0 +1,36 @@
1
+-- file: ch03/02ex11.hs
2
+
3
+type Point
4
+    = (Double, Double)
5
+
6
+
7
+data Direction
8
+    = L -- Left
9
+    | R -- Right
10
+    | S -- Straight
11
+    deriving (Show)
12
+
13
+
14
+turn :: Point -> Point -> Point -> Direction
15
+turn (x1, y1) (x2, y2) (x3, y3)
16
+    | crossProd > 0 = Main.Left
17
+    | crossProd < 0 = Main.Right
18
+    | otherwise     = Main.Straight
19
+        where
20
+            crossProd = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)
21
+
22
+
23
+turns :: [Point] -> [Direction]
24
+turns points =
25
+    case points of
26
+        [] ->
27
+            []
28
+        
29
+        (p:[]) ->
30
+            []
31
+        
32
+        (p1:p2:[]) ->
33
+            []
34
+        
35
+        (p1:p2:p3:ps) ->
36
+            turn p1 p2 p3 : turns (p2:p3:ps)

+ 96
- 0
ch03/02ex12.hs View File

@@ -0,0 +1,96 @@
1
+-- file: ch03/02ex12.hs
2
+
3
+import Data.List
4
+
5
+
6
+type Point
7
+    = (Double, Double)
8
+
9
+
10
+data Direction
11
+    = L -- Left
12
+    | R -- Right
13
+    | S -- Straight
14
+    deriving (Eq, Show)
15
+
16
+
17
+turn :: Point -> Point -> Point -> Direction
18
+turn (x1, y1) (x2, y2) (x3, y3)
19
+    | crossProduct > 0 = L
20
+    | crossProduct < 0 = R
21
+    | otherwise        = S
22
+    where
23
+        crossProduct = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)
24
+
25
+
26
+turns :: [Point] -> [Direction]
27
+turns points =
28
+    case points of
29
+        [] ->
30
+            []
31
+        
32
+        (_:[]) ->
33
+            []
34
+        
35
+        (_:_:[]) ->
36
+            []
37
+        
38
+        (p1:p2:p3:ps) ->
39
+            turn p1 p2 p3 : turns (p2:p3:ps)
40
+
41
+
42
+sortByCoord :: [Point] -> [Point]
43
+sortByCoord points = sortBy (sort) points
44
+    where
45
+        sort a b
46
+            | snd a > snd b = GT
47
+            | snd a < snd b = LT
48
+            | otherwise     = fst a `compare` fst b
49
+
50
+
51
+sortByAngle :: [Point] -> [Point]
52
+sortByAngle points =
53
+    case points of
54
+        [] ->
55
+            []
56
+        
57
+        (p:pts) ->
58
+            p : sortBy (sort) pts
59
+            where
60
+                sort a b
61
+                    | tanA < tanB           = LT
62
+                    | tanA > tanB           = GT
63
+                    | distanceA < distanceB = LT
64
+                    | distanceA > distanceB = GT
65
+                    | otherwise = EQ
66
+                    where
67
+                        tanA = atan2 (snd a - snd p) (fst a - fst p)
68
+                        tanB = atan2 (snd b - snd p) (fst b - fst p)
69
+                        distanceA = distance p a
70
+                        distanceB = distance p b
71
+                        distance a b = sqrt (((fst b - fst a) ^ 2) + ((snd b - snd a) ^ 2))
72
+
73
+
74
+grahamScan :: [Point] -> [Point]
75
+grahamScan points =
76
+    case points of
77
+        [] ->
78
+            []
79
+        
80
+        (_:[]) ->
81
+            []
82
+        
83
+        (_:_:[]) ->
84
+            []
85
+        
86
+        _ ->
87
+            scan (drop 2 pts) (reverse (take 2 pts))
88
+        
89
+    where
90
+        scan [] res             = reverse res
91
+        scan (p:ps) (r1:r2:res)
92
+            | turn r2 r1 p == L = scan ps (p:r1:r2:res)
93
+            | null res          = scan ps (r1:r2:res)
94
+            | otherwise         = scan (p:ps) (r2:res)
95
+        
96
+        pts = sortByAngle (sortByCoord points)

Loading…
Cancel
Save