-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDC__5__.py
87 lines (49 loc) · 1.49 KB
/
DC__5__.py
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
"""
September 22, 2018
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'.
"""
def cons(a, b):
def pair(f):
return f(a, b)
return pair
# TODOS: it's all about functional programming !
def car(f):
def first(a, b):
return a
return f(first)
def cdr(f):
def last(a, b):
return b
return f(last)
""" My first aproach (Wrong !!!)
def f(a, b):
return (a, b)
def cons(a, b):
def pair(f):
return f(a, b)
return pair(f)
def car(cons):
return cons[0]
def cdr(cons):
return cons[1]
"""
"""
========================================================================
=============================== TESTING ================================
========================================================================
"""
print("\n'''=====================<Test 1 - begin>===================='''")
sample_1_ = cons(3, 4)
print("\nsample_1_ =", sample_1_, "\n")
car_ris = car(cons(3, 4))
cdr_ris = cdr(cons(3, 4))
print("car_ris = <", car_ris, ">\ncdr_ris = <", cdr_ris, ">\n")
print("'''=====================<Test 1 - end>======================'''\n")