Found in 2-year old code:
def car(t): return t[0]
def cdr(t): return t[1]
Good Lord, what was I thinking?
You are here: dive into mark → Archives → April 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?
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 |
|---|---|---|
| Good Stuff | Buy The Cow | Go Away |
|
||
| Powered by Google | Drink The Milk | Don't Steal |
| posts / comments | © 2001-8 Mark Pilgrim |
Yeah, totally! It obviously should have read
def cdr(t): return t[1:]
:-)
Comment by Gordo — Thursday, April 10, 2003 @ 4:00 pm
I detect signs of a previous lisp obsession.
Comment by Simon Willison — Thursday, April 10, 2003 @ 4:01 pm
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
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
I think you need more parentheses…
Comment by Bruce — Thursday, April 10, 2003 @ 7:55 pm
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
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
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
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
Ack.
Comment by Jeremy Dunck — Friday, April 11, 2003 @ 4:28 pm
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
Phil: AFAIK, there’s no way to do that in Python.
Comment by Mark — Tuesday, April 15, 2003 @ 4:07 pm