-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.operat
76 lines (61 loc) · 1.5 KB
/
core.operat
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
($define $umacro
($uvau (name args body)
(~ $define name (~ $uvau args body name)) $umacro))
($define $macro
($uvau (name args body)
(~ $define name (~ $vau args body name)) $macro))
($macro $lambda (args body name)
(wrap (~ $uvau args body) name))
(log (($lambda (n) (* n 2)) 4))
($umacro $function (name args body)
(~ $define name (~ $lambda args body name)))
($macro $cadr (l) ($car ($cdr l)))
($function cadr (l) (car (cdr l)))
($macro $when (
cond ;; whether
body ;; to do
(doc "when this, do that")
)
($if (eval cond)
(eval body)
false))
($macro $cond ((rest ifthens))
($let
((head (car ifthens))
(tail (cdr ifthens)))
($if (eval (car head))
(eval (cadr head))
($if (is tail)
(apply $cond tail)
false))))
($function fact (n)
($if (eq n 0)
1
(* n (fact (- n 1)))))
($function pow (n p)
($if (eq p 0)
1
(* n (pow n (- p 1)))))
;; the slow way!
($function fib (n)
($if (lt n 2)
n
(+ (fib (- n 1)) (fib (- n 2)))))
($logtime (_nativefib 24))
($logtime (fib 24))
(log (concat "cond "
($cond
((gt (fib 5) 150) "bad")
((lt (fib 5) 150) "good")
(true "bad again"))))
($function fibfact (n)
($let
(
(fibn (fib n))
(factn (fact n))
)
(* fibn factn)))
(log "fibfact" (fibfact 10))
(log "run" (run "(+ (fact 5) 7)"))
(log "jsrun" (jsrun "let n = 1; for (let i = 0; i < 10; i++) { n = (n + i) * 2; } return n"))
(log "$compile" ($compile ($if (lt n 2) 1 (* n (fact (- n 1))))))