-
Notifications
You must be signed in to change notification settings - Fork 5
/
contract-exp.sls
71 lines (51 loc) · 1.55 KB
/
contract-exp.sls
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
#!r6rs
(library (mpl contract-exp)
(export contract-exp)
(import (mpl rnrs-sans)
(mpl misc)
(mpl arithmetic)
(mpl exp)
(mpl expand-main-op))
(define (contract-exp-rules u)
(let ((v (expand-main-op u)))
(cond ( (power? v)
(let ((b (list-ref v 1))
(s (list-ref v 2)))
(if (exp? b)
(let ((p (* (list-ref b 1) s)))
(if (or (product? p)
(power? p))
(exp (contract-exp-rules p))
(exp p)))
v)) )
( (product? v)
(let ((p 1)
(s 0))
(for-each
(lambda (y)
(if (exp? y)
(set! s (+ s (list-ref y 1)))
(set! p (* p y))))
(cdr v))
(* (exp s) p)) )
( (sum? v)
(let ((s 0))
(for-each
(lambda (y)
(if (or (product? y)
(power? y))
(set! s (+ s (contract-exp-rules y)))
(set! s (+ s y))))
(cdr v))
s) )
( else v ))))
(define (contract-exp u)
(if (or (number? u)
(symbol? u))
u
(let ((v (map contract-exp u)))
(if (or (product? v)
(power? v))
(contract-exp-rules v)
v))))
)