我刚刚发现pattern matching中的Racket功能非常强大。
> (match '(1 2 3) [(list a b c) (list c b a)])
'(3 2 1)
> (match '(1 2 3) [(list 1 a ...) a])
'(2 3)
> (match '(1 2 3)
[(list 1 a ..3) a]
[_ 'else])
'else
> (match '(1 2 3 4)
[(list 1 a ..3) a]
[_ 'else])
'(2 3 4)
> (match '(1 2 3 4 5)
[(list 1 a ..3 5) a]
[_ 'else])
'(2 3 4)
> (match '(1 (2) (2) (2) 5)
[(list 1 (list a) ..3 5) a]
[_ 'else])
'(2 2 2)
在Python中是否有相似的语法糖或库?
答案 0 :(得分:4)
没有,python的模式匹配只能像这样迭代解压缩:
>>> (x, y) = (1, 2)
>>> print x, y
1 2
或者在函数定义中:
>>> def x((x, y)):
...
或者在python 3中:
>>> x, *y = (1, 2, 3)
>>> print(x)
1
>>> print(y)
[2, 3]
但是有一些external libraries可以实现模式匹配。
答案 1 :(得分:1)
Python没有内置的模式匹配功能,但是有一些Python的库使用统一实现模式匹配。
根据您的第一个示例,可以使用unification库来匹配模式:
from unification import *
a,b,c = var('a'),var('b'),var('c')
matched_pattern = unify([1,2,3],[var('a'),var('b'),var('c')])
if(matched_pattern):
second_pattern = [matched_pattern[c],matched_pattern[b],matched_pattern[a]]
print(second_pattern)
else:
print("The pattern could not be matched.")
答案 2 :(得分:1)
如果您愿意使用基于python的语言(即使它们不是严格意义上的python),那么您可能会喜欢Coconut。
Cocunut为Python增加了一些功能,以进行函数式编程,包括模式匹配。
case [1,2,3]:
match (a,b,c):
print((c,b,a))
# (3,2,1)
case [1,2,3]:
match (1,) + a:
print(a)
# (2, 3)
case [1,2,3]:
match (1,) + a if len(a) == 3:
print(a)
else:
print("else")
# else
case [1,2,3,4]:
match (1,) + a if len(a) == 3:
print(a)
else:
print("else")
# (2,3,4)
case [1,2,3,4,5]:
match (1,) + a + (5,) if len(a) == 3:
print(a)
else:
print("else")
# (2,3,4)
答案 3 :(得分:0)
2020年将至,https://github.com/thautwarm/moshmosh
您可以通过以下方式使用它:
在main.py
import moshmosh
import mypackage
在mypackage
# +pattern-matching
with match([1, [2, (3, 4)]]):
if [a, [2, (_, 4)]]:
print(a) # print 1
也支持IPython。
moshmosh的模式匹配是可扩展的(__match__
用于创建新模式)并且速度快(不应比手写的慢)。