-
Notifications
You must be signed in to change notification settings - Fork 0
/
solutions-c7.js
112 lines (110 loc) · 2.9 KB
/
solutions-c7.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
111
//Chapter SEven
lilJSter = _ = extendAndWrap(lilJSter, {
isSet: function (l) {
return isNull(l) ? true :
_.isMember(car(l),cdr(l)) ? false :
/*ELSE*/ _.isSet(cdr(l));
},
makeset : function (l) {
return isNull (l) ? l :
_.isMember(car(l), cdr(l)) ? _.makeset(cdr(l)) :
/*ELSE*/ cons(car(l), _.makeset(cdr(l)));
},
makeset2 : function (l) {
return isNull (l) ? l :
/*Else*/cons( car(l),
_.makeset2( _.multirember( car(l),
cdr(l))));
},
isSubset : function (s1,s2) {
return isNull (s1) ? true :
_.isMember(car(s1),s2) ? _.isSubset(cdr(s1),s2) :
/*Else*/ false;
},
isSubset2 : function (s1,s2) {
return isNull (s1) ? true :
_.isMember(car(s1),s2) && _.isSubset(cdr(s1),s2);
},
isEqset: function (s1,s2) { // XXX: review
return isNull (s1) ? true :
_.isSubset(s1,s2) && _.isSubset(s2,s1) ;
},
isIntersect: function (s1,s2) {
return isNull (s1) ? false :
_.isMember(car(s1),s2) ? true :
/*else*/ _.isIntersect(cdr(s1),s2);
},
isIntersect2: function (s1,s2) {
return isNull (s1) ? false :
_.isMember(car(s1),s2) || _.isIntersect(cdr(s1),s2);
},
intersect : function (s1, s2) {
return isNull (s1) ? null :
_.isMember(car(s1), s2) ? cons( car(s1),
_.intersect( cdr(s1), s2)) :
/*ELSE*/ _.intersect(cdr(s1),s2);
},
union : function (s1, s2) {
return isNull (s1) ? s2 :
_.isMember(car(s1),s2) ?
_.union(cdr(s1),s2) :
/*ELSE*/ cons(car(s1), _.union(cdr(s1), s2));
},
difference : function (s1, s2) {
return isNull (s1) ? null :
_.isMember(car(s1),s2) ?
_.difference(cdr(s1),s2) :
/*else*/ cons(car(s1), _.difference(cdr(s1),s2));
},
intersectall: function (lset) {
return isNull (cdr(lset)) ? car(lset) :
_.intersect(car(lset), _.intersectall(cdr(lset)));
},
isaPair : function (x) {
return isAtom(x) ? false :
isNull (x) ? false :
isNull(cdr(x)) ? false :
isNull(cdr(cdr(x))) ? true :
/*Else*/ false;
},
first : function (l) {
return car(l);
},
second : function (l) {
return car(cdr(l));
},
build : function (s1,s2) {
return cons( s1,
cons(s2, null));
},
third : function (l) {
return car(cdr(cdr(l)));
},
isFun : function (r) {
return _.isSet(_.firsts(r));
},
revrel : function (r) {
return isNull(r) ? null :
cons( _.build( _.second(car(r)),
_.first(car(r))),
_.revrel(cdr(r)))
},
revpair : function (p) {
return _.build(_.second(p),_.first(p));
},
revrel2 : function (r) {
return isNull(r) ? null :
cons( _.revpair(car(r)),
_.revrel2(cdr(r)));
},
isFullful : function (r) {
//"seconds" is defined through wishful thinking (see SICP lecture 2A)
//which means using a procedure or function before it was defined hope
//that it will be.
return _.isSet(_.seconds(r));
},
seconds : function (l) {
return isNull(l) ? null :
cons(_.second(car(l)), _.seconds(cdr(l))); //Now, "seconds" is defined
}
});