Re: Haskell like (c:cs) syntax
Ricardo Aráoz ha scritto: 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. This might surprise people who see L[1:] = [], since changing a copy is not supposed to change the original. -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
Erik Jones [EMAIL PROTECTED] wrote: front, last = l[:len(l) - 1], l[len(l) - 1] Normally written as front, last = l[:-1], l[-1] -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
Marco Mariani wrote: Ricardo Aráoz ha scritto: 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. This might surprise people who see L[1:] = [], since changing a copy is not supposed to change the original. Sorry, should have written RETURN copies instead of ARE copies. -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
On Aug 29, 8:12 am, Ricardo Aráoz [EMAIL PROTECTED] wrote: Caution : L[0] and L[1:] are COPIES of the head and tail of the list. Sorry, should have written RETURN copies instead of ARE copies. L[0] does not return a copy, it does what is says, returns the object stored at index 0. i. -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
Istvan Albert wrote: On Aug 29, 8:12 am, Ricardo Aráoz [EMAIL PROTECTED] wrote: Caution : L[0] and L[1:] are COPIES of the head and tail of the list. Sorry, should have written RETURN copies instead of ARE copies. L[0] does not return a copy, it does what is says, returns the object stored at index 0. Well, if we're going to get picky it actually returns a reference to the object referenced by the list item indexed by 0. What was your point? regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden --- Asciimercial -- Get on the web: Blog, lens and tag the Internet Many services currently offer free registration --- Thank You for Reading - -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
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 [EMAIL PROTECTED] schrieb im Newsbeitrag news:[EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
Marco Mariani wrote: Ricardo Aráoz ha scritto: 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. This might surprise people who see L[1:] = [], since changing a copy is not supposed to change the original. That's because slicing and assigning is not the same thing as slicing alone. Slicing and assigning mutates the sequence. Slicing alone returns a copy. L = ['one', 'two', 'three', 'four', 'five'] x = L[1:] # grab a slice x[:] = [] # mutate it x [] L # original list is unchanged ['one', 'two', 'three', 'four', 'five'] -- Erik Max Francis [EMAIL PROTECTED] http://www.alcyone.com/max/ San Jose, CA, USA 37 20 N 121 53 W AIM, Y!M erikmaxfrancis Eternity is very long, especially near the end. -- Woody Allen -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
On Tue, 28 Aug 2007 17:30:47 -0500, Erik Jones wrote: On Aug 28, 2007, at 5:12 PM, Chris Mellon wrote: 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). Apples and oranges, really. Pattern matching can do an awful lot of cool things, but arbitrary slicing isn't one of the more pleasant ones. (At least not without a good set of slice-matching patterns handy, which I don't know if Haskell has.) Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Haskell like (c:cs) syntax
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
On 8/28/07, Stefan Niemann [EMAIL PROTECTED] 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
Stefan Niemann [EMAIL PROTECTED] writes: 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. Python's lists are actually linear arrays. You can subscript them, i.e. x[0] is the first element, and slice them, i.e. x[1:] is all elements after the first. Note that x[1:] actually makes a new array and copies all the elements of x. Normally in Python one writes iterative loops rather than recursing or folding over a list: for a in x: print a so slicing isn't that common. Python also has something called generators which are sort of like Haskell's lazy-evaluation lists. They often make it possible to program in a Haskell-like style. Note however that they are mutable objects, i.e. using g.next() to get the next element of a generator actually updates the generator's internal state. This can lead to a bunch of subtle pitfalls if you're not careful, but overall they are a very useful feature. -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
On Aug 28, 2007, at 5:12 PM, Chris Mellon wrote: On 8/28/07, Stefan Niemann [EMAIL PROTECTED] 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® [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell like (c:cs) syntax
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 -- http://mail.python.org/mailman/listinfo/python-list