Haskell like (c:cs) syntax
Erik Jones
erik at myemma.com
Tue Aug 28 18:30:47 EDT 2007
On Aug 28, 2007, at 5:12 PM, Chris Mellon wrote:
> On 8/28/07, Stefan Niemann <stefan at 3niemann.de> wrote:
>> Hi,
>>
>> sorry that I'm relatively new to Python. But the syntax and
>> semantics of
>> Python already fascinate me, because I'm familiar with functional
>> languages
>> like Haskell.
>>
>> 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.
>>
>> Regards,
>> Stefan
>>
>>
>
> Python does not have haskell like pattern matching. Things are written
> and done in a different way.
>
> When working with lists, Python has a slice syntax (which is rather
> more powerful than Haskells limited head->tail linked list syntax)
> that you can use to chop a sequence up into various parts.
That is extremely arguable (in fact, Haskell's list semantics are
extremely powerful as they are not limited to just head/tail). But,
rather than debate the merits of one language over the other, to the
OP: no, Python doesn't have any pattern matching facilities.
Binding statements must be explicit, so you could do something along
the lines of (using parallel assignment):
head, tail = l[0], l[1:]
or,
front, last = l[:len(l) - 1], l[len(l) - 1]
Erik Jones
Software Developer | Emma®
erik at myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)
Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com
More information about the Python-list
mailing list