Skip to content

Commit 5ac3c5c

Browse files
committed
Update frontpage examples
1 parent cc5f52f commit 5ac3c5c

13 files changed

+94
-44
lines changed

_examples/1-fact-large.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
description: Functional programming
3+
display: large
4+
---
5+
<!-- This file has to be 15 lines long -->
6+
```erlang
7+
fact(1) -> 1; %% Pattern matching for control-flow
8+
fact(N) -> N * fact(N-1). %% Recursion to create loops
9+
10+
> example:fact(10). %% Interactive shell for fast iterations
11+
3628800
12+
> [{I, example:fact(I)} || I <- lists:seq(1,10)].
13+
[{1, 1}, {2, 2}, {3, 6}, {4, 24}, {5, 120}, {6, 720},
14+
{7, 5040}, {8, 40320}, {9, 362880}, {10, 3628800}]
15+
```

_examples/1-fact.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

_examples/2-fact-small.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
description: Functional programming
3+
display: small
4+
---
5+
<!-- This file has to be 13 lines long -->
6+
```erlang
7+
%% Return factorial for N
8+
fact(1) -> 1;
9+
fact(N) -> N * fact(N-1).
10+
11+
> example:fact(10).
12+
3628800
13+
```

_examples/2-lists.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

_examples/3-lists-large.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
description: Higher-order functions
3+
display: large
4+
---
5+
<!-- This file has to be 15 lines long -->
6+
```erlang
7+
> Fruits = ["banana","monkey","jungle"]. %% Immutable variables
8+
["banana","monkey","jungle"]
9+
> lists:map(fun string:uppercase/1, Fruits). %% Map values using stdlib functions
10+
["BANANA","MONKEY","JUNGLE"]
11+
%% Fold over lists using custom functions
12+
> lists:fold(fun(Str, Cnt) -> string:length(Str) + Cnt end, 0, Fruits).
13+
17
14+
15+
```

_examples/3-processes.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

_examples/4-lists-small.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
description: Higher-order functions
3+
display: small
4+
---
5+
<!-- This file has to be 13 lines long -->
6+
```erlang
7+
> Fruits = ["banana","monkey"].
8+
["banana","monkey"]
9+
> lists:map(
10+
fun string:uppercase/1,
11+
Fruits).
12+
["BANANA","MONKEY"]
13+
```

_examples/5-processes-large.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
description: Light-weigth processes
3+
display: large
4+
---
5+
<!-- This file has to be 15 lines long -->
6+
```erlang
7+
> Parent = self(). %% Get own process id
8+
<0.376.0>
9+
> Child = spawn(fun() -> receive go -> Parent ! lists:seq(1,100) end end).
10+
<0.930.0>
11+
> Child ! go. %% Send message to child
12+
go
13+
> receive Reply -> Reply end. %% Receive response from child
14+
[1,2,3,4,5,6,7,8,9,10,11|...]
15+
```

_examples/5-processes-small.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
description: Light-weigth processes
3+
display: small
4+
---
5+
<!-- This file has to be 13 lines long -->
6+
```erlang
7+
> Me = self().
8+
<0.376.0> %% Send msg using !
9+
> spawn(fun() -> Me!lists:seq(1,10) end).
10+
<0.930.0>
11+
> receive Reply -> Reply end.
12+
[1,2,3,4,5,6,7,8,9,10]
13+
```

_examples/7-even-numbers.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ description: Find even numbers
33
display: small
44
---
55
```erlang
6-
-spec even(list(integer())) ->
7-
list(integer()).
6+
-spec even(In) -> even(Out)
7+
when In :: Out :: list(integer()).
88
even(Numbers) ->
9+
%% Use list comprehensions to map
910
[Number || Number <- Numbers,
1011
Number rem 2 == 0].
1112
```

0 commit comments

Comments
 (0)