Search code examples
schemelispmit-scheme

How do I evaluate a symbol in MIT Scheme?


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?


Solution

  • 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))