Questions on Using Python to Teach Data Structures and Algorithms

MonkeeSage MonkeeSage at gmail.com
Thu Sep 28 12:42:36 EDT 2006


Bruno Desthuilliers wrote:
> Brendon Towle wrote:
> > Some of your Lisp translations are subtly off...
>
> Seems correct to me. Lisp lists are linked lists, not arrays.

Actually, Brendon was correct. In lisp / scheme:

(cons 1 '(2 3)) -> (1 2 3)
(car '(1 2 3)) -> 1
(cdr '(1 2 3)) -> (2 3)

But Brendon's code also needs a correction: [a].extend(b) is wrong,
because extend is in-place and returns None, and [a] is anonymous...it
needs to be something like:

def cons(a, b):
  b.insert(0, a)
  return b

Regards,
Jordan




More information about the Python-list mailing list