Search code examples
lisp

QUOTE with multiple arguments


I'm analyzing LISP, I'm no expert, but something is bothering me:

Some primitives as list accepts more than one parameter. e.g.:

(list 1 2 3)
=> (1 2 3)

On the other hand quote seems to accept just one parameter. e.g:

(quote (1 2 3))
=> (1 2 3)
(quote x)
=> 'x
(quote 1 2 3)
=> 1  ???

Is there a reason why (quote 1 2 3) i.e. quote with multiple params, just ignores the other arguments?

what will happen if (quote 1 2 3) evaluates to (1 2 3), i.e. an special case when more than one argument is supplied?

I do understand that this special case is superfluous, but my question to LISP hackers is:

adding such special case to quote will break everything? will it break the REPL? will it break macros?

Note: tested on http://repl.it/ and http://clojurescript.net/


Solution

  • Note that Lisp is not a single language, but a large family of somewhat similar languages. You seem to have tried out Scheme (repl.it runs BiwaScheme) and ClojureScript.

    The Scheme spec only defines one argument for quote, so BiwaScheme seems to be wrong in that respect. (quote 1 2 3) should be an error in Scheme. For example, Racket, another dialect of Scheme, does not accept them:

    $ racket
    Welcome to Racket v5.3.6.
    > (quote 1)
    1
    > (quote 1 2 3)
    stdin::10: quote: wrong number of parts
      in: (quote 1 2 3)
      context...:
       /usr/share/racket/collects/racket/private/misc.rkt:87:7
    

    BiwaScheme is written in JavaScript, and JavaScript simply ignores extra arguments to any function, so the behavior probably comes from there.

    ClojureScript might inherit its manners from JavaScript or from Clojure. Clojure's documentation explicitly states that quote with multiple arguments evaluates to the first of them only.

    Common Lisp, another popular Lisp language, also only accepts a single argument to quote:

    $ sbcl
    * (quote 1 2 3)
    debugger invoked on a SIMPLE-ERROR in thread
    #<THREAD "main thread" RUNNING {1002B2AE83}>:
      wrong number of args to QUOTE:
     (QUOTE 1 2 3)
    

    Note that in general, for any Lisp, quote is seldom spelled out. It is just a special form that is an expansion of '. In the ' form, it is not even possible to give quote extra arguments:

    '(1 2 3) ≡ (quote (1 2 3))
    'x       ≡ (quote x)
    '???     ≡ (quote 1 2 3)
    

    I don't immediately see a problem with expanding quote's definition in any given language to, in case of multiple arguments, evaluate them as a list, but I certainly do not see a use for that feature, either.