Commit 3216ae96 authored by Christoph Ruegge's avatar Christoph Ruegge
Browse files

Syntax fix

parent 6ddf7bd3
......@@ -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)
......
%% 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
```
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment