Use of # a.k.a. read-macro

前端 未结 4 985
天涯浪人
天涯浪人 2021-01-22 03:26

Reading book \"Let Over Lambda\" by Doug Hoyte, I found the following description of #. sign, a.k.a. read-macro:

A basic read macro that come

4条回答
  •  不思量自难忘°
    2021-01-22 04:11

    Unlike most languages Common Lisp doesn't really have a parser. It has a lexer known as the reader. The reader consumes single characters and looks them up in a table and calls the function found there[1]. The role played by a parser in other languages is, in Lisp, fulfilled by macros.

    [1] http://www.lispworks.com/documentation/lw51/CLHS/Body/02_b.htm

    For example semicolon's reader consumes the rest of the line and discards it as a comment. So, for example, the reader for open paren. calls a function that recursively reading the elements of a list. So, for example single-quote recursively reads a single form and then wraps it in quote. Thus '(1 2 3) is read as (quote (1 2 3)). There are a handfull of these key complex token readers.

    [2] http://www.lispworks.com/documentation/lw51/CLHS/Body/02_d.htm

    The character #\# provides a place for to put a slew of extra reader behaviors. The reader for hash repeats the design for the main reader. It consumes another character and look that up in a table and calls the function found there. There are a lots[3] of these.

    [3] http://www.lispworks.com/documentation/lw51/CLHS/Body/02_dh.htm

    So, for example, we have a reader analogous to the one for lists that reads vectors instead, e.g. #(1 2 3). So, for example, we have a reader for single characters such that you can enter a single semicolon, double quote, or period as #\;, #\", and #\. respectively.

    To answer your specific question: the hash reader for quote, e.g. #'foo, is analogous to the one for regular quote. It reads the following token and wraps it in function. #'foo is read as (function foo).

    It is possible to modify the reader's tables to customize the language. The entries in the table are known as reader macros. A name that tends to confuse people somewhat because they are quite distinct from the macros defined by defmacro. Together they provide what has been called the ability to "grow" the language[4].

    [4] http://www.catonmat.net/blog/growing-a-language-by-guy-steele/

提交回复
热议问题