-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.cc
144 lines (142 loc) · 4.05 KB
/
example.cc
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include "crz_lisp.hh"
template<typename T1, typename T2>
void assert_eq() {
static_assert(std::is_same_v<T1, T2>);
}
int main() {
using namespace crz::tmp::lisp;
{
/*
* (define fib
* (lambda (n)
* (letrec
* (
* (iter (lambda (n a b)
* (if (eqv? n 0)
* a
* (iter (- n 1) b (+ a b))))))
* (iter n 0 1))))
*/
using fib = eval<
_<lambda, _<V(n)>,
_<letrec,
_<
_<V(iter), _<lambda, _<V(n), V(a), V(b)>,
_<iff, _<is_eq, n, N(0) >,
a,
_<iter, _<sub, n, N(1) >, b, _<add, a, b>>>>>>,
_<iter, n, N(0), N(1)>>>
>;
// (fib 10)
using expr = eval<_<fib, N(10)>>;
assert_eq<expr, N(55) >();
runtime<expr>::output(std::cout) << std::endl; // 55
}
{
// (map + (list 1 2) (list 3 4) (list 5 6))
using expr = eval<
_<map, add, _<list, N(1), N(2) >, _<list, N(3), N(4) >, _<list, N(5), N(6)>>
>;
assert_eq<expr, _<N(9), N(12)>>();
runtime<expr>::output(std::cout) << std::endl; // (9 12)
}
{
// (reverse '(1 2 #f #t))
using expr = eval<
_<reverse, _<quote, _<N(1), N(2), B(false), B(true)>>>
>;
assert_eq<expr, _<B(true), B(false), N(2), N(1)>>();
runtime<expr>::output(std::cout) << std::endl; // (#t #f 2 1)
}
{
// (interleave (list 1 2) (list 3 4))
using expr = eval<
_<interleave, _<list, N(1), N(2) >, _<list, N(3), N(4)>>
>;
assert_eq<expr, _<N(1), N(3), N(2), N(4)>>();
runtime<expr>::output(std::cout) << std::endl; // (1 3 2 4)
}
{
/* mutual recursive 1
* (letrec
* (
* (even? (lambda (n)
* (if (eqv? n zero)
* #t
* (odd? (sub1 n)))))
* (one 1)
* (odd? (lambda (n)
* (if (eqv? n zero)
* #f
* (even? (sub1 n)))))
* (sub1 (lambda (n) (- n one)))
* (zero (sub1 one)))
* (even? 12))
*/
using expr = eval<
_<letrec,
_<
_<V(is_even), _<lambda, _<V(n)>,
_<iff, _<is_eq, n, V(zero)>,
B(true),
_<V(is_odd), _<V(sub1), n>>>>>,
_<V(one), N(1) >,
_<V(is_odd), _<lambda, _<V(n)>,
_<iff, _<is_eq, n, V(zero)>,
B(false),
_<is_even, _<V(sub1), n>>>>>,
_<V(sub1), _<lambda, _<V(n)>, _<sub, n, one>>>,
_<V(zero), _<sub1, one>>>,
_<is_even, N(12)>>
>;
assert_eq<expr, B(true) >();
runtime<expr>::output(std::cout) << std::endl; // #t
}
{
/* mutual recursive 2
* (letrec
* ((fs (cons
* (lambda (n)
* (if (eqv? n 0)
* #t
* ((cdr fs) (- n 1))))
* (lambda (n)
* (if (eqv? n 0)
* #f
* ((car fs) (- n 1)))))))
* ((car fs) 12))
*/
using expr = eval<
_<letrec,
_<_<V(fs), _<cons,
_<lambda, _<V(n)>,
_<iff, _<is_eq, n, N(0) >,
B(true),
_<_<cdr, fs>, _<sub, n, N(1)>>>>,
_<lambda, _<V(n)>,
_<iff, _<is_eq, n, N(0) >,
B(false),
_<_<car, fs>, _<sub, n, N(1)>>>>>>>,
_<_<car, fs>, N(12)>>
>;
assert_eq<expr, B(true) >();
runtime<expr>::output(std::cout) << std::endl; // #t
}
{
/* dot
* (let
* (
* (head (lambda (head . tail) head))
* (head (list 1 2 #f)))
*/
using expr = eval<
_<let,
_<
_<V(head), _<lambda, _<V(head), dot, V(tail)>, head>>>,
_<head, N(1), N(2), B(false)>>
>;
assert_eq<expr, N(1) >();
runtime<expr>::output(std::cout) << std::endl; // 1
}
}