4 ms·
Shouldn't cdr be changed so that it gives the list that doesn't contain the first element? def cdr(x): return x if x == () else x[1:]
by sn9 10y ago
Shouldn't cdr be changed so that it gives the list that doesn't contain the first element?
def cdr(x):
return x if x == () else x[1:]
- DonaldFisk 10y ago# No. x = sexpr.list(1, 2, 3, 4) >>> print x (1, (2, (3, (4, ())))) >>> print sexpr.cdr(x) (2, (3, (4, ()))) # which is what you want. >>> def cdr2(x): return x if x == () else x[1:] ... >>> print cdr2(x) ((2, (3, (4, ()))),) # which is wrong.
- sn9 10y agoBut that's how it's used in Lisp, which is where car and cdr are used. (car x) is the first sexp in x and (cdr x) is x without the first sexp.
- DonaldFisk 10y agoYou are mistaken. In Lisp, lists are constructed out of NIL and dotted pairs, so, (1 . (2 . (3 . NIL))) = (1 2 3) I chose to implement lists in Python in the same way, out of () and 2-element tuples. So (1 2 3) = (1 . (2 . (3 . NIL))) in Lisp which becomes (1, (2, (3, ())) in Python. (cdr '(1 2 3)) = (2 3) = (2 . (3 . NIL)) which should therefore be (2, (3, ())) in Python, i.e. the second element of the 2-element tuple (1, (2, (3, ())), NOT the 1-element tuple ((2, (3, ())),) obtained by removing the first element. I should point out that there is one difference, in case anyone else spots it: tuples in Python are immutable, whereas dotted pairs in Lisp are not, so my implementation won't allow rplaca and rplacd.
- deleted 10y ago[deleted]