Reputation: 897
I have a following in Scheme:
((car '(null? null?)) ())
which should evaluate to #t but I'm getting an error:
the object null? is not applicable
I tried some of the solutions in other SO questions but none of them seems to work.
How do I evaluate a symbol?
Upvotes: 3
Views: 217
Reputation: 315
The answer from Sylwester shows the right way to understand the problem. I will try to make it simpler.
(i) The problem arise with every function, not only with the function "null?".
(define (square x) (* x x))
((car '(square square) 2) ---> The object square is not applicable.
((car (list square square) 2) ---> 4
(ii) To reduct the problem to more simple expressions:
(car '(square)) ---> square
(car (list square)) ---> (#[compound-procedure 20 square])
(symbol? (car '(square))) ---> #t
(procedure? (car (list square))) ---> #t
(iii) Before reading your question and sylwester's answer I thought that '(square) and (list square) are the same thing.
Upvotes: 0
Reputation: 48745
It should not evaluate to #t
. You are mixing symbols and variables. The second you quote something it's the code representation that becomes data.
'(null? null?)
; ==> (null? null?)
That is a list with two symbols. They have noting to do with:
null?
; ==> #<procedure:null?> (implementation dependent visualization)
When you evaluate the variable null?
you get the closure object. If you want to make a assoc of primitives you need to use list
or cons
to not quote the variables or you need to use quasiquote-syntax:
(define *primitives*
`((null? . ,null?)
(car . ,car)
(cdr . ,cdr)))
This is just syntax sugar for using list
and cons
.
When you evaluate this you notice right side isn't symbols:
*primitives*
; ==> ((null? . #<procedure:null?>)
; (car . #<procedure:car>)
; (cdr . #<procedure:cdr>))
Again. The visualization of a procedure differs. Here is how you can use it:
(define prim 'car)
(let ((found (assq prim *primitives*)))
(if found
((cdr found) '(1 2 3))
'signal-error))
Upvotes: 4