There was no lecture or tutorial this week.
Our topics for this week include
fold functions (foldl
, foldr
,
foldl1
, foldr1
)
the $
operator
function composition
modules
To learn these features, please read the following sections of Learn You a Haskell:
6. Higher order functions: Only folds and horses, Function application with $, Function composition
7. Modules (entire chapter)
Here are a couple of more notes:
As you have probably noticed by now, when Haskell reports a type error in your program it is not always so easy to figure out what is wrong. You may also have noticed that sometimes Haskell may report a type error in one function when in reality the error in your code is in a completely different function. This is due to Haskell's extensive type inference. If you make an error when writing a function F, Haskell may infer an incorrect type for a function G that F calls. And then that may cause Haskell to infer incorrect types for functions that G calls and so on. In the end, Haskell may finally reports a type error in code that is only distantly related to the place where your original mistake occurred.
For this reason, I highly recommend that you always explicitly declare the type of every function you write in Haskell. Then Haskell will know the actual type of every function, so type errors cannot cascade between functions. Of course, even when you do explicitly declare all function types, Haskell's type errors can still sometimes be tricky to understand, but if type errors can propagate then the problem becomes significantly worse.
In chapter 7 of
Learn You A Haskell (part of this week's reading), the
section "Data.List" describes many useful functions on
lists. Probably you will just skim that section, however be
sure to read the text about
Maybe
values, since we will use them extensively in our course. Briefly, a
Maybe t
is a value that might either hold a value of type t, or it might not,
in which case its value is Nothing
.
In other words, it is an optional value of type t. (In languages
such as C# and Java we often use a null pointer to represent the
absence of a value. But Haskell is stricter; there is no such thing
as a null pointer, and only values of type Maybe t
are optional.)
Since we now know about
Maybe
, I
have expanded our quick library reference with several useful
functions that return Maybe
values (including the find
and elemIndex
functions.)