-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_5.cpp
42 lines (36 loc) · 975 Bytes
/
problem_5.cpp
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
// 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.
// Example
// auto cons_result = cons(3, 4);
// auto car_result = car(cons_result);
// auto cdr_result = cdr(cons_result);
#include <functional>
using namespace std;
function<int (const function<int(const int, const int)>)> cons(const int& a, const int& b)
{
return [&](function<int(const int, const int)> f)
{
return f(a, b);
};
}
int car(const function<int(const function<int(const int, const int)>)> f)
{
return f([](const int arg1, const int arg2)
{
return arg1;
});
}
int cdr(const function<int(const function<int(const int, const int)>)> f)
{
return f([](const int arg1, const int arg2)
{
return arg2;
});
}