Created
August 9, 2015 14:58
-
-
Save zelark/0319b1c2a65188d6932d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Возвращает замкнутое лямбда-выражение | |
# в котором n - свазянная переменная, | |
# а x и y - свободные, захваченные из внешнего контекста. | |
def make_pair(x, y): | |
"""Создаёт пару из двух элементов x и y.""" | |
return lambda n: x if n == 0 else y | |
# API: | |
def head(pair): | |
"""Возвращает голову пары, первый элемент.""" | |
return pair(0) | |
def tail(pair): | |
"""Возвращает хвост пары, второй элемент.""" | |
return pair(1) | |
# Пример использования: | |
pair = make_pair(4, 2) | |
print(head(pair)) | |
print(tail(pair)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment