How to implement Varient/Tagged Unions/Pattern Matching in Python?
Aahz
aahz at pythoncraft.com
Fri Dec 11 16:48:52 EST 2009
In article <358b227c-d836-4243-b79a-57258590a7a7 at a10g2000pre.googlegroups.com>,
metal <metal29a at gmail.com> wrote:
>
>I want to get pattern matching like OCaml in python(ref:http://
>en.wikipedia.org/wiki/Tagged_union)
>
>I consider the syntax:
>
>def decl():
> def Plus(expr, expr): pass
> def Minus(expr, expr): pass
> def Times(expr, expr): pass
> def Divide(expr, expr): pass
> def Value(str): pass
> @enum
> def expr(Plus, Minus, Times, Divide, Value): pass
> declare_types(locals())
>
>def f(e):
> def _Plus(l, r):
> return '(%s+%s)' % (l, r)
> def _Minus(l, r):
> return '(%s-%s)' % (l, r)
> def _Times(l, r):
> return '(%s*%s)' % (l, r)
> def _Divide(l, r):
> return '(%s/%s)' % (l, r)
> def _Value(x):
> return x
> try:
> match(e, locals()) # visitor pattern
> except NoMatchedError:
> pass
Perhaps you want to define what match() does?
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
The best way to get information on Usenet is not to ask a question, but
to post the wrong information.
More information about the Python-list
mailing list