dive into mark

You are here: dive into markArchivesApril 2003My other car is a cdr

Thursday, April 10, 2003

My other car is a cdr

Found in 2-year old code:

def car(t): return t[0]
def cdr(t): return t[1]

Good Lord, what was I thinking?

Filed under

13 comments

  1. Trackback by Advanced Combo Tricks
  2. Yeah, totally! It obviously should have read

    def cdr(t): return t[1:]

    :-)

    Comment by Gordo — Thursday, April 10, 2003 @ 4:00 pm

  3. I detect signs of a previous lisp obsession.

    Comment by Simon Willison — Thursday, April 10, 2003 @ 4:01 pm

  4. Funny.. I’m just now starting to learn Lisp.

    In my newbie-dom, Gordo seems right.

    Comment by Jeremy Dunck — Thursday, April 10, 2003 @ 5:26 pm

  5. It depends on if you see car and cdr as operating on
    lists or cells. The code is correct if you define:

    def cons(a, b):
    ….return [a, b]

    Comment by Neil Schemenauer — Thursday, April 10, 2003 @ 5:43 pm

  6. I think you need more parentheses…

    Comment by Bruce — Thursday, April 10, 2003 @ 7:55 pm

  7. Mark,

    Consider placing all the entries in your mobile edition on one page instead of making a list of links to really short one-entry pages. This would help those of us who look forward to reading you on the long, lonely train rides.

    - Bo

    Comment by Bo — Thursday, April 10, 2003 @ 8:07 pm

  8. Gordo nailed it. If I remember my Scheme correctly, cdr should always return a list. The code never actually used cdr (although it did use car). I had just added cdr to be cute, and yesterday I realized (two years later) that I had been cute and wrong. Which is infinitely less satisfying than being cute and right.

    Comment by Mark — Friday, April 11, 2003 @ 9:20 am

  9. Bo, you should look into Plucker. Its default spidering depth of 2 will grab the entire mobile edition in one shot and wrap it up as a PDB for you. I’ve only used it under emulation, but several of my readers swear by it.

    Comment by Mark — Friday, April 11, 2003 @ 9:21 am

  10. OK, I just fixed the code, and added this:

    def cadr(t): return t[1] # t[1:][0]

    And it wasn’t just for the heck of it; I’m actually using it. So I’m not just putting the car(t) before the hors(e).

    Comment by Mark — Friday, April 11, 2003 @ 12:57 pm

  11. Ack.

    Comment by Jeremy Dunck — Friday, April 11, 2003 @ 4:28 pm

  12. A core semantic of CAR, CDR is that they return
    a *reference* to parts of the original list, not values.

    car(t) and cdr(t) as defined return *copies* of first and last part of t. Which means that if I modify cdr(t), t is *not* modified. In Lisp, modifying (CDR t) would modify t.

    Comment by Phil — Tuesday, April 15, 2003 @ 12:25 pm

  13. Phil: AFAIK, there’s no way to do that in Python.

    Comment by Mark — Tuesday, April 15, 2003 @ 4:07 pm

Respond privately

I am no longer accepting public comments on this post, but you can use this form to contact me privately. (Your message will not be published.)



Recent Stuff For You, Special Price Stay Here
  • Greasemonkey Hacks
Good Stuff Buy The Cow Go Away
Dive Into Python
Powered by Google Drink The Milk Don't Steal

 

posts / comments
© 2001-8 Mark Pilgrim