Search code examples
appendschemeracketcons

Scheme: how to produce '(5 . (5))


I have tried all kinds of combinations of cons and append to produce '(5 . (5)) but I couldn't. Is there any way?


Solution

  • At the risk of sounding like Bill Clinton, it depends on what you mean by "produce".

    If you mean "produce a value that prints on the screen as '(5 . (5)), then you're sort of out of luck, because this value prints as '(5 5).

    For a similar example: how do I produce the number 1e-1 ? Well, try typing it in; this is the same as 0.1, and if you type in 1e-1, it's going to print as 0.1.

    However, you can evaluate

    #lang racket
    (= 0.1 1e-1)
    

    ... and you'll see that they're the same number.

    In the same way, try evaluating

    #lang racket
    (equal? '(5 . (5)) (list 5 5))
    

    and you'll see that these are two ways of writing the same value.