Does anyone know why Racket differentiates Lists from Pairs?
They appear to be so similar
Just a guess, but usually the cdr of a list is also a list, not an atom
so if you passed a pair to a contract that specified list, there could still be a runtime error if it does something with the tail of it
@rick.moynihan has joined the channel
Lists are built out of pairs, and if the pair notation was the only thing we had available then a list would be written as ( a . ( b . ( c . ( d . ())))). It’s rather cumbersome to write a list like that so it’s just easier to employ a list notation (a b c d) and be done with it.
And why the need for () as the very last thing in the list written out in the pair notation? If you consider a list of just one element, (d). How would you want to write that in pair notation? You might try write it as (d . ) but that doesn’t work, a pair always has two elements, the car and cdr of a pair always return valid elements. So we just define the concept of an empty list, identify it as (), which is a valid value in Racket. Now we can write (d) as (d . ()). Thus the cdr of (d) will always be ().