Browse Source

Add ch01/ex01, ch01/ex03, ch01/ex04, ch02/ex02, ch03/ex01, ch03/ex02 solutions

master
Piotr Orzechowski 4 years ago
parent
commit
0db066266d
7 changed files with 63 additions and 0 deletions
  1. 17
    0
      ch01/ex01.txt
  2. 5
    0
      ch01/ex03.hs
  3. 5
    0
      ch01/ex04.hs
  4. 5
    0
      ch01/quux.txt
  5. 12
    0
      ch02/ex02.hs
  6. 13
    0
      ch03/ex01.hs
  7. 6
    0
      ch03/ex02.hs

+ 17
- 0
ch01/ex01.txt View File

@@ -0,0 +1,17 @@
-- file: ch01/ex01.hs

5 + 8 :: Num a => a
3 * 5 + 8 :: Num a => a
2 + 4 :: Num a => a
(+) 2 4 :: Num a => a
sqrt 16 :: Floating a => a
succ 6 :: (Num a, Enum a) => a
succ 7 :: (Num a, Enum a) => a
pred 9 :: (Num a, Enum a) => a
pred 8 :: (Num a, Enum a) => a
sin (pi / 2) :: Floating a => a
truncate pi :: Integral b => b
round 3.5 :: Integral b => b
round 3.4 :: Integral b => b
floor 3.7 :: Integral b => b
ceiling 3.3 :: Integral b => b

+ 5
- 0
ch01/ex03.hs View File

@@ -0,0 +1,5 @@
-- file: ch01/ex02.hs
-- lines beginning with "--" are comments.

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

+ 5
- 0
ch01/ex04.hs View File

@@ -0,0 +1,5 @@
-- file: ch01/ex03.hs
-- lines beginning with "--" are comments.

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

+ 5
- 0
ch01/quux.txt View File

@@ -0,0 +1,5 @@
Paris, France
Ulm, Germany,
Auxerre, France
Beaumont-en-Auge, France
Ryazan, Russia

+ 12
- 0
ch02/ex02.hs View File

@@ -0,0 +1,12 @@
-- file: ch02/ex02.hs

-- "naive" approach - results in "non-exhaustive patterns" exception
--lastButOne :: [a] -> a
--lastButOne (x:xs) = if length xs == 1 then x else lastButOne xs

-- errorproof approach
lastButOne :: [a] -> Maybe a
lastButOne [] = Nothing
lastButOne ([x]) = Nothing
lastButOne (x:y:[]) = Just x
lastButOne (x:xs) = lastButOne xs

+ 13
- 0
ch03/ex01.hs View File

@@ -0,0 +1,13 @@
-- file: ch03/ListADT.hs

data List a = Cons a (List a)
| Nil
deriving (Show)

fromList :: [a] -> List a
fromList (x:xs) = Cons x (fromList xs)
fromList [] = Nil

toList :: List a -> [a]
toList Nil = []
toList (Cons a b) = a : toList b

+ 6
- 0
ch03/ex02.hs View File

@@ -0,0 +1,6 @@
-- file: ch03/ex02.hs

data Tree a = Node a (Maybe (Tree a)) (Maybe (Tree a))
deriving (Show)
-- Example:
-- Node "parent" (Just (Node "left" Nothing Nothing)) (Just (Node "right" Nothing Nothing))

Loading…
Cancel
Save