-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbinary.lam
27 lines (27 loc) · 1.13 KB
/
binary.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
// represent numbers as 8 bits
#define b1 (\t \f f)
#define b0 (\t \f t)
#define Fst b0
#define Snd b1
#define Nil b1
#define C (\x \y \p p x y)
#define Zero (C b0 (C b0 (C b0 (C b0 (C b0 (C b0 (C b0 (C b0 Nil))))))))
#define AddBit (\x \y y (C x b0) (x (C b1 b0) (C b0 b1)))
#define AddBitC ((\addbit \x \y \c c (addbit x y) (x (addbit y c) (y (addbit x c) (C b1 b1)))) AddBit)
#define SuccR \
(\self \x \xs \acc \
(\r \
(\f (C (C (f Fst) (r Fst)) (f Snd))) \
(AddBit x (r Snd))) \
(xs (self self) acc))
#define Succ (\n (\succr n (succr succr) (C Nil b1)) SuccR Fst)
#define AddByteR \
(\self \x \xs \y \ys \acc \
(\r \
(\f (C (C (f Fst) (r Fst)) (f Snd))) \
(AddBitC x y (r Snd))) \
(ys (xs (self self)) acc))
#define AddByte (\x \y (\addbyter y (x (addbyter addbyter)) (C Nil b0)) AddByteR Fst)
#define AddBytesR (\self \x \xs \acc (AddByte x (xs (self self) acc)))
#define AddBytes (\xs (\addbytesr xs (addbytesr addbytesr) Zero) AddBytesR)
\input (C (AddBytes input) Nil)