Haskell like (c:cs) syntax

Matimus mccredie at gmail.com
Tue Aug 28 20:56:52 EDT 2007


> Is there a pattern matching construct in Python like (head : tail), meaning
> 'head' matches the first element of a list and 'tail' matches the rest? I
> could not find this in the Python documentation.

Not really, but you could do something like this:

[code]
def foo(head, *tail):
    #do stuff with head and tail

foo(*seq)
[/code]

Also, Python 3.0 will have `Extended Iterable Unpacking'
http://www.python.org/dev/peps/pep-3132/

This isn't quite the same as Haskell's type matching, but would enable
similar behavior in some cases.

example:
[code]
head, *tail = seq
[/code]

Which would assign the first element of seq to head, and the remainder
to tail.

Matt




More information about the Python-list mailing list