I am trying to write a simple Scheme function which convert given list to its dotted pair notation .
For example if the input is -
((1 (2)) 3 ((4)))
What will correspond to its dotted pair notation and what rules should be kept in the function to write such logic.
Any such pointers will be truly grateful.
If you ever implement your own LISP and need to do this remember than what you are asking actually is simpler than displaying lists as lists rather than dotted pair notation. Here's one way to do it:
You see that we need to keep a state telling us that we are processing a list since how it looks depend on the
cdr
. What you want is to treat all pairs the same even when thecdr
is()
orpair?
so it's much simpler:Of course a real implementation of this might have much more logic to display the different atomic values so thanks to that these actually just address the pairs. When making my own Lisp Zozotez print was the second largest function after read.
@Chris has given the right approach. Just make sure that printing the
car
and thecdr
are actually recursive calls to your procedure:then
Here is the long form:
Writing a function to print the long form is simple:
"("
or#\(
car
of the pair" . "
cdr
of the pair")"
or#\)