-
Notifications
You must be signed in to change notification settings - Fork 1
/
5.js
44 lines (40 loc) · 1009 Bytes
/
5.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
/**
*
* This problem was asked by Jane Street.
*
* cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4.
*
* Given this implementation of cons:
*
* def cons(a, b):
* def pair(f):
* return f(a, b)
* return pair
* Implement car and cdr.
*
*/
function solution1() {
function car(pair) {
return pair((car, _cdr) => car);
}
function cdr(pair) {
return pair((_car, cdr) => cdr);
}
return {
car,
cdr
};
}
// given functions
function cons(a, b) {
function pair(f) {
return f(a, b);
}
return pair;
}
function testUsing(solution) {
console.log(`TESTING ${solution.name}`);
console.log(`car(cons(3,4)) -> ${solution().car(cons(3, 4))}`);
console.log(`car(cons(3,4)) -> ${solution().cdr(cons(3, 4))}`);
}
testUsing(solution1);