Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Jochen Schulz
haskell_labcourse
Commits
3216ae96
Commit
3216ae96
authored
Mar 15, 2016
by
Christoph Ruegge
Browse files
Syntax fix
parent
6ddf7bd3
Changes
2
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
3216ae96
...
...
@@ -32,7 +32,6 @@ A lecture to teach Haskell in a 2 weeks block lab course.
*
[
26 continous optimization
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/26
continous optimization.ipynb)
*
[
27 inverse
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/27
inverse.ipynb)
*
[
28 minsurf
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/28
minsurf.ipynb)
*
[
Untitled
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/Untitled.ipynb
)
*
[
general software management
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/general
software management.ipynb)
*
[
shortest path
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/shortest
path.ipynb)
...
...
lecture/06 syntactic sugar.ipynb
View file @
3216ae96
%% Cell type:markdown id: tags:
# Syntactic Sugar (Guards, where, let)
## Indentation of code blocks
code block are defined by lines with identical indentation
-
convention: indent by multiples of 4 spaces
-
curly braces and semicolons to delimit blocks are possible, but should be avoided
Example:
%% Cell type:code id: tags:
```
haskell
:
option
no
-
lint
doubleIfEven
x
=
if
even
x
then
x
*
2
else
x
```
%% Cell type:markdown id: tags:
## Guards
*syntactic sugar*
for
`if cond then ex else exDefault`
```
haskell
function
|
cond1
->
ex1
|
cond2
->
ex2
|
cond3
->
ex3
|
otherwise
->
exDefault
function
patterns
|
cond1
=
ex1
|
cond2
=
ex2
|
cond3
=
ex3
|
otherwise
=
exDefault
```
-
for complete definition:
`otherwise`
as final guard (defined as
`True`
)
Example:
%% Cell type:code id: tags:
```
haskell
volumeTell
length
width
height
=
if
length
*
width
*
height
<=
0.0048
then
"up to a classic lego brick"
else
if
length
*
width
*
height
<=
4.237
then
"up to a football"
else
if
length
*
width
*
height
<=
66.4
then
"up to the size of an average human"
else
"all the things (bigger than humans)"
```
%% Cell type:code id: tags:
```
haskell
volumeTell
::
(
RealFloat
a
)
=>
a
->
a
->
a
->
String
volumeTell
length
width
height
|
length
*
width
*
height
<=
0.0048
=
"up to a classic lego brick"
|
length
*
width
*
height
<=
4.237
=
"up to a football"
|
length
*
width
*
height
<=
66.4
=
"up to the size of an average human"
|
otherwise
=
"all the things (bigger than humans)"
```
%% Cell type:markdown id: tags:
## where
Variable binding using
`where`
(mathematical notation)
```
haskell
somecode
where
variable
=
definition
```
-
`where`
is a syntactical construct
-
The binding is visible in the entire block or function
`somecode`
*Example*
: Previous example using
`where`
instead duplication of code and computation
%% Cell type:code id: tags:
```
haskell
volumeTell
::
(
RealFloat
a
)
=>
a
->
a
->
a
->
String
volumeTell
length
width
height
|
volume
<=
0.0048
=
"up to a classic lego brick"
|
volume
<=
4.237
=
"up to a football"
|
volume
<=
66.4
=
"up to the size of an average human"
|
otherwise
=
"all the things (bigger than humans)"
where
volume
=
length
*
width
*
height
```
%% Cell type:markdown id: tags:
*Example*
: Multiple variables (and pattern matching)
%% Cell type:code id: tags:
```
haskell
volumeTell
::
(
RealFloat
a
)
=>
a
->
a
->
a
->
String
volumeTell
length
width
height
|
volume
<=
legobrick
=
"up to a classic lego brick"
|
volume
<=
football
=
"up to a football"
|
volume
<=
human
=
"up to the size of an average human"
|
otherwise
=
"all the things (bigger than humans)"
where
volume
=
length
*
width
*
height
(
legobrick
,
football
,
human
)
=
(
0.0048
,
4.237
,
66.4
)
```
%% Cell type:markdown id: tags:
## let
Definition / binding of variables inside expression
```
haskell
let
variable
=
definition
in
expression
```
Similar to
`where`
, but
-
`let`
is an expression, usable everywhere,
-
the binding is visible locally inside the expression,
-
`let`
does not work for guards.
*Example*
:
%% Cell type:code id: tags:
```
haskell
let
double
x
=
x
+
x
in
double
3
```
%%%% Output: display_data
%% Cell type:code id: tags:
```
haskell
[
let
square
x
=
x
*
x
in
(
square
5
,
square
3
,
square
2
)]
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
Example with two variables:
%% Cell type:code id: tags:
```
haskell
cylinder
::
(
RealFloat
a
)
=>
a
->
a
->
a
cylinder
r
h
=
let
sideArea
=
2
*
pi
*
r
*
h
topArea
=
pi
*
r
^
2
in
sideArea
+
2
*
topArea
```
%% Cell type:code id: tags:
```
haskell
cylinder
r
h
=
sideArea
+
2
*
topArea
where
sideArea
=
2
*
pi
*
r
*
h
topArea
=
pi
*
r
^
2
```
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment