-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathechallenge.hum
50 lines (44 loc) · 1.16 KB
/
echallenge.hum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#
# Joe Armstrong's Erlang challenge:
# Create a ring of N processes
# Send M simple messages around the ring
# Increase N until out of resources
#
LET countdown_ring_beh(next) = \m.[
SEND m TO next
]
LET countdown_ring_0_beh(first) = \m.[
# SEND m TO println
IF $m = 1 [
BECOME \_.[]
] ELSE [
SEND add(m, -1) TO first
]
]
LET countdown_builder_beh(n) = \(first, m).[
IF $n = 1 [
BECOME countdown_ring_0_beh(first)
SEND m TO first # start message passing phase
] ELSE [
CREATE next WITH countdown_builder_beh(add(n, -1))
BECOME countdown_ring_beh(next)
SEND (first, m) TO next
]
]
# CREATE countdown WITH countdown_builder_beh(123456)
# SEND (countdown, 789) TO countdown
#
# an alternative implementation
# that grows the ring by one process
# for each time a message traverses the ring
#
LET ring_seed_beh(n) = \first.[
CREATE next WITH ring_seed_beh(add(n, 1))
BECOME ring_link_beh(next)
SEND first TO first
]
LET ring_link_beh(next) = \first.[
SEND first TO next
]
# CREATE ring WITH ring_seed_beh(0)
# SEND ring TO ring