-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairs.js
43 lines (39 loc) · 799 Bytes
/
pairs.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
function cons(a,b) {
return function (selector) {
if (selector=='areyoualist?')
return 'yesIam';
return selector(a,b);
};
}
function car(list) {
function carHelper(a,b) {
return a;
}
return list(carHelper);
}
function cdr(list) {
function cdrHelper(a,b) {
return b;
}
return list(cdrHelper);
}
function isList(thing) {
if (typeof(thing)!='function')
return false;
try {
if (thing('areyoualist?')=='yesIam')
return true;
} catch(e) {
}
return false;
}
function show(list) {
var sval;
if (list==null)
sval = '()';
else if (isList(list))
sval = '('+ show(car(list)) +' '+show(cdr(list))+')';
else
sval = String(list);
return sval;
}