Re: Python and Lisp : car and cdr

2011-06-19 Thread Lie Ryan
On 06/18/11 00:45, Franck Ditter wrote: Hi, I'm just wondering about the complexity of some Python operations to mimic Lisp car and cdr in Python... def length(L) : if not L : return 0 return 1 + length(L[1:]) Should I think of the slice L[1:] as (cdr L) ? I mean, is the slice

Re: Python and Lisp : car and cdr

2011-06-19 Thread Ethan Furman
Lie Ryan wrote: On 06/18/11 00:45, Franck Ditter wrote: Hi, I'm just wondering about the complexity of some Python operations to mimic Lisp car and cdr in Python... def length(L) : if not L : return 0 return 1 + length(L[1:]) Should I think of the slice L[1:] as (cdr L) ? I mean

Re: Python and Lisp : car and cdr

2011-06-19 Thread Chris Angelico
On Sun, Jun 19, 2011 at 10:56 PM, Ethan Furman et...@stoneleaf.us wrote: Lie Ryan wrote: def cdr(L):    return L[1] IANAL (I am not a Lisper), but shouldn't that be 'return L[1:]' ? In LISP, a list is a series of two-item units (conses). L = (a, (b, (c, (d, None This represents the

Re: Python and Lisp : car and cdr

2011-06-19 Thread Steven D'Aprano
On Sun, 19 Jun 2011 05:56:27 -0700, Ethan Furman wrote: Lie Ryan wrote: On 06/18/11 00:45, Franck Ditter wrote: Hi, I'm just wondering about the complexity of some Python operations to mimic Lisp car and cdr in Python... def length(L) : if not L : return 0 return 1 + length(L[1

Re: Python and Lisp : car and cdr

2011-06-19 Thread Elias Fotinis
On Sun, 19 Jun 2011 15:56:27 +0300, Ethan Furman et...@stoneleaf.us wrote: Lie Ryan wrote: def length(L): if not L: return 0 return 1 + length(cdr(L)) How is this different from regular ol' 'len' ? It's better, because len() can't overflow the stack. ;) --

Re: Python and Lisp : car and cdr

2011-06-19 Thread Hrvoje Niksic
Ethan Furman et...@stoneleaf.us writes: def car(L): return L[0] def cdr(L): return L[1] IANAL (I am not a Lisper), but shouldn't that be 'return L[1:]' ? Not for the linked list implementation he presented. def length(L): if not L: return 0 return 1 + length(cdr(L))

Re: Python and Lisp : car and cdr

2011-06-19 Thread Ethan Furman
Ethan Furman wrote: IANAL (I am not a Lisper), but shouldn't that be 'return L[1:]' ? Ah, thanks all for the clarification. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list

Re: Python and Lisp : car and cdr

2011-06-19 Thread Terry Reedy
On 6/19/2011 9:24 AM, Steven D'Aprano wrote: No. Each cell in a Lisp-style linked list has exactly two elements, and in Python are usually implemented as nested tuples: (head, tail) # Annoyingly, this is also known as (car, cdr). where head is the data value and tail is either another

Re: Python and Lisp : car and cdr

2011-06-19 Thread Teemu Likonen
* 2011-06-19T12:20:32-04:00 * Terry Reedy wrote: On 6/19/2011 9:24 AM, Steven D'Aprano wrote: No. Each cell in a Lisp-style linked list has exactly two elements, and in Python are usually implemented as nested tuples: (head, tail) # Annoyingly, this is also known as (car, cdr). where head

Re: Python and Lisp : car and cdr

2011-06-18 Thread Nobody
On Fri, 17 Jun 2011 16:45:38 +0200, Franck Ditter wrote: Hi, I'm just wondering about the complexity of some Python operations to mimic Lisp car and cdr in Python... def length(L) : if not L : return 0 return 1 + length(L[1:]) Python's lists are arrays/vectors, not linked lists

Python and Lisp : car and cdr

2011-06-17 Thread Franck Ditter
Hi, I'm just wondering about the complexity of some Python operations to mimic Lisp car and cdr in Python... def length(L) : if not L : return 0 return 1 + length(L[1:]) Should I think of the slice L[1:] as (cdr L) ? I mean, is the slice a copy of a segment of L, or do I actually get

Re: Python and Lisp : car and cdr

2011-06-17 Thread Ian Kelly
On Fri, Jun 17, 2011 at 8:45 AM, Franck Ditter fra...@ditter.org wrote: Hi, I'm just wondering about the complexity of some Python operations to mimic Lisp car and cdr in Python... def length(L) :  if not L : return 0  return 1 + length(L[1:]) Should I think of the slice L[1:] as (cdr L