Haskell like (c:cs) syntax

Stefan Niemann stefan at 3niemann.de
Wed Aug 29 16:53:16 EDT 2007


Thanks for all the good answers.

In fact the `Extended Iterable Unpacking' is exactly what I was looking for. 
Ok, my main aspect of writing

    head, *tail = seq

instead of

    head, tail = seq[0], seq[1:]

is the syntactic sugar. As mentioned in the PEP this may also be faster when 
iterables are involved.

Stefan

"Matimus" <mccredie at gmail.com> schrieb im Newsbeitrag 
news:1188349012.502720.296440 at x40g2000prg.googlegroups.com...
>> 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