add illustration samples

This commit is contained in:
Oli Sturm
2022-07-22 15:00:53 +01:00
parent 0a28574514
commit 250d86bafa
5 changed files with 58 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
data Color = R | B
data RedBlackSet a = E | T Color (RedBlackSet a) a (RedBlackSet a)
balance B (T R (T R a x b) y c) z d = T R (T B a x b) y (T B c z d)
balance B (T R a x (T R b y c)) z d = T R (T B a x b) y (T B c z d)
balance B a x (T R (T R b y c) z d) = T R (T B a x b) y (T B c z d)
balance B a x (T R b y (T R c z d)) = T R (T B a x b) y (T B c z d)
balance color a x b = T color a x b
+14
View File
@@ -0,0 +1,14 @@
% Message passing in Erlang - the "add" and "mult"
% identifiers are "atoms", constant values that are
% used to match incoming messages (which are technically
% tuples).
loop() ->
receive
{add, A, B} ->
io:format("adding: ~p~n", [A + B]),
loop();
{mult, A, B} ->
io:format("multiplying: ~p~n", [A * B]),
loop()
end.
+10
View File
@@ -0,0 +1,10 @@
-- In Haskell, patterns are used to define function overloads.
-- This is a very "static" case.
data Food = Pasta | Pizza | Chips
isEqualFood :: Food -> Food -> Bool
isEqualFood Pasta Pasta = True
isEqualFood Pizza Pizza = True
isEqualFood Chips Chips = True
isEqualFood _ _ = False
+12
View File
@@ -0,0 +1,12 @@
-- This function uses a boolean guard and defines
-- its value only for the remaining cases.
fact :: Int -> Int
fact x | x <= 1 = 1
| otherwise = x * (fact (x - 1))
-- Alternatively, pattern matching can be used for an
-- even more explicit declaration.
fact' :: Int -> Int
fact' 0 = 1
fact' 1 = 1
fact' x = x * (fact (x - 1))
+14
View File
@@ -0,0 +1,14 @@
// In F# the keywoard `match` can be used to run an
// explicit pattern match.
let rec listLength l =
match l with
| [] -> 0
| _ :: xs -> 1 + (listLength xs)
// Alternatively, this special syntax is available
// that removes the explicit parameter and the
// `match` call.
let rec listLength' = function
| [] -> 0
| _ :: xs -> 1 + (listLength' xs)