Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 30fdbb8

Browse files
committed
+MAP
1 parent 6922cde commit 30fdbb8

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

‎lambdas.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,16 @@
119119
)
120120
REVERSE = Y(
121121
lambda f: lambda xs: EMPTY(xs)
122-
(lambda _: xs)
122+
(lambda _: LIST)
123123
(lambda _: APPEND(f(TAIL(xs)))(HEAD(xs)))
124124
(TRUE)
125125
)
126+
MAP = Y(
127+
lambda f: lambda a: lambda xs: EMPTY(xs)
128+
(lambda _: LIST)
129+
(lambda _: PREPEND(f(a)(TAIL(xs)))(a(HEAD(xs))))
130+
(TRUE)
131+
)
126132

127133

128134
# helpers
@@ -134,11 +140,12 @@ def decode_number(f) -> int:
134140

135141
def decode_list(encoded) -> list:
136142
decoded = []
137-
while1:
143+
for_inrange(100):
138144
if EMPTY(encoded) is TRUE:
139145
return decoded
140146
decoded.append(HEAD(encoded))
141147
encoded = TAIL(encoded)
148+
raise RuntimeError('probably infinite list')
142149

143150

144151
# improve repr

‎test_lambdas.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from lambdas import CONS, CAR, CDR
1010
from lambdas import SIGN, UNSIGN, NEG, ISPOS, ISNEG, SADD, SSUB, SMUL
1111
from lambdas import FAC, FIB
12-
from lambdas import LIST, APPEND, PREPEND, REVERSE
12+
from lambdas import LIST, APPEND, PREPEND, REVERSE, MAP
1313
from lambdas import decode_number, decode_list
1414

1515

@@ -306,5 +306,19 @@ def test_reverse(given):
306306
assert decode_list(REVERSE(lst)) == given
307307

308308

309+
@pytest.mark.parametrize('given, expected', [
310+
([], []),
311+
([ONE], [3]),
312+
([ONE, TWO, THREE], [3, 4, 5]),
313+
])
314+
def test_map(given, expected):
315+
lst = LIST
316+
for el in reversed(given):
317+
lst = PREPEND(lst)(el)
318+
result = MAP(ADD(TWO))(lst)
319+
decoded = [decode_number(n) for n in decode_list(result)]
320+
assert decoded == expected
321+
322+
309323
if __name__ == '__main__':
310324
pytest.main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /