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
.
Input: {3,4}
Output: The value of a is: 3
The value of b is: 4
Approach:
C++
#include <bits/stdc++.h>using namespace std;function<int(int (*f)(int, int))> cons(int a, int b){auto pair = [a, b](int (*f)(int, int)) -> int {return (*f)(a, b);};return pair;}int car(function<int(int (*f)(int, int))> cons){auto get_a = [](int a, int b) -> int {return a;};return cons(*get_a);}int cdr(function<int(int (*f)(int, int))> cons){auto get_b = [](int a, int b) -> int {return b;};return cons(*get_b);}int main(){function<int(int (*f)(int, int))> cons_v = cons(3, 4);int a = car(cons_v);int b = cdr(cons_v);cout << "The value of a is: " << a << "\n";cout << "The value of b is: " << b << "\n";return 0;}
No comments:
Post a Comment