-
Notifications
You must be signed in to change notification settings - Fork 0
/
solutions-c4.js
110 lines (93 loc) · 2.53 KB
/
solutions-c4.js
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
//Chapter Four
lilJSter = _ = extendAndWrap(lilJSter, {
plus: function (n,m) {
return isZero(m) ? n:
add1(plus(n,sub1(m)));
},
minus: function (n,m) {
return isZero(m) ? n:
sub1(minus(n,sub1(m)));
},
addtup : function (t) { //Written using Crockford's primitives. They're more efficient. The rest of the solutions will follow suit.
return isNull(t) ? 0 :
plus(car(t),_.addtup(cdr(t)));
},
star : function (n,m) {
return isZero(m) ? 0:
plus(n,_.star(n,sub1(m)));
},
tupplus : function (t1,t2) { //As in book
return isNull(t1) && isNull(t2) ?
null :
cons(plus(car(t1),car(t2)),
_.tupplus(
cdr(t1),cdr(t2)));
},
tupplusmod : function (t1,t2) {
return isNull(t1) ? t2 :
isNull(t2) ? t1 :
cons(plus(car(t1),car(t2)),
_.tupplusmod(
cdr(t1),cdr(t2)));
},
gt : function (n,m) { //As in book
return isZero(n) ? false :
isZero(m) ? true :
_.gt(sub1(n),sub1(m));
},
lt : function (n,m) { //As in book
return isZero(m) ? false :
isZero(n) ? true :
_.gt(sub1(n),sub1(m));
},
//SKIPPED (= n m) -- Equal Numbers
power : function (n,m){
return isZero(m) ? 1:
_.star(n, _.power(n,sub1(m)));
},
quotient : function (n,m){
return lt(n,m) ? 0:
add1(_.quotient(minus(n,m),m));
},
length : function (l) {
return isNull(l) ? 0 :
add1(_.length(cdr(l)));
},
pick : function (n,l) {
return isZero(sub1(n)) ? car(l) :
_.pick(sub1(n),cdr(l));
},
rempick : function (n,l) {
return isZero(sub1(n)) ? cdr(l):
cons(car(l), _.rempick(sub1(n),cdr(l)));
},
noNums : function (l) {
return isNull(l) ? null :
isNumber(car(l)) ? _.noNums(cdr(l)) :
cons(car(l), _.noNums(cdr(l)));
},
allNums : function (l) {
return isNull(l) ? null :
isNumber(car(l)) ?
cons(car(l), _.allNums(cdr(l))) :
_.allNums(cdr(l));
},
eqan : function (a1,a2) { //NOTE: Recheck XXX
return isNumber(a1) && isNumber(a2) ?
isEqn(a1,a2) :
isNumber(a1) || isNumber(a2) ? false :
isEq(a1,a2);
},
occur : function (a,l) { //Note: Recheck XXX
return isNull(l) ? null :
isEq(car(l),a) ? add1( _.occur(a,cdr(l))) :
_.occur(a,cdr(l));
},
isOne : function(n) {
return isEqn(n, 1);
},
rempickRewrite : function (n,l) {
return _.isOne(n) ? cdr(l):
cons(car(l), _.rempickRewrite(sub1(n),cdr(l)));
}
});