Skip to content

Commit c9631d3

Browse files
committed
solutions for chapter 01
1 parent 3df3f67 commit c9631d3

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

ch01/11.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{-
2+
- Give another possible calculation for the result of double (double 2)
3+
-}
4+
5+
main = do
6+
putStrLn "double (double 2)"
7+
putStrLn "{applying the inner double}"
8+
putStrLn "double 2 + double 2"
9+
putStrLn "{applying the first double}"
10+
putStrLn "(2 + 2) + double 2"
11+
putStrLn "{applying the second double}"
12+
putStrLn "(2 + 2) + (2 + 2)"
13+
putStrLn "{applying the first +}"
14+
putStrLn "4 + (2 + 2)"
15+
putStrLn "{applying the second +}"
16+
putStrLn "4 + 4"
17+
putStrLn "{applying +}"
18+
putStrLn "8"

ch01/12.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{-
2+
- Show that sum [x] = x for any number x.
3+
-}
4+
5+
main = do
6+
putStrLn "sum [] = 0"
7+
putStrLn "sum (x:xs) = x + sum xs"
8+
putStrLn "{given sum [x], applying the second definition}"
9+
putStrLn "sum (x:[]) = x + sum []"
10+
putStrLn "{applying the first defintion}"
11+
putStrLn "sum (x:[]) = x + 0"
12+
putStrLn "{simplifying the right side}"
13+
putStrLn "sum (x:[]) = x"
14+
putStrLn "{simplifying the left side}"
15+
putStrLn "sum [x] = x"

ch01/13.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{-
2+
- Define a function product that produces the product of a list of numbers,
3+
- and show using your definition that product [2,3,4] = 24
4+
-}
5+
6+
recProduct [] = 1
7+
recProduct (x:xs) = x * product xs
8+
9+
main = print recProduct [2,3,4]

ch01/14.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{-
2+
- How should the definition of the function qsort be modified
3+
- so that it produces a sorted reverse version of a list?
4+
-}
5+
6+
rQsort [] = []
7+
rQsort (x:xs) = rQsort larger ++ [x] ++ rQsort smaller
8+
where
9+
larger = [b | b <- xs, b > x]
10+
smaller = [a | a <- xs, a <= x]

ch01/15.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{-
2+
- What would be the effect of replacing <= by < in the original
3+
- definition of qsort? Hint: consider the example qsort [2,2,3,1,1].
4+
-}
5+
6+
buggyQsort [] = []
7+
buggyQsort (x:xs) = buggyQsort smaller ++ [x] ++ buggyQsort larger
8+
where
9+
smaller = [a | a <- xs, a < x]
10+
larger = [b | b <- xs, b > x]
11+
12+
main = do
13+
putStrLn "using buggyQsort in a list with"
14+
putStrLn "duplicate values, such as [2,2,3,1,1]"
15+
putStrLn "will remove the duplicate values from"
16+
putStrLn "the result sorted list, such as [1,2,3]."

0 commit comments

Comments
 (0)