Skip to content

Commit

Permalink
Added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Sep 11, 2023
1 parent 0aca841 commit 1d2189c
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions docs/ml.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,52 +41,80 @@
## Immutable "variables"

```
let x = 42
printf "x=%d" x
x <- x + 1
printf "x=%d" x
```

---

## Mutability is ugly

```
let mutable x = 42
printf "x=%d" x
x <- x + 1
printf "x=%d" x
```

---

## ML type inference

```
let x = 42
```

---

## Functions

```
let inc x = x + 1
```

---

## Polymorphic functions

```
let inc x:int = x + 1
```

---

## Better example
## Polymorphic functions

```
let ident x = x;;
Printf.printf "ident=%d\n" (ident 10);;
Printf.printf "ident=%s\n" (ident "foo");;
```

---

## Pattern matching

```
let rec fib = function
0 -> 0
| 1 -> 1
| n -> fib (n-1) + fib (n-2)
fib 10
```
---

## Pattern matching

```
let rec length([]) = 0
| length(head::tail) = 1 + length(tail);
```

```
let rec length([]) = 0
| length(_::tail) = 1 + length(tail);
```

0 comments on commit 1d2189c

Please sign in to comment.