Browse Source

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

Piotr Orzechowski 3 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 @@
1
+-- file: ch01/ex01.hs
2
+
3
+5 + 8 :: Num a => a
4
+3 * 5 + 8 :: Num a => a
5
+2 + 4 :: Num a => a
6
+(+) 2 4 :: Num a => a
7
+sqrt 16 :: Floating a => a
8
+succ 6 :: (Num a, Enum a) => a
9
+succ 7 :: (Num a, Enum a) => a
10
+pred 9 :: (Num a, Enum a) => a
11
+pred 8 :: (Num a, Enum a) => a
12
+sin (pi / 2) :: Floating a => a
13
+truncate pi :: Integral b => b
14
+round 3.5 :: Integral b => b
15
+round 3.4 :: Integral b => b
16
+floor 3.7 :: Integral b => b
17
+ceiling 3.3 :: Integral b => b

+ 5
- 0
ch01/ex03.hs View File

@@ -0,0 +1,5 @@
1
+-- file: ch01/ex02.hs
2
+-- lines beginning with "--" are comments.
3
+
4
+main = interact wordCount
5
+    where wordCount input = show (length (words input)) ++ "\n"

+ 5
- 0
ch01/ex04.hs View File

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

+ 5
- 0
ch01/quux.txt View File

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

+ 12
- 0
ch02/ex02.hs View File

@@ -0,0 +1,12 @@
1
+-- file: ch02/ex02.hs
2
+
3
+-- "naive" approach - results in "non-exhaustive patterns" exception
4
+--lastButOne :: [a] -> a
5
+--lastButOne (x:xs) = if length xs == 1 then x else lastButOne xs
6
+
7
+-- errorproof approach
8
+lastButOne :: [a] -> Maybe a
9
+lastButOne []       = Nothing
10
+lastButOne ([x])    = Nothing
11
+lastButOne (x:y:[]) = Just x
12
+lastButOne (x:xs)   = lastButOne xs

+ 13
- 0
ch03/ex01.hs View File

@@ -0,0 +1,13 @@
1
+-- file: ch03/ListADT.hs
2
+
3
+data List a = Cons a (List a)
4
+            | Nil
5
+              deriving (Show)
6
+
7
+fromList :: [a] -> List a
8
+fromList (x:xs) = Cons x (fromList xs)
9
+fromList []     = Nil
10
+
11
+toList :: List a -> [a]
12
+toList Nil        = []
13
+toList (Cons a b) = a : toList b

+ 6
- 0
ch03/ex02.hs View File

@@ -0,0 +1,6 @@
1
+-- file: ch03/ex02.hs
2
+
3
+data Tree a = Node a (Maybe (Tree a)) (Maybe (Tree a))
4
+              deriving (Show)
5
+-- Example:
6
+-- Node "parent" (Just (Node "left" Nothing Nothing)) (Just (Node "right" Nothing Nothing))

Loading…
Cancel
Save