Safe HaskellSafe

S01

Contents

Description

Solutions to Exercises for October 12, 2018

Synopsis

Exercise 3

x :: Int Source #

The previous version was

x = mod length data Y
   where
     { data = [1..10] Y = 5 }

containing the following errors:

  1. data is a keyword and is therefore not allowed as variable name.
  2. In the last line we are using braces ({ and }) to disregard the layout rule. But then items need to be separated by semicolons (;).
  3. Y is not allowed as variable name (since it is uppercase).
  4. Missing parentheses around length data, since it is supposed to be the first argument of mod.

Exercise 4

nth :: [a] -> Int -> a Source #

nth corresponds to the Prelude function !!, that is, we have

nth xs i = xs !! i

whenever i is between 0 and the length of xs.

Exercise 5

fromTo :: [a] -> Int -> Int -> [a] Source #

Just to get some intuition: for integer lists of the shape [0..k], fromTo satisfies the property

fromTo [0..k] i j = [i..j]

for all 0 <= i <= j <= k.

Exercise 6

allTrue :: [Bool] -> Bool Source #

allTrue corresponds to the Prelude function and, that is, we have

allTrue bs = and bs

for all lists of boolean values bs.