Browse Source

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

master
Piotr Orzechowski 4 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 @@
-- file: ch01/ex03.hs

main = interact wordCount
where wordCount input = show (length (words input)) ++ "\n"
where
wordCount input = show (length (words input)) ++ "\n"

+ 2
- 1
ch01/ex04.hs View File

@@ -1,4 +1,5 @@
-- file: ch01/ex04.hs

main = interact wordCount
where wordCount input = show (length (concat (words input))) ++ "\n"
where
wordCount input = show (length (concat (words input))) ++ "\n"

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

@@ -2,5 +2,6 @@

length' [] = 0
length' (x:xs) = len xs 1
where len [] l = l
len (x:xs) l = len xs (l + 1)
where
len [] l = l
len (x:xs) l = len xs (l + 1)

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

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

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

@@ -4,8 +4,9 @@
mean :: Fractional a => [a] -> Maybe a
mean [] = Nothing
mean (x:xs) = Just (mean' x xs 1)
where mean' m [] l = m / l
mean' m (x:xs) l = mean' (m + x) xs (l + 1)
where
mean' m [] l = m / l
mean' m (x:xs) l = mean' (m + x) xs (l + 1)

-- I'll let libraries do it for me:
--mean :: Fractional a => [a] -> Maybe a

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

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

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

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

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

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

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

@@ -0,0 +1,11 @@
-- file: ch03/02ex09.hs

type Point
= (Double, Double)


data Direction
= L -- Left
| R -- Right
| S -- Straight
deriving (Show)

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

@@ -0,0 +1,20 @@
-- file: ch03/02ex10.hs

type Point
= (Double, Double)


data Direction
= L -- Left
| R -- Right
| S -- Straight
deriving (Show)


turn :: Point -> Point -> Point -> Direction
turn (x1, y1) (x2, y2) (x3, y3)
| crossProd > 0 = Main.Left
| crossProd < 0 = Main.Right
| otherwise = Main.Straight
where
crossProd = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)

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

@@ -0,0 +1,36 @@
-- file: ch03/02ex11.hs

type Point
= (Double, Double)


data Direction
= L -- Left
| R -- Right
| S -- Straight
deriving (Show)


turn :: Point -> Point -> Point -> Direction
turn (x1, y1) (x2, y2) (x3, y3)
| crossProd > 0 = Main.Left
| crossProd < 0 = Main.Right
| otherwise = Main.Straight
where
crossProd = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)


turns :: [Point] -> [Direction]
turns points =
case points of
[] ->
[]
(p:[]) ->
[]
(p1:p2:[]) ->
[]
(p1:p2:p3:ps) ->
turn p1 p2 p3 : turns (p2:p3:ps)

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

@@ -0,0 +1,96 @@
-- file: ch03/02ex12.hs

import Data.List


type Point
= (Double, Double)


data Direction
= L -- Left
| R -- Right
| S -- Straight
deriving (Eq, Show)


turn :: Point -> Point -> Point -> Direction
turn (x1, y1) (x2, y2) (x3, y3)
| crossProduct > 0 = L
| crossProduct < 0 = R
| otherwise = S
where
crossProduct = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)


turns :: [Point] -> [Direction]
turns points =
case points of
[] ->
[]
(_:[]) ->
[]
(_:_:[]) ->
[]
(p1:p2:p3:ps) ->
turn p1 p2 p3 : turns (p2:p3:ps)


sortByCoord :: [Point] -> [Point]
sortByCoord points = sortBy (sort) points
where
sort a b
| snd a > snd b = GT
| snd a < snd b = LT
| otherwise = fst a `compare` fst b


sortByAngle :: [Point] -> [Point]
sortByAngle points =
case points of
[] ->
[]
(p:pts) ->
p : sortBy (sort) pts
where
sort a b
| tanA < tanB = LT
| tanA > tanB = GT
| distanceA < distanceB = LT
| distanceA > distanceB = GT
| otherwise = EQ
where
tanA = atan2 (snd a - snd p) (fst a - fst p)
tanB = atan2 (snd b - snd p) (fst b - fst p)
distanceA = distance p a
distanceB = distance p b
distance a b = sqrt (((fst b - fst a) ^ 2) + ((snd b - snd a) ^ 2))


grahamScan :: [Point] -> [Point]
grahamScan points =
case points of
[] ->
[]
(_:[]) ->
[]
(_:_:[]) ->
[]
_ ->
scan (drop 2 pts) (reverse (take 2 pts))
where
scan [] res = reverse res
scan (p:ps) (r1:r2:res)
| turn r2 r1 p == L = scan ps (p:r1:r2:res)
| null res = scan ps (r1:r2:res)
| otherwise = scan (p:ps) (r2:res)
pts = sortByAngle (sortByCoord points)

Loading…
Cancel
Save