Search code examples
schemequotesyntax-checking

Function for detecting quotes in Scheme code


I am trying to write a function that can check whether or not some input is a quotation for a syntax checker.

I have the following code:

(define is-quote-expression?
  (lambda (v)
    (equal? (car v) 'quote)
    (is-quotation? (cdr v)))))

(define is-quotation?
  (lambda (v)
    (or (number? v)
        (boolean? v)
        (char? v)
        (string? v)
        (symbol? v)
        (null? v)
        (and (pair? v)
             (is-quotation? (car v))
             (is-quotation? (cdr v)))))

When I try to evaluate, I get the following:

 (is-quote-expression? '1)
 #f

 (is-quote-expression? (cons 'quote 1))
 #t

I think my TA told me that the Scheme environment replaced all "'" with "'quote", however, this does not seem to be the case. We are running Petite Chez Scheme.

What am I not seeing?

Thanks in advance.


Solution

  • I agree with Óscar's post, though, is-quote-expression? accepts a list rather than a pair and returns whether it was a quotation.

    (define is-quote-expression?
      (lambda (v)
        (and (proper-list-of-given-length? v 2)
             (equal? (car v) 'quote)
             (is-quotation? (cadr v)))))
    

    Also, your original question shows some confusion as to what quote actually does. This is what really should happen:

    > (is-quote-expression? '1)
    #f
    > (is-quote-expression? (cons 'quote 1))
    #f
    > (is-quote-expression? (quote 42))
    #f
    > (is-quote-expression? (quote (quote 42)))
    #t
    

    Note how the built-in quote procedure simply returns what it is passed. In the case of (quote 42) it simply returns 42. But (quote (quote 42)) returns (quote 42), which is what you wish to pass to is-quote-expression?.