I am working in Guile, and need to use a procedure that is stored in a pair. If I store car
in the following examples:
(define pairA (cons 1 car))
(define pairB '(1 . car))
.. I can only evaluate the procedure from the first expression. The second is turned into a symbol, I'm guessing somehow due to it being in a pair but not a list:
>(display pairA)
(1 . #<primitive-procedure car>)
>(display pairB)
(1 . car)
Why does the first example return car
as a procedure, but not the second one? The only difference I can tell is that pairA
is both a list/pair, and pairB
is only a pair. Is there some way to use (cdr pairB)
as a procedure?
What you want is
`(1 . ,car)
This is the quasiquote construct -- in short, it evaluates only car (in general -- only the unquoted "," expressions), and inserts its value in the position of unquoted form. One more small example:
scheme@(guile-user)> `(quasiquote magic (+ 2 3) => ,(+ 2 3))
$1 = (quasiquote magic (+ 2 3) => 5)
Your first example worked because cons is a procedure (i.e. not a special form) so it's arguments are evaluated prior to application. The second did not work, because you had (1 . car) quoted.
Note also that your pairA is NOT a list -- procedures are not represented as lists, they are atoms:
scheme@(guile-user)> (pair? car)
$1 = #f