-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
59 lines (49 loc) · 1.07 KB
/
test.php
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
<?php
include_once __DIR__ . "/SimpleLisp.php";
use \SimpleLisp\SimpleLisp;
$fib = '
(let
(
(nth 9)
(fib
(lambda (n)
(if (< n 2) n (+
(fib (- n 1)) (fib (- n 2))
)
)
)
)
)
(fib nth)
)
';
//f0 0 f1 1
//f2 1 f3 2
//f4 3 f5 5
//f6 8 f7 13
//f8 21 f9 34
$tokenList = SimpleLisp::tokenize($fib);
$ast = SimpleLisp::createAst($tokenList);
var_dump(SimpleLisp::interpret($ast));
//output float(34)
//1+2+3+...+100
$sum100 = '
(let
(
(nth 100)
(sum100
(lambda (acc i max)
(if (> i max) acc (sum100
(+ acc i) (+ i 1) max
)
)
)
)
)
(sum100 0 1 nth)
)
';
$tokenList = SimpleLisp::tokenize($sum100);
$ast = SimpleLisp::createAst($tokenList);
var_dump(SimpleLisp::interpret($ast));
//output float(5050)