Skip to content
Snippets Groups Projects
Commit 185bc646 authored by Nicolas Lenz's avatar Nicolas Lenz :snowflake:
Browse files

Various improvements

parent 37b5bcc7
No related branches found
No related tags found
No related merge requests found
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Make",
"type": "shell",
"command": "make",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
\ No newline at end of file
......@@ -22,7 +22,7 @@
- `:quit` / `:q`: Close GHCi
- `:help` / `:h`/`:?`: Get a list of available commands
**Make sure you can open a file in GHCI!**
**Make sure you can open a file in GHCi!**
## Compiling
......
......@@ -3,26 +3,27 @@
## The Difference
- *Imperative* vs. *Functional*
- Programming lanuages like Java or C are imperative
- Imperative: program is list of commands to be executed one after another
- Programming lanuages like Java or C are **imperative**
- Program = sequence of commands to be executed one after another
```
x = 3
do_thing(x)
x = x + 4
y = 2
do_another_thing(x, y)
x = 2
doThing(x)
x = 4
doThing(x)
```
Works without problem.
## Functional
- Different approach
- In functional programming a program is no list of commands
- More a *collection of definitions*
- In **functional** programming a program is **not** a list of commands
- Program = *collection of definitions*
```haskell
x = 4
x = 2
x = 4
```
What will happen?
Will *this* work?
\ No newline at end of file
# First Functions
Let's take a look at some predefined basic functions, how to use them and learn how we can define our first own functions.
## Defining Values
`x = 42` defines the variable `x` to hold the value `42`.
Any variable can only be defined *once*[^ghci_multiple]!
We can also use truth values: `True` and `False` (*capitalized!*)
[^ghci_multiple]: Except in GHCi where defining a value again overwrites previous definitions for convenience
## Basic Calculation
`x = 1` defines `x` to hold the value `1` as already seen.
Basic calculations work as expected
......@@ -15,8 +26,6 @@ Basic calculations work as expected
## Truth values
Truth values: `True` and `False` (*capitalized!*)
- `True && False`
- `True || False`
- `not False`
......@@ -30,7 +39,9 @@ Comparisons also work as expected.
- `1 >= 10`
- `2 /= 2` (note that not `!=`)
## First Functions
## Define our own
Let's define some functions of our own.
```haskell
double x = x * 2
......
......@@ -11,7 +11,9 @@
- `Bool`: Truth values (`True`, `False`) (*capitalized!*)
- `Char`: Single characters (`'a'`, `'c'`)
- `String`: Text (`"Hello"`, `"Cheese sandwich"`)
- `a -> b`: Function with the
- `a -> b`: Function with
- the input type (*domain*) `a`
- the output type (*codomain* or *range*) `b`
## Specifying a Type
......@@ -29,7 +31,7 @@ double :: Int -> Int
double x = x * 2
```
Recommendation: Always specify the type.
Recommendation: *Always* specify the type.
## Lists
......@@ -50,13 +52,17 @@ empty = []
Lists are a *recursive data structure*
A list is always either
- *an empty list* or
- *an element added in front of another list*.
A list is always one of two things:
Adding an element is done with `:`
- *an empty list:* `[]`
- *an element added in front of another list:* `element : list`
`[1,2,3]` is the same as `1 : (2 : (3 : []))`
These are all the same list:
- `[1, 2, 3]`
- `1 : [2, 3]`
- `1 : (2 : (3 : []))`
- `1 : 2 : 3 : []`
```haskell
moreFruits :: [String]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment