-
Notifications
You must be signed in to change notification settings - Fork 4
/
scott.lam
55 lines (45 loc) · 1.63 KB
/
scott.lam
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
51
52
53
54
55
//reference:
// J.M. Jansen, R. Plasmeijer, and P. Koopman,
// Functional Pearl: Comprehensive Encoding of Data Types and Algorithms in the lambda-Calculus,
// http://www.nlda-tw.nl/janmartin/papers/jmjansenLambdapaper.pdf
#define U (\x x x)
#define UU (U U)
// data Boolean = True | False
#define false (\t \f f)
#define true (\t \f t)
// data Nat = Zero | Suc Nat
#define zero (\f \g f)
#define suc (\n \f \g g n)
#define pred (\n n UU (\m m))
// recursive functions
// add Zero m = m
// add (Suc n) m = Suc (add n m)
#define add (\Add \n \m n m (\n Suc (Add Add n m)))
// mutually recursive functions
// isOdd Zero = False
// isOdd (Suc n) = isEven n
// isEven Zero = True
// isEven (Suc n) = isOdd n
#define isOdd (\IsOdd \IsEven \n n false (\n IsEven IsOdd IsEven n))
#define isEven (\IsOdd \IsEven \n n true (\n IsOdd IsOdd IsEven n))
// data List t = Nil | Cons t (List t)
#define nil (\f \g f)
#define cons (\h \t \g g h t)
#define head (\s s UU (\h \t h))
#define tail (\s s UU (\h \t t))
// Eratosthenes prime sieve
// nat n = Cons n (nats (Suc n))
#define nats (\Nats \n Cons n (Nats Nats (Suc n)))
// sieve (Cons Zero xs) = sieve xs
// sieve (Cons (Suc k) xs) = Cons (Suc k) (sieve (rem k k xs))
#define sieve (\Sieve \s s (\h \t h (Sieve Sieve t) (\k Cons h (Sieve Sieve (Rem Rem k k t)))))
// rem p Zero (Cons x xs) = Cons Zero (rem p p xs))
// rem p (Suc k) (Cons x xs) = Cons x (rem p k xs))
#define rem (\Rem \p \k \s s (\h \t k (Cons Zero (Rem Rem p p t)) (\k Cons h (Rem Rem p k t))))
(\Zero \Suc \Cons
(\Rem \Nats
(\Sieve
Sieve Sieve (Nats Nats (Suc (Suc Zero))))
sieve)
rem nats)
zero suc cons