Haskell like (c:cs) syntax

Ricardo Aráoz ricaraoz at gmail.com
Tue Aug 28 18:24:47 EDT 2007


Stefan Niemann 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
> 
> 
> 

L = ['one', 'two', 'three', 'four', 'five']

print L[0]    # This would be 'head'
print L[1:]   # This would be 'tail'

Caution : L[0] and L[1:] are COPIES of the head and tail of the list.




More information about the Python-list mailing list