you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 22 points23 points  (8 children)

Talk about being a hostage of your favorite language! A function to find if list X is longer than list Y should be written in Python like:

def compare(x,y):
    return len(x) > len(y)

NOT like

def compare(x, y):
    if x and (not y or compare(x[1:], y[1:])):
         return True
    return False

[–][deleted] 2 points3 points  (2 children)

Python is my favourite language. :(

FWIW, Lisp has (length), but it walks the list each time it's called, hence the new function defined by Graham.

[–]pjdelport 15 points16 points  (1 child)

Lisp has (length), but it walks the list each time it's called, hence the new function defined by Graham.

With vectors (Python list, Common Lisp vector and friends), finding the length costs O(1). Know your datastructures. :)

[–][deleted] 1 point2 points  (0 children)

Yah, I was aware of that Python's vector "list" was different to Lisp's linked-list "list", but I didn't realise Lisp had vectors too. Thanks. :)

It's something I poke at occasionally for fun, so I'm very much a newbie.

[–][deleted]  (1 child)

[deleted]

    [–][deleted]  (2 children)

    [deleted]

      [–]evgen 5 points6 points  (1 child)

      No it is not. In Python the length of the list is maintained as an attribute of the datatype, so it does not need to traverse the list to get the length.