4 ms·
> If you wanted to implement cons, car, and cdr in Python such that (cons (car "abc") (cdr "abc")) = "abc" you wouldn't be able to do it in the natural way Sur
by sweeneyrod 7y ago
> If you wanted to implement cons, car, and cdr in Python such that (cons (car "abc") (cdr "abc")) = "abc" you wouldn't be able to do it in the natural way
Sure you can!
>>> car = lambda x: x[0]
>>> cdr = lambda x: x[1:]
>>> cons = lambda x, y: x + y
>>> cons (car("abc"), cdr("abc"))
'abc'
- inimino 7y agoBut that cons is broken, such that you can't construct lists of strings. For a correct implementation of cons, the following gives a list '("a" "b") of two strings: (cons "a" (cons "b" ())) and (list "a" "b") is just sugar for the above. This doesn't work if you redefine cons on strings to mean concat. In other words, if (cons "a" "bc") gives you "abc", then it's not cons.