diff --git a/exercises/Set1.hs b/exercises/Set1.hs index 7405ea1d..5c8fe787 100644 --- a/exercises/Set1.hs +++ b/exercises/Set1.hs @@ -75,7 +75,7 @@ checkPassword password = if password == "swordfish" ------------------------------------------------------------------------------ -- Ex 7: A postal service prices packages the following way. -- Packages that weigh up to 500 grams cost 250 credits. --- Packages over 500 grams cost 300 credit + 1 credit per gram. +-- Packages over 500 grams and up to 5000 grams cost 300 credit + 1 credit per gram. -- Packages over 5000 grams cost a constant 6000 credits. -- -- Write a function postagePrice that takes the weight of a package diff --git a/part1.html b/part1.html index 5f3750a3..a8958c1a 100644 --- a/part1.html +++ b/part1.html @@ -1398,7 +1398,7 @@

-

This tree then exaclty corresponds with the expression +

This tree then exactly corresponds with the expression (1 + 1) + (1 + (1 + 1)). Recursion can often produce chain-like, tree-like, nested, or loopy structures and computations. Recursion is one of the main techniques in functional programming, so @@ -1407,7 +1407,7 @@

1.10 All Together Now!

Finally, here’s a complete Haskell module that uses ifs, pattern -matching, local defintions and recursion. The module is interested in +matching, local definitions and recursion. The module is interested in the Collatz conjecture, a famous open problem in mathematics. It asks:

@@ -1550,7 +1550,7 @@

1.12 Quiz

-

At the end of each lecture you’ll find a quiz like this. The quizes +

At the end of each lecture you’ll find a quiz like this. The quizzes aren’t graded, they’re just here to help you check you’ve understood the chapter. You can check your answer by clicking on an option. You’ll see a green background if you were right, a red one if you were wrong. Feel @@ -1560,10 +1560,10 @@

combine(prettify(lawn),construct(house,concrete))?

  1. -combine prettify (lawn) construct (house concerete) +combine prettify (lawn) construct (house concrete)
  2. -combine (prettify lawn (counstruct house concrete)) +combine (prettify lawn (construct house concrete))
  3. combine (prettify lawn) (construct house concrete) @@ -1684,7 +1684,7 @@

    There are primarily two types of files in the exercises directory: exercise sets named SetNX.hs and accompanying test program for the exercises named SetNXTest.hs. Both are -Haskell source files, but only the exercise file should to be edited +Haskell source files, but only the exercise file should be edited when solving the exercises. Instructions to all individual exercises are embedded in the exercise file as comments.

    Use the tests file to check your answers. For example when you have @@ -1881,11 +1881,11 @@

    fibonacci' _ _ n calls fibonacci' _ _ (n-1) once, and this means that we can compute fibonacci' _ _ n in n steps.

    -

    This type of recursion where a function just directly calls itself -with different arguments is called tail recursion. As you’ve -seen above, tail recursion corresponds to loops. This is why tail -recursion is often fast: the compiler can generate a loop in machine -code when it sees tail recursion.

    +

    This type of recursion is called tail recursion, meaning +its recursive call occurs in tail position — that is, there is no +remaining work to perform after the recursive call. Because nothing +must be done afterward, a tail-recursive function can be optimized +into an efficient loop by the compiler in machine code.

    2.2 Guards

    Before we move on to new types, let’s go over one more piece of @@ -1986,7 +1986,7 @@

    init :: [a] -> [a] -- returns everything except the last element take :: Int -> [a] -> [a] -- returns the n first elements drop :: Int -> [a] -> [a] -- returns everything except the n first elements -(++) :: [a] -> [a] -> [a] -- lists are catenated with the ++ operator +(++) :: [a] -> [a] -> [a] -- lists are concatenated with the ++ operator (!!) :: [a] -> Int -> a -- lists are indexed with the !! operator reverse :: [a] -> [a] -- reverse a list null :: [a] -> Bool -- is this list empty? @@ -2290,6 +2290,15 @@

    +

    Sidenote: the constructors of Either are called + Left and Right because they refer to the left + and right type arguments of Either. Note how in + Either a b, a is the left argument and + b is the right argument. Thus Left contains a + value of type a and likewise Right of type + b. The convention of using Right for success + is probably simply because right also means correct. No offense is + intended to left-handed people.

    Here’s a simple example: a readInt function that only knows a couple of numbers and returns a descriptive error for the rest. Note the Haskell convention of using Left for errors and @@ -2299,15 +2308,6 @@

    readInt "0" = Right 0 readInt "1" = Right 1 readInt s = Left ("Unsupported string: " ++ s) -

    Sidenote: the constructors of Either are called -Left and Right because they refer to the left -and right type arguments of Either. Note how in -Either a b, a is the left argument and -b is the right argument. Thus Left contains a -value of type a and likewise Right of type -b. The convention of using Right for success -is probably simply because right also means correct. No offense is -intended to left-handed people.

    Here’s another example: pattern matching an Either. Just like with Maybe, there are two patterns for an Either, one for each constructor.

    @@ -2903,7 +2903,7 @@

    add above.

    Note that omitting the parentheses leads into a type error:

    Prelude> zipWith + [0,2,5,3] [1,3,3]
    +class="sourceCode haskell">Prelude> zipWith + [0,2,5] [1,3,3]
     
     <interactive>:1:11: error:
     Couldn't match expected type ‘[Integer]
    @@ -2918,8 +2918,8 @@ 

    (bound at <interactive>:1:1)

    The reason for this weird-looking error is that GHCi got confused and thought that we were somehow trying to add zipWith and -[0,2,5,3] [1,3,3] together. Logically, it deduced that -[0,2,5,3] must be a function since it’s being applied to +[0,2,5] [1,3,3] together. Logically, it deduced that +[0,2,5] must be a function since it’s being applied to [1,3,3] (remember that functions bind tighter than operators).

    Unfortunately, error messages can sometimes be obscure, since the