Many fixes
This commit is contained in:
parent
d7b5a5ed85
commit
3be44696c9
19 changed files with 112 additions and 124 deletions
|
@ -226,11 +226,6 @@ A major part of this tutorial will explain why.
|
||||||
In Haskell, there is a `main` function and every object has a type.
|
In Haskell, there is a `main` function and every object has a type.
|
||||||
The type of `main` is `IO ()`.
|
The type of `main` is `IO ()`.
|
||||||
This means, `main` will cause side effects.
|
This means, `main` will cause side effects.
|
||||||
`IO` is a ... .
|
|
||||||
Wait! No! I won't say it now!
|
|
||||||
I am afraid to terrify you.
|
|
||||||
You might run away crying.
|
|
||||||
For now, I won't talk about what `IO` really is.
|
|
||||||
|
|
||||||
Just remember that Haskell can look a lot like other imperative languages.
|
Just remember that Haskell can look a lot like other imperative languages.
|
||||||
|
|
||||||
|
@ -702,7 +697,7 @@ square'' x = (^2) x
|
||||||
</code>
|
</code>
|
||||||
</div>
|
</div>
|
||||||
We can remove `x` in the left and right side!
|
We can remove `x` in the left and right side!
|
||||||
It's called currying.
|
It's called η-reduction.
|
||||||
|
|
||||||
<div class="codehighlight">
|
<div class="codehighlight">
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
|
@ -812,7 +807,7 @@ int evenSum(int *list) {
|
||||||
int accumSum(int n, int *list) {
|
int accumSum(int n, int *list) {
|
||||||
int x;
|
int x;
|
||||||
int *xs;
|
int *xs;
|
||||||
if (*list == NULL) { // if the list is empty
|
if (*list == 0) { // if the list is empty
|
||||||
return n;
|
return n;
|
||||||
} else {
|
} else {
|
||||||
x = list[0]; // let x be the first element of the list
|
x = list[0]; // let x be the first element of the list
|
||||||
|
@ -990,7 +985,9 @@ evenSum l = accumSum 0 l
|
||||||
</code>
|
</code>
|
||||||
</div>
|
</div>
|
||||||
What is pattern matching?
|
What is pattern matching?
|
||||||
Use value instead of general parameter name.
|
Use value instead of general parameter name[^021301].
|
||||||
|
|
||||||
|
[^021301]: For the brave, a more complete explanation of pattern matching can be found [here](http://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/patterns.html).
|
||||||
|
|
||||||
Instead of saying: `foo l = if l == [] then <x> else <y>`
|
Instead of saying: `foo l = if l == [] then <x> else <y>`
|
||||||
You simply state:
|
You simply state:
|
||||||
|
@ -1036,7 +1033,7 @@ main = print $ evenSum [1..10]
|
||||||
|
|
||||||
<hr/><a href="code/02_Hard_Part/14_Functions.lhs" class="cut">02_Hard_Part/<strong>14_Functions.lhs</strong></a>
|
<hr/><a href="code/02_Hard_Part/14_Functions.lhs" class="cut">02_Hard_Part/<strong>14_Functions.lhs</strong></a>
|
||||||
|
|
||||||
In Haskell you can simplify function definition by curry them.
|
In Haskell you can simplify function definition by η-reducing them.
|
||||||
For example, instead of writing:
|
For example, instead of writing:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
|
@ -1221,7 +1218,7 @@ The `(.)` function correspond to the mathematical composition.
|
||||||
(f . g . h) x ⇔ f ( g (h x))
|
(f . g . h) x ⇔ f ( g (h x))
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
We can take advantage of this operator to curry a bit more our function:
|
We can take advantage of this operator to η-reduce our function:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
-- Version 9
|
-- Version 9
|
||||||
|
@ -2585,7 +2582,7 @@ let (y,w') = action x w in
|
||||||
|
|
||||||
Even if for some line the first `x` argument isn't needed.
|
Even if for some line the first `x` argument isn't needed.
|
||||||
The output type is a couple, `(answer, newWorldValue)`.
|
The output type is a couple, `(answer, newWorldValue)`.
|
||||||
Each function `f` must have a type of kind:
|
Each function `f` must have a type similar to:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
f :: World -> (a,World)
|
f :: World -> (a,World)
|
||||||
|
@ -2619,7 +2616,7 @@ And of course `actionN w :: (World) -> (a,World)`.
|
||||||
>
|
>
|
||||||
> ~~~
|
> ~~~
|
||||||
> let (x,w1) = action1 w0 in
|
> let (x,w1) = action1 w0 in
|
||||||
> let (y,w2) - action2 w1 in
|
> let (y,w2) = action2 w1 in
|
||||||
> ~~~
|
> ~~~
|
||||||
>
|
>
|
||||||
> and
|
> and
|
||||||
|
@ -2756,9 +2753,9 @@ Haskell has made a syntactical sugar for us:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
do
|
do
|
||||||
y <- action1
|
x <- action1
|
||||||
z <- action2
|
y <- action2
|
||||||
t <- action3
|
z <- action3
|
||||||
...
|
...
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
|
@ -2782,7 +2779,8 @@ blindBind action1 action2 w0 =
|
||||||
bind action (\_ -> action2) w0
|
bind action (\_ -> action2) w0
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
I didn't curried this definition for clarity purpose. Of course we can use a better notation, we'll use the `(>>)` operator.
|
I didn't simplified this definition for clarity purpose.
|
||||||
|
Of course we can use a better notation, we'll use the `(>>)` operator.
|
||||||
|
|
||||||
And
|
And
|
||||||
|
|
||||||
|
|
|
@ -232,11 +232,6 @@ A major part of this tutorial will explain why.
|
||||||
In Haskell, there is a `main` function and every object has a type.
|
In Haskell, there is a `main` function and every object has a type.
|
||||||
The type of `main` is `IO ()`.
|
The type of `main` is `IO ()`.
|
||||||
This means, `main` will cause side effects.
|
This means, `main` will cause side effects.
|
||||||
`IO` is a ... .
|
|
||||||
Wait! No! I won't say it now!
|
|
||||||
I am afraid to terrify you.
|
|
||||||
You might run away crying.
|
|
||||||
For now, I won't talk about what `IO` really is.
|
|
||||||
|
|
||||||
Just remember that Haskell can look a lot like other imperative languages.
|
Just remember that Haskell can look a lot like other imperative languages.
|
||||||
|
|
||||||
|
@ -708,7 +703,7 @@ square'' x = (^2) x
|
||||||
</code>
|
</code>
|
||||||
</div>
|
</div>
|
||||||
We can remove `x` in the left and right side!
|
We can remove `x` in the left and right side!
|
||||||
It's called currying.
|
It's called η-reduction.
|
||||||
|
|
||||||
<div class="codehighlight">
|
<div class="codehighlight">
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
|
@ -818,7 +813,7 @@ int evenSum(int *list) {
|
||||||
int accumSum(int n, int *list) {
|
int accumSum(int n, int *list) {
|
||||||
int x;
|
int x;
|
||||||
int *xs;
|
int *xs;
|
||||||
if (*list == NULL) { // if the list is empty
|
if (*list == 0) { // if the list is empty
|
||||||
return n;
|
return n;
|
||||||
} else {
|
} else {
|
||||||
x = list[0]; // let x be the first element of the list
|
x = list[0]; // let x be the first element of the list
|
||||||
|
@ -996,7 +991,9 @@ evenSum l = accumSum 0 l
|
||||||
</code>
|
</code>
|
||||||
</div>
|
</div>
|
||||||
What is pattern matching?
|
What is pattern matching?
|
||||||
Use value instead of general parameter name.
|
Use value instead of general parameter name[^021301].
|
||||||
|
|
||||||
|
[^021301]: For the brave, a more complete explanation of pattern matching can be found [here](http://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/patterns.html).
|
||||||
|
|
||||||
Instead of saying: `foo l = if l == [] then <x> else <y>`
|
Instead of saying: `foo l = if l == [] then <x> else <y>`
|
||||||
You simply state:
|
You simply state:
|
||||||
|
@ -1042,7 +1039,7 @@ main = print $ evenSum [1..10]
|
||||||
|
|
||||||
<hr/><a href="code/02_Hard_Part/14_Functions.lhs" class="cut">02_Hard_Part/<strong>14_Functions.lhs</strong></a>
|
<hr/><a href="code/02_Hard_Part/14_Functions.lhs" class="cut">02_Hard_Part/<strong>14_Functions.lhs</strong></a>
|
||||||
|
|
||||||
In Haskell you can simplify function definition by curry them.
|
In Haskell you can simplify function definition by η-reducing them.
|
||||||
For example, instead of writing:
|
For example, instead of writing:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
|
@ -1227,7 +1224,7 @@ The `(.)` function correspond to the mathematical composition.
|
||||||
(f . g . h) x ⇔ f ( g (h x))
|
(f . g . h) x ⇔ f ( g (h x))
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
We can take advantage of this operator to curry a bit more our function:
|
We can take advantage of this operator to η-reduce our function:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
-- Version 9
|
-- Version 9
|
||||||
|
@ -2591,7 +2588,7 @@ let (y,w') = action x w in
|
||||||
|
|
||||||
Even if for some line the first `x` argument isn't needed.
|
Even if for some line the first `x` argument isn't needed.
|
||||||
The output type is a couple, `(answer, newWorldValue)`.
|
The output type is a couple, `(answer, newWorldValue)`.
|
||||||
Each function `f` must have a type of kind:
|
Each function `f` must have a type similar to:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
f :: World -> (a,World)
|
f :: World -> (a,World)
|
||||||
|
@ -2625,7 +2622,7 @@ And of course `actionN w :: (World) -> (a,World)`.
|
||||||
>
|
>
|
||||||
> ~~~
|
> ~~~
|
||||||
> let (x,w1) = action1 w0 in
|
> let (x,w1) = action1 w0 in
|
||||||
> let (y,w2) - action2 w1 in
|
> let (y,w2) = action2 w1 in
|
||||||
> ~~~
|
> ~~~
|
||||||
>
|
>
|
||||||
> and
|
> and
|
||||||
|
@ -2762,9 +2759,9 @@ Haskell has made a syntactical sugar for us:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
do
|
do
|
||||||
y <- action1
|
x <- action1
|
||||||
z <- action2
|
y <- action2
|
||||||
t <- action3
|
z <- action3
|
||||||
...
|
...
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
|
@ -2788,7 +2785,8 @@ blindBind action1 action2 w0 =
|
||||||
bind action (\_ -> action2) w0
|
bind action (\_ -> action2) w0
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
I didn't curried this definition for clarity purpose. Of course we can use a better notation, we'll use the `(>>)` operator.
|
I didn't simplified this definition for clarity purpose.
|
||||||
|
Of course we can use a better notation, we'll use the `(>>)` operator.
|
||||||
|
|
||||||
And
|
And
|
||||||
|
|
||||||
|
|
|
@ -285,11 +285,6 @@ A major part of this tutorial will explain why.
|
||||||
In Haskell, there is a `main` function and every object has a type.
|
In Haskell, there is a `main` function and every object has a type.
|
||||||
The type of `main` is `IO ()`.
|
The type of `main` is `IO ()`.
|
||||||
This means, `main` will cause side effects.
|
This means, `main` will cause side effects.
|
||||||
`IO` is a ... .
|
|
||||||
Wait! No! I won't say it now!
|
|
||||||
I am afraid to terrify you.
|
|
||||||
You might run away crying.
|
|
||||||
For now, I won't talk about what `IO` really is.
|
|
||||||
|
|
||||||
Just remember that Haskell can look a lot like other imperative languages.
|
Just remember that Haskell can look a lot like other imperative languages.
|
||||||
|
|
||||||
|
@ -761,7 +756,7 @@ square'' x = (^2) x
|
||||||
</code>
|
</code>
|
||||||
</div>
|
</div>
|
||||||
We can remove `x` in the left and right side!
|
We can remove `x` in the left and right side!
|
||||||
It's called currying.
|
It's called η-reduction.
|
||||||
|
|
||||||
<div class="codehighlight">
|
<div class="codehighlight">
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
|
@ -871,7 +866,7 @@ int evenSum(int *list) {
|
||||||
int accumSum(int n, int *list) {
|
int accumSum(int n, int *list) {
|
||||||
int x;
|
int x;
|
||||||
int *xs;
|
int *xs;
|
||||||
if (*list == NULL) { // if the list is empty
|
if (*list == 0) { // if the list is empty
|
||||||
return n;
|
return n;
|
||||||
} else {
|
} else {
|
||||||
x = list[0]; // let x be the first element of the list
|
x = list[0]; // let x be the first element of the list
|
||||||
|
@ -1049,7 +1044,9 @@ evenSum l = accumSum 0 l
|
||||||
</code>
|
</code>
|
||||||
</div>
|
</div>
|
||||||
What is pattern matching?
|
What is pattern matching?
|
||||||
Use value instead of general parameter name.
|
Use value instead of general parameter name[^021301].
|
||||||
|
|
||||||
|
[^021301]: For the brave, a more complete explanation of pattern matching can be found [here](http://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/patterns.html).
|
||||||
|
|
||||||
Instead of saying: `foo l = if l == [] then <x> else <y>`
|
Instead of saying: `foo l = if l == [] then <x> else <y>`
|
||||||
You simply state:
|
You simply state:
|
||||||
|
@ -1095,7 +1092,7 @@ main = print $ evenSum [1..10]
|
||||||
|
|
||||||
<hr/><a href="code/02_Hard_Part/14_Functions.lhs" class="cut">02_Hard_Part/<strong>14_Functions.lhs</strong></a>
|
<hr/><a href="code/02_Hard_Part/14_Functions.lhs" class="cut">02_Hard_Part/<strong>14_Functions.lhs</strong></a>
|
||||||
|
|
||||||
In Haskell you can simplify function definition by curry them.
|
In Haskell you can simplify function definition by η-reducing them.
|
||||||
For example, instead of writing:
|
For example, instead of writing:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
|
@ -1280,7 +1277,7 @@ The `(.)` function correspond to the mathematical composition.
|
||||||
(f . g . h) x ⇔ f ( g (h x))
|
(f . g . h) x ⇔ f ( g (h x))
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
We can take advantage of this operator to curry a bit more our function:
|
We can take advantage of this operator to η-reduce our function:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
-- Version 9
|
-- Version 9
|
||||||
|
@ -2644,7 +2641,7 @@ let (y,w') = action x w in
|
||||||
|
|
||||||
Even if for some line the first `x` argument isn't needed.
|
Even if for some line the first `x` argument isn't needed.
|
||||||
The output type is a couple, `(answer, newWorldValue)`.
|
The output type is a couple, `(answer, newWorldValue)`.
|
||||||
Each function `f` must have a type of kind:
|
Each function `f` must have a type similar to:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
f :: World -> (a,World)
|
f :: World -> (a,World)
|
||||||
|
@ -2678,7 +2675,7 @@ And of course `actionN w :: (World) -> (a,World)`.
|
||||||
>
|
>
|
||||||
> ~~~
|
> ~~~
|
||||||
> let (x,w1) = action1 w0 in
|
> let (x,w1) = action1 w0 in
|
||||||
> let (y,w2) - action2 w1 in
|
> let (y,w2) = action2 w1 in
|
||||||
> ~~~
|
> ~~~
|
||||||
>
|
>
|
||||||
> and
|
> and
|
||||||
|
@ -2815,9 +2812,9 @@ Haskell has made a syntactical sugar for us:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
do
|
do
|
||||||
y <- action1
|
x <- action1
|
||||||
z <- action2
|
y <- action2
|
||||||
t <- action3
|
z <- action3
|
||||||
...
|
...
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
|
@ -2841,7 +2838,8 @@ blindBind action1 action2 w0 =
|
||||||
bind action (\_ -> action2) w0
|
bind action (\_ -> action2) w0
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
I didn't curried this definition for clarity purpose. Of course we can use a better notation, we'll use the `(>>)` operator.
|
I didn't simplified this definition for clarity purpose.
|
||||||
|
Of course we can use a better notation, we'll use the `(>>)` operator.
|
||||||
|
|
||||||
And
|
And
|
||||||
|
|
||||||
|
|
|
@ -40,10 +40,5 @@ A major part of this tutorial will explain why.
|
||||||
In Haskell, there is a `main` function and every object has a type.
|
In Haskell, there is a `main` function and every object has a type.
|
||||||
The type of `main` is `IO ()`.
|
The type of `main` is `IO ()`.
|
||||||
This means, `main` will cause side effects.
|
This means, `main` will cause side effects.
|
||||||
`IO` is a ... .
|
|
||||||
Wait! No! I won't say it now!
|
|
||||||
I am afraid to terrify you.
|
|
||||||
You might run away crying.
|
|
||||||
For now, I won't talk about what `IO` really is.
|
|
||||||
|
|
||||||
Just remember that Haskell can look a lot like other imperative languages.
|
Just remember that Haskell can look a lot like other imperative languages.
|
||||||
|
|
|
@ -30,7 +30,7 @@ You just have to put it inside parenthesis.
|
||||||
> square'' x = (^2) x
|
> square'' x = (^2) x
|
||||||
|
|
||||||
We can remove `x` in the left and right side!
|
We can remove `x` in the left and right side!
|
||||||
It's called currying.
|
It's called η-reduction.
|
||||||
|
|
||||||
> square''' = (^2)
|
> square''' = (^2)
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ int evenSum(int *list) {
|
||||||
int accumSum(int n, int *list) {
|
int accumSum(int n, int *list) {
|
||||||
int x;
|
int x;
|
||||||
int *xs;
|
int *xs;
|
||||||
if (*list == NULL) { // if the list is empty
|
if (*list == 0) { // if the list is empty
|
||||||
return n;
|
return n;
|
||||||
} else {
|
} else {
|
||||||
x = list[0]; // let x be the first element of the list
|
x = list[0]; // let x be the first element of the list
|
||||||
|
|
|
@ -10,7 +10,9 @@ Next, we can use pattern matching.
|
||||||
> else accumSum n xs
|
> else accumSum n xs
|
||||||
|
|
||||||
What is pattern matching?
|
What is pattern matching?
|
||||||
Use value instead of general parameter name.
|
Use value instead of general parameter name[^021301].
|
||||||
|
|
||||||
|
[^021301]: For the brave, a more complete explanation of pattern matching can be found [here](http://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/patterns.html).
|
||||||
|
|
||||||
Instead of saying: `foo l = if l == [] then <x> else <y>`
|
Instead of saying: `foo l = if l == [] then <x> else <y>`
|
||||||
You simply state:
|
You simply state:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
In Haskell you can simplify function definition by curry them.
|
In Haskell you can simplify function definition by η-reducing them.
|
||||||
For example, instead of writing:
|
For example, instead of writing:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
|
|
|
@ -31,7 +31,7 @@ The `(.)` function correspond to the mathematical composition.
|
||||||
(f . g . h) x ⇔ f ( g (h x))
|
(f . g . h) x ⇔ f ( g (h x))
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
We can take advantage of this operator to curry a bit more our function:
|
We can take advantage of this operator to η-reduce our function:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
-- Version 9
|
-- Version 9
|
||||||
|
|
|
@ -193,7 +193,7 @@ let (y,w') = action x w in
|
||||||
|
|
||||||
Even if for some line the first `x` argument isn't needed.
|
Even if for some line the first `x` argument isn't needed.
|
||||||
The output type is a couple, `(answer, newWorldValue)`.
|
The output type is a couple, `(answer, newWorldValue)`.
|
||||||
Each function `f` must have a type of kind:
|
Each function `f` must have a type similar to:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
f :: World -> (a,World)
|
f :: World -> (a,World)
|
||||||
|
@ -227,7 +227,7 @@ And of course `actionN w :: (World) -> (a,World)`.
|
||||||
>
|
>
|
||||||
> ~~~
|
> ~~~
|
||||||
> let (x,w1) = action1 w0 in
|
> let (x,w1) = action1 w0 in
|
||||||
> let (y,w2) - action2 w1 in
|
> let (y,w2) = action2 w1 in
|
||||||
> ~~~
|
> ~~~
|
||||||
>
|
>
|
||||||
> and
|
> and
|
||||||
|
@ -365,9 +365,9 @@ Haskell has made a syntactical sugar for us:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
do
|
do
|
||||||
y <- action1
|
x <- action1
|
||||||
z <- action2
|
y <- action2
|
||||||
t <- action3
|
z <- action3
|
||||||
...
|
...
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
|
@ -391,7 +391,8 @@ blindBind action1 action2 w0 =
|
||||||
bind action (\_ -> action2) w0
|
bind action (\_ -> action2) w0
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
I didn't curried this definition for clarity purpose. Of course we can use a better notation, we'll use the `(>>)` operator.
|
I didn't simplified this definition for clarity purpose.
|
||||||
|
Of course we can use a better notation, we'll use the `(>>)` operator.
|
||||||
|
|
||||||
And
|
And
|
||||||
|
|
||||||
|
|
|
@ -331,12 +331,7 @@ A major part of this tutorial will explain why.</p>
|
||||||
|
|
||||||
<p>In Haskell, there is a <code>main</code> function and every object has a type.
|
<p>In Haskell, there is a <code>main</code> function and every object has a type.
|
||||||
The type of <code>main</code> is <code>IO ()</code>.
|
The type of <code>main</code> is <code>IO ()</code>.
|
||||||
This means, <code>main</code> will cause side effects.
|
This means, <code>main</code> will cause side effects.</p>
|
||||||
<code>IO</code> is a … .
|
|
||||||
Wait! No! I won’t say it now!
|
|
||||||
I am afraid to terrify you.
|
|
||||||
You might run away crying.
|
|
||||||
For now, I won’t talk about what <code>IO</code> really is.</p>
|
|
||||||
|
|
||||||
<p>Just remember that Haskell can look a lot like other imperative languages.</p>
|
<p>Just remember that Haskell can look a lot like other imperative languages.</p>
|
||||||
|
|
||||||
|
@ -828,7 +823,7 @@ square'' x = (^2) x
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<p>We can remove <code>x</code> in the left and right side!
|
<p>We can remove <code>x</code> in the left and right side!
|
||||||
It’s called currying.</p>
|
It’s called η-reduction.</p>
|
||||||
|
|
||||||
<div class="codehighlight">
|
<div class="codehighlight">
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
|
@ -946,7 +941,7 @@ Note, for simplicity, I assume the int list should end with the first <code>0</c
|
||||||
<span class="Storage">int</span> <span class="Entity">accumS<span class="Entity">um</span></span>(<span class="Storage">int</span> n, <span class="Storage">int</span> *list) {
|
<span class="Storage">int</span> <span class="Entity">accumS<span class="Entity">um</span></span>(<span class="Storage">int</span> n, <span class="Storage">int</span> *list) {
|
||||||
<span class="Storage">int</span> x;
|
<span class="Storage">int</span> x;
|
||||||
<span class="Storage">int</span> *xs;
|
<span class="Storage">int</span> *xs;
|
||||||
<span class="Keyword">if</span> (*list == <span class="Constant">NULL</span>) { <span class="Comment"><span class="Comment">//</span> if the list is empty</span>
|
<span class="Keyword">if</span> (*list == <span class="Constant">0</span>) { <span class="Comment"><span class="Comment">//</span> if the list is empty</span>
|
||||||
<span class="Keyword">return</span> n;
|
<span class="Keyword">return</span> n;
|
||||||
} <span class="Keyword">else</span> {
|
} <span class="Keyword">else</span> {
|
||||||
x = list[<span class="Constant">0</span>]; <span class="Comment"><span class="Comment">//</span> let x be the first element of the list</span>
|
x = list[<span class="Constant">0</span>]; <span class="Comment"><span class="Comment">//</span> let x be the first element of the list</span>
|
||||||
|
@ -1125,7 +1120,7 @@ evenSum l = accumSum 0 l
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<p>What is pattern matching?
|
<p>What is pattern matching?
|
||||||
Use value instead of general parameter name.</p>
|
Use value instead of general parameter name<sup id="fnref:021301"><a href="#fn:021301" rel="footnote">3</a></sup>.</p>
|
||||||
|
|
||||||
<p>Instead of saying: <code>foo l = if l == [] then <x> else <y></code>
|
<p>Instead of saying: <code>foo l = if l == [] then <x> else <y></code>
|
||||||
You simply state: </p>
|
You simply state: </p>
|
||||||
|
@ -1172,7 +1167,7 @@ main = <span class="Entity">print</span> $ evenSum [1..10]
|
||||||
<hr />
|
<hr />
|
||||||
<p><a href="code/02_Hard_Part/14_Functions.lhs" class="cut">02_Hard_Part/<strong>14_Functions.lhs</strong></a></p>
|
<p><a href="code/02_Hard_Part/14_Functions.lhs" class="cut">02_Hard_Part/<strong>14_Functions.lhs</strong></a></p>
|
||||||
|
|
||||||
<p>In Haskell you can simplify function definition by curry them.
|
<p>In Haskell you can simplify function definition by η-reducing them.
|
||||||
For example, instead of writing:</p>
|
For example, instead of writing:</p>
|
||||||
|
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
|
@ -1359,7 +1354,7 @@ The <code>(.)</code> function correspond to the mathematical composition.</p>
|
||||||
(f . g . h) x ⇔ f ( g (h x))
|
(f . g . h) x ⇔ f ( g (h x))
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>We can take advantage of this operator to curry a bit more our function:</p>
|
<p>We can take advantage of this operator to η-reduce our function:</p>
|
||||||
|
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
<span class="Comment"><span class="Comment">--</span> Version 9</span>
|
<span class="Comment"><span class="Comment">--</span> Version 9</span>
|
||||||
|
@ -1401,7 +1396,7 @@ squareEvenSum' = evenSum . (<span class="Entity">map</span> (^2))
|
||||||
squareEvenSum'' = <span class="Entity">sum</span>' . (<span class="Entity">map</span> (^2)) . (<span class="Entity">filter</span> <span class="Entity">even</span>)
|
squareEvenSum'' = <span class="Entity">sum</span>' . (<span class="Entity">map</span> (^2)) . (<span class="Entity">filter</span> <span class="Entity">even</span>)
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<p>We just had to add another “transformation function”<sup id="fnref:0216"><a href="#fn:0216" rel="footnote">3</a></sup>.</p>
|
<p>We just had to add another “transformation function”<sup id="fnref:0216"><a href="#fn:0216" rel="footnote">4</a></sup>.</p>
|
||||||
|
|
||||||
<pre><code>map (^2) [1,2,3,4] ⇔ [1,4,9,16]
|
<pre><code>map (^2) [1,2,3,4] ⇔ [1,4,9,16]
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
@ -2407,7 +2402,7 @@ Its definition is:</p>
|
||||||
<p>This is a nice way to tell there was an error while trying to create/compute
|
<p>This is a nice way to tell there was an error while trying to create/compute
|
||||||
a value.
|
a value.
|
||||||
The <code>maybeRead</code> function is a great example of this.
|
The <code>maybeRead</code> function is a great example of this.
|
||||||
This is a function similar to the function <code>read</code><sup id="fnref:1"><a href="#fn:1" rel="footnote">4</a></sup>,
|
This is a function similar to the function <code>read</code><sup id="fnref:1"><a href="#fn:1" rel="footnote">5</a></sup>,
|
||||||
but if something goes wrong the returned value is <code>Nothing</code>.
|
but if something goes wrong the returned value is <code>Nothing</code>.
|
||||||
If the value is right, it returns <code>Just <the value></code>.
|
If the value is right, it returns <code>Just <the value></code>.
|
||||||
Don’t try to understand too much of this function.
|
Don’t try to understand too much of this function.
|
||||||
|
@ -2640,10 +2635,10 @@ It’s type is then something like:</p>
|
||||||
|
|
||||||
<p>Not all function could have access to this variable.
|
<p>Not all function could have access to this variable.
|
||||||
Those who have access to this variable can potentially be impure.
|
Those who have access to this variable can potentially be impure.
|
||||||
Functions whose the world variable isn’t provided to should be pure<sup id="fnref:032001"><a href="#fn:032001" rel="footnote">5</a></sup>.</p>
|
Functions whose the world variable isn’t provided to should be pure<sup id="fnref:032001"><a href="#fn:032001" rel="footnote">6</a></sup>.</p>
|
||||||
|
|
||||||
<p>Haskell consider the state of the world is an input variable for <code>main</code>.
|
<p>Haskell consider the state of the world is an input variable for <code>main</code>.
|
||||||
But the real type of main is closer to this one<sup id="fnref:032002"><a href="#fn:032002" rel="footnote">6</a></sup>:</p>
|
But the real type of main is closer to this one<sup id="fnref:032002"><a href="#fn:032002" rel="footnote">7</a></sup>:</p>
|
||||||
|
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
<span class="Entity">main</span> :: <span class="Constant">World</span> -> ((),<span class="Constant">World</span>)
|
<span class="Entity">main</span> :: <span class="Constant">World</span> -> ((),<span class="Constant">World</span>)
|
||||||
|
@ -2743,7 +2738,7 @@ Each line is of the form:</p>
|
||||||
|
|
||||||
<p>Even if for some line the first <code>x</code> argument isn’t needed.
|
<p>Even if for some line the first <code>x</code> argument isn’t needed.
|
||||||
The output type is a couple, <code>(answer, newWorldValue)</code>.
|
The output type is a couple, <code>(answer, newWorldValue)</code>.
|
||||||
Each function <code>f</code> must have a type of kind:</p>
|
Each function <code>f</code> must have a type similar to:</p>
|
||||||
|
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
<span class="Entity">f</span> :: <span class="Constant">World</span> -> (<span class="Variable">a,World</span>)
|
<span class="Entity">f</span> :: <span class="Constant">World</span> -> (<span class="Variable">a,World</span>)
|
||||||
|
@ -2777,7 +2772,7 @@ And in particular, each action can take a parameter from the result of a line ab
|
||||||
<p>IMPORTANT, there are only two important pattern for us:</p>
|
<p>IMPORTANT, there are only two important pattern for us:</p>
|
||||||
|
|
||||||
<pre><code>let (x,w1) = action1 w0 in
|
<pre><code>let (x,w1) = action1 w0 in
|
||||||
let (y,w2) - action2 w1 in
|
let (y,w2) = action2 w1 in
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
<p>and</p>
|
<p>and</p>
|
||||||
|
@ -2914,9 +2909,9 @@ Haskell has made a syntactical sugar for us:</p>
|
||||||
|
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
<span class="Keyword">do</span>
|
<span class="Keyword">do</span>
|
||||||
y <- action1
|
x <- action1
|
||||||
z <- action2
|
y <- action2
|
||||||
t <- action3
|
z <- action3
|
||||||
...
|
...
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
@ -2940,7 +2935,8 @@ blindBind action1 action2 w0 =
|
||||||
bind action (\_ -> action2) w0
|
bind action (\_ -> action2) w0
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>I didn’t curried this definition for clarity purpose. Of course we can use a better notation, we’ll use the <code>(>>)</code> operator.</p>
|
<p>I didn’t simplified this definition for clarity purpose.
|
||||||
|
Of course we can use a better notation, we’ll use the <code>(>>)</code> operator.</p>
|
||||||
|
|
||||||
<p>And</p>
|
<p>And</p>
|
||||||
|
|
||||||
|
@ -3271,7 +3267,7 @@ In particular, monad are very useful for: </p>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<p>If you have followed me until here, then you’ve done it!
|
<p>If you have followed me until here, then you’ve done it!
|
||||||
You know monads<sup id="fnref:03021301"><a href="#fn:03021301" rel="footnote">7</a></sup>!</p>
|
You know monads<sup id="fnref:03021301"><a href="#fn:03021301" rel="footnote">8</a></sup>!</p>
|
||||||
|
|
||||||
<p><a href="code/03_Hell/02_Monads/13_Monads.lhs" class="cut">03_Hell/02_Monads/<strong>13_Monads.lhs</strong> </a></p>
|
<p><a href="code/03_Hell/02_Monads/13_Monads.lhs" class="cut">03_Hell/02_Monads/<strong>13_Monads.lhs</strong> </a></p>
|
||||||
|
|
||||||
|
@ -3628,6 +3624,9 @@ treeFromList' (x:xs) n = <span class="Constant">Node</span> x left right
|
||||||
<li id="fn:2">
|
<li id="fn:2">
|
||||||
<p>I know I cheat. But I will talk about non-strict later.<a href="#fnref:2" rel="reference">↩</a></p>
|
<p>I know I cheat. But I will talk about non-strict later.<a href="#fnref:2" rel="reference">↩</a></p>
|
||||||
</li>
|
</li>
|
||||||
|
<li id="fn:021301">
|
||||||
|
<p>For the brave, a more complete explanation of pattern matching can be found <a href="http://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/patterns.html">here</a>.<a href="#fnref:021301" rel="reference">↩</a></p>
|
||||||
|
</li>
|
||||||
<li id="fn:0216">
|
<li id="fn:0216">
|
||||||
<p>You should remark <code>squareEvenSum''</code> is more efficient that the two other versions. The order of <code>(.)</code> is important.<a href="#fnref:0216" rel="reference">↩</a></p>
|
<p>You should remark <code>squareEvenSum''</code> is more efficient that the two other versions. The order of <code>(.)</code> is important.<a href="#fnref:0216" rel="reference">↩</a></p>
|
||||||
</li>
|
</li>
|
||||||
|
|
|
@ -40,10 +40,5 @@ A major part of this tutorial will explain why.
|
||||||
In Haskell, there is a `main` function and every object has a type.
|
In Haskell, there is a `main` function and every object has a type.
|
||||||
The type of `main` is `IO ()`.
|
The type of `main` is `IO ()`.
|
||||||
This means, `main` will cause side effects.
|
This means, `main` will cause side effects.
|
||||||
`IO` is a ... .
|
|
||||||
Wait! No! I won't say it now!
|
|
||||||
I am afraid to terrify you.
|
|
||||||
You might run away crying.
|
|
||||||
For now, I won't talk about what `IO` really is.
|
|
||||||
|
|
||||||
Just remember that Haskell can look a lot like other imperative languages.
|
Just remember that Haskell can look a lot like other imperative languages.
|
||||||
|
|
|
@ -30,7 +30,7 @@ You just have to put it inside parenthesis.
|
||||||
> square'' x = (^2) x
|
> square'' x = (^2) x
|
||||||
|
|
||||||
We can remove `x` in the left and right side!
|
We can remove `x` in the left and right side!
|
||||||
It's called currying.
|
It's called η-reduction.
|
||||||
|
|
||||||
> square''' = (^2)
|
> square''' = (^2)
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ int evenSum(int *list) {
|
||||||
int accumSum(int n, int *list) {
|
int accumSum(int n, int *list) {
|
||||||
int x;
|
int x;
|
||||||
int *xs;
|
int *xs;
|
||||||
if (*list == NULL) { // if the list is empty
|
if (*list == 0) { // if the list is empty
|
||||||
return n;
|
return n;
|
||||||
} else {
|
} else {
|
||||||
x = list[0]; // let x be the first element of the list
|
x = list[0]; // let x be the first element of the list
|
||||||
|
|
|
@ -10,7 +10,9 @@ Next, we can use pattern matching.
|
||||||
> else accumSum n xs
|
> else accumSum n xs
|
||||||
|
|
||||||
What is pattern matching?
|
What is pattern matching?
|
||||||
Use value instead of general parameter name.
|
Use value instead of general parameter name[^021301].
|
||||||
|
|
||||||
|
[^021301]: For the brave, a more complete explanation of pattern matching can be found [here](http://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/patterns.html).
|
||||||
|
|
||||||
Instead of saying: `foo l = if l == [] then <x> else <y>`
|
Instead of saying: `foo l = if l == [] then <x> else <y>`
|
||||||
You simply state:
|
You simply state:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
In Haskell you can simplify function definition by curry them.
|
In Haskell you can simplify function definition by η-reducing them.
|
||||||
For example, instead of writing:
|
For example, instead of writing:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
|
|
|
@ -31,7 +31,7 @@ The `(.)` function correspond to the mathematical composition.
|
||||||
(f . g . h) x ⇔ f ( g (h x))
|
(f . g . h) x ⇔ f ( g (h x))
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
We can take advantage of this operator to curry a bit more our function:
|
We can take advantage of this operator to η-reduce our function:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
-- Version 9
|
-- Version 9
|
||||||
|
|
|
@ -193,7 +193,7 @@ let (y,w') = action x w in
|
||||||
|
|
||||||
Even if for some line the first `x` argument isn't needed.
|
Even if for some line the first `x` argument isn't needed.
|
||||||
The output type is a couple, `(answer, newWorldValue)`.
|
The output type is a couple, `(answer, newWorldValue)`.
|
||||||
Each function `f` must have a type of kind:
|
Each function `f` must have a type similar to:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
f :: World -> (a,World)
|
f :: World -> (a,World)
|
||||||
|
@ -227,7 +227,7 @@ And of course `actionN w :: (World) -> (a,World)`.
|
||||||
>
|
>
|
||||||
> ~~~
|
> ~~~
|
||||||
> let (x,w1) = action1 w0 in
|
> let (x,w1) = action1 w0 in
|
||||||
> let (y,w2) - action2 w1 in
|
> let (y,w2) = action2 w1 in
|
||||||
> ~~~
|
> ~~~
|
||||||
>
|
>
|
||||||
> and
|
> and
|
||||||
|
@ -365,9 +365,9 @@ Haskell has made a syntactical sugar for us:
|
||||||
|
|
||||||
<code class="haskell">
|
<code class="haskell">
|
||||||
do
|
do
|
||||||
y <- action1
|
x <- action1
|
||||||
z <- action2
|
y <- action2
|
||||||
t <- action3
|
z <- action3
|
||||||
...
|
...
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
|
@ -391,7 +391,8 @@ blindBind action1 action2 w0 =
|
||||||
bind action (\_ -> action2) w0
|
bind action (\_ -> action2) w0
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
I didn't curried this definition for clarity purpose. Of course we can use a better notation, we'll use the `(>>)` operator.
|
I didn't simplified this definition for clarity purpose.
|
||||||
|
Of course we can use a better notation, we'll use the `(>>)` operator.
|
||||||
|
|
||||||
And
|
And
|
||||||
|
|
||||||
|
|
|
@ -338,12 +338,7 @@ A major part of this tutorial will explain why.</p>
|
||||||
|
|
||||||
<p>In Haskell, there is a <code>main</code> function and every object has a type.
|
<p>In Haskell, there is a <code>main</code> function and every object has a type.
|
||||||
The type of <code>main</code> is <code>IO ()</code>.
|
The type of <code>main</code> is <code>IO ()</code>.
|
||||||
This means, <code>main</code> will cause side effects.
|
This means, <code>main</code> will cause side effects.</p>
|
||||||
<code>IO</code> is a … .
|
|
||||||
Wait! No! I won’t say it now!
|
|
||||||
I am afraid to terrify you.
|
|
||||||
You might run away crying.
|
|
||||||
For now, I won’t talk about what <code>IO</code> really is.</p>
|
|
||||||
|
|
||||||
<p>Just remember that Haskell can look a lot like other imperative languages.</p>
|
<p>Just remember that Haskell can look a lot like other imperative languages.</p>
|
||||||
|
|
||||||
|
@ -835,7 +830,7 @@ square'' x = (^2) x
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<p>We can remove <code>x</code> in the left and right side!
|
<p>We can remove <code>x</code> in the left and right side!
|
||||||
It’s called currying.</p>
|
It’s called η-reduction.</p>
|
||||||
|
|
||||||
<div class="codehighlight">
|
<div class="codehighlight">
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
|
@ -953,7 +948,7 @@ Note, for simplicity, I assume the int list should end with the first <code>0</c
|
||||||
<span class="Storage">int</span> <span class="Entity">accumS<span class="Entity">um</span></span>(<span class="Storage">int</span> n, <span class="Storage">int</span> *list) {
|
<span class="Storage">int</span> <span class="Entity">accumS<span class="Entity">um</span></span>(<span class="Storage">int</span> n, <span class="Storage">int</span> *list) {
|
||||||
<span class="Storage">int</span> x;
|
<span class="Storage">int</span> x;
|
||||||
<span class="Storage">int</span> *xs;
|
<span class="Storage">int</span> *xs;
|
||||||
<span class="Keyword">if</span> (*list == <span class="Constant">NULL</span>) { <span class="Comment"><span class="Comment">//</span> if the list is empty</span>
|
<span class="Keyword">if</span> (*list == <span class="Constant">0</span>) { <span class="Comment"><span class="Comment">//</span> if the list is empty</span>
|
||||||
<span class="Keyword">return</span> n;
|
<span class="Keyword">return</span> n;
|
||||||
} <span class="Keyword">else</span> {
|
} <span class="Keyword">else</span> {
|
||||||
x = list[<span class="Constant">0</span>]; <span class="Comment"><span class="Comment">//</span> let x be the first element of the list</span>
|
x = list[<span class="Constant">0</span>]; <span class="Comment"><span class="Comment">//</span> let x be the first element of the list</span>
|
||||||
|
@ -1132,7 +1127,7 @@ evenSum l = accumSum 0 l
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<p>What is pattern matching?
|
<p>What is pattern matching?
|
||||||
Use value instead of general parameter name.</p>
|
Use value instead of general parameter name<sup id="fnref:021301"><a href="#fn:021301" rel="footnote">3</a></sup>.</p>
|
||||||
|
|
||||||
<p>Instead of saying: <code>foo l = if l == [] then <x> else <y></code>
|
<p>Instead of saying: <code>foo l = if l == [] then <x> else <y></code>
|
||||||
You simply state: </p>
|
You simply state: </p>
|
||||||
|
@ -1179,7 +1174,7 @@ main = <span class="Entity">print</span> $ evenSum [1..10]
|
||||||
<hr />
|
<hr />
|
||||||
<p><a href="code/02_Hard_Part/14_Functions.lhs" class="cut">02_Hard_Part/<strong>14_Functions.lhs</strong></a></p>
|
<p><a href="code/02_Hard_Part/14_Functions.lhs" class="cut">02_Hard_Part/<strong>14_Functions.lhs</strong></a></p>
|
||||||
|
|
||||||
<p>In Haskell you can simplify function definition by curry them.
|
<p>In Haskell you can simplify function definition by η-reducing them.
|
||||||
For example, instead of writing:</p>
|
For example, instead of writing:</p>
|
||||||
|
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
|
@ -1366,7 +1361,7 @@ The <code>(.)</code> function correspond to the mathematical composition.</p>
|
||||||
(f . g . h) x ⇔ f ( g (h x))
|
(f . g . h) x ⇔ f ( g (h x))
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>We can take advantage of this operator to curry a bit more our function:</p>
|
<p>We can take advantage of this operator to η-reduce our function:</p>
|
||||||
|
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
<span class="Comment"><span class="Comment">--</span> Version 9</span>
|
<span class="Comment"><span class="Comment">--</span> Version 9</span>
|
||||||
|
@ -1408,7 +1403,7 @@ squareEvenSum' = evenSum . (<span class="Entity">map</span> (^2))
|
||||||
squareEvenSum'' = <span class="Entity">sum</span>' . (<span class="Entity">map</span> (^2)) . (<span class="Entity">filter</span> <span class="Entity">even</span>)
|
squareEvenSum'' = <span class="Entity">sum</span>' . (<span class="Entity">map</span> (^2)) . (<span class="Entity">filter</span> <span class="Entity">even</span>)
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<p>We just had to add another “transformation function”<sup id="fnref:0216"><a href="#fn:0216" rel="footnote">3</a></sup>.</p>
|
<p>We just had to add another “transformation function”<sup id="fnref:0216"><a href="#fn:0216" rel="footnote">4</a></sup>.</p>
|
||||||
|
|
||||||
<pre><code>map (^2) [1,2,3,4] ⇔ [1,4,9,16]
|
<pre><code>map (^2) [1,2,3,4] ⇔ [1,4,9,16]
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
@ -2414,7 +2409,7 @@ Its definition is:</p>
|
||||||
<p>This is a nice way to tell there was an error while trying to create/compute
|
<p>This is a nice way to tell there was an error while trying to create/compute
|
||||||
a value.
|
a value.
|
||||||
The <code>maybeRead</code> function is a great example of this.
|
The <code>maybeRead</code> function is a great example of this.
|
||||||
This is a function similar to the function <code>read</code><sup id="fnref:1"><a href="#fn:1" rel="footnote">4</a></sup>,
|
This is a function similar to the function <code>read</code><sup id="fnref:1"><a href="#fn:1" rel="footnote">5</a></sup>,
|
||||||
but if something goes wrong the returned value is <code>Nothing</code>.
|
but if something goes wrong the returned value is <code>Nothing</code>.
|
||||||
If the value is right, it returns <code>Just <the value></code>.
|
If the value is right, it returns <code>Just <the value></code>.
|
||||||
Don’t try to understand too much of this function.
|
Don’t try to understand too much of this function.
|
||||||
|
@ -2647,10 +2642,10 @@ It’s type is then something like:</p>
|
||||||
|
|
||||||
<p>Not all function could have access to this variable.
|
<p>Not all function could have access to this variable.
|
||||||
Those who have access to this variable can potentially be impure.
|
Those who have access to this variable can potentially be impure.
|
||||||
Functions whose the world variable isn’t provided to should be pure<sup id="fnref:032001"><a href="#fn:032001" rel="footnote">5</a></sup>.</p>
|
Functions whose the world variable isn’t provided to should be pure<sup id="fnref:032001"><a href="#fn:032001" rel="footnote">6</a></sup>.</p>
|
||||||
|
|
||||||
<p>Haskell consider the state of the world is an input variable for <code>main</code>.
|
<p>Haskell consider the state of the world is an input variable for <code>main</code>.
|
||||||
But the real type of main is closer to this one<sup id="fnref:032002"><a href="#fn:032002" rel="footnote">6</a></sup>:</p>
|
But the real type of main is closer to this one<sup id="fnref:032002"><a href="#fn:032002" rel="footnote">7</a></sup>:</p>
|
||||||
|
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
<span class="Entity">main</span> :: <span class="Constant">World</span> -> ((),<span class="Constant">World</span>)
|
<span class="Entity">main</span> :: <span class="Constant">World</span> -> ((),<span class="Constant">World</span>)
|
||||||
|
@ -2750,7 +2745,7 @@ Each line is of the form:</p>
|
||||||
|
|
||||||
<p>Even if for some line the first <code>x</code> argument isn’t needed.
|
<p>Even if for some line the first <code>x</code> argument isn’t needed.
|
||||||
The output type is a couple, <code>(answer, newWorldValue)</code>.
|
The output type is a couple, <code>(answer, newWorldValue)</code>.
|
||||||
Each function <code>f</code> must have a type of kind:</p>
|
Each function <code>f</code> must have a type similar to:</p>
|
||||||
|
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
<span class="Entity">f</span> :: <span class="Constant">World</span> -> (<span class="Variable">a,World</span>)
|
<span class="Entity">f</span> :: <span class="Constant">World</span> -> (<span class="Variable">a,World</span>)
|
||||||
|
@ -2784,7 +2779,7 @@ And in particular, each action can take a parameter from the result of a line ab
|
||||||
<p>IMPORTANT, there are only two important pattern for us:</p>
|
<p>IMPORTANT, there are only two important pattern for us:</p>
|
||||||
|
|
||||||
<pre><code>let (x,w1) = action1 w0 in
|
<pre><code>let (x,w1) = action1 w0 in
|
||||||
let (y,w2) - action2 w1 in
|
let (y,w2) = action2 w1 in
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
<p>and</p>
|
<p>and</p>
|
||||||
|
@ -2921,9 +2916,9 @@ Haskell has made a syntactical sugar for us:</p>
|
||||||
|
|
||||||
<pre class="twilight">
|
<pre class="twilight">
|
||||||
<span class="Keyword">do</span>
|
<span class="Keyword">do</span>
|
||||||
y <- action1
|
x <- action1
|
||||||
z <- action2
|
y <- action2
|
||||||
t <- action3
|
z <- action3
|
||||||
...
|
...
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
@ -2947,7 +2942,8 @@ blindBind action1 action2 w0 =
|
||||||
bind action (\_ -> action2) w0
|
bind action (\_ -> action2) w0
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>I didn’t curried this definition for clarity purpose. Of course we can use a better notation, we’ll use the <code>(>>)</code> operator.</p>
|
<p>I didn’t simplified this definition for clarity purpose.
|
||||||
|
Of course we can use a better notation, we’ll use the <code>(>>)</code> operator.</p>
|
||||||
|
|
||||||
<p>And</p>
|
<p>And</p>
|
||||||
|
|
||||||
|
@ -3278,7 +3274,7 @@ In particular, monad are very useful for: </p>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<p>If you have followed me until here, then you’ve done it!
|
<p>If you have followed me until here, then you’ve done it!
|
||||||
You know monads<sup id="fnref:03021301"><a href="#fn:03021301" rel="footnote">7</a></sup>!</p>
|
You know monads<sup id="fnref:03021301"><a href="#fn:03021301" rel="footnote">8</a></sup>!</p>
|
||||||
|
|
||||||
<p><a href="code/03_Hell/02_Monads/13_Monads.lhs" class="cut">03_Hell/02_Monads/<strong>13_Monads.lhs</strong> </a></p>
|
<p><a href="code/03_Hell/02_Monads/13_Monads.lhs" class="cut">03_Hell/02_Monads/<strong>13_Monads.lhs</strong> </a></p>
|
||||||
|
|
||||||
|
@ -3635,6 +3631,9 @@ treeFromList' (x:xs) n = <span class="Constant">Node</span> x left right
|
||||||
<li id="fn:2">
|
<li id="fn:2">
|
||||||
<p>I know I cheat. But I will talk about non-strict later.<a href="#fnref:2" rel="reference">↩</a></p>
|
<p>I know I cheat. But I will talk about non-strict later.<a href="#fnref:2" rel="reference">↩</a></p>
|
||||||
</li>
|
</li>
|
||||||
|
<li id="fn:021301">
|
||||||
|
<p>For the brave, a more complete explanation of pattern matching can be found <a href="http://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/patterns.html">here</a>.<a href="#fnref:021301" rel="reference">↩</a></p>
|
||||||
|
</li>
|
||||||
<li id="fn:0216">
|
<li id="fn:0216">
|
||||||
<p>You should remark <code>squareEvenSum''</code> is more efficient that the two other versions. The order of <code>(.)</code> is important.<a href="#fnref:0216" rel="reference">↩</a></p>
|
<p>You should remark <code>squareEvenSum''</code> is more efficient that the two other versions. The order of <code>(.)</code> is important.<a href="#fnref:0216" rel="reference">↩</a></p>
|
||||||
</li>
|
</li>
|
||||||
|
|
Loading…
Reference in a new issue