Appendix. Answers to end-of-lesson exercises

 

The beautiful thing about code is that there are no wrong answers as long as you get the right results. The answers to the exercises shown here should be viewed as simply one possible solution to the problem. Especially in Haskell, there are many paths to the correct solution; if you have an alternative answer that gives the correct results, that’s the correct solution.

Unit 1

Lesson 2

inc x = x + 1
double x = x*2
square x = x^2
ex3 n = if n 'mod' 2 == 0
        then n - 2
        else 3*n+1
ifEven n = if even n
           then n - 2
           else 3 * n + 1

Lesson 3

simple = (\x -> x)
makeChange = (\owed given ->
               if given - owed > 0
               then given - owed
               else 0)
inc = (\x -> x+1)
double = (\x -> x*2)
square = (\x -> x^2)

counter x = (\x -> x + 1)
            ((\x -> x + 1)
             ((\x -> x) x))

Lesson 4

Note—if the results are equal, you need to compare first names:

compareLastNames name1 name2 = if result == EQ
                               then compare (fst name1) (fst name2)
                               else result
  where result = compare (snd name1) (snd name2)

And the new DC office:

Unit 2

 
 

Unit 3

 
 
 

Unit 4

 
 

Unit 5

 
 
 

Unit 6

 
 

Unit 7

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage