What is ' (apostrophe) in Racket?

前端 未结 2 744
旧时难觅i
旧时难觅i 2021-01-14 07:50

I am a little confused about the meaning of the \' sign in racket. It appears to me that the same sign has different meanings. Look at 2 simple examples below:<

2条回答
  •  执笔经年
    2021-01-14 08:32

    > is a sign of REPL - the Read-Eval-Print Loop.

    First, whatever expression you've typed at the REPL prompt is read - converted to some internal abstract syntax tree representation. Then this internal representation of the typed-in expression is evaluated - i.e. its value is found. Then the result is printed.

    When we type

    > (list 1 2 3 4)
    

    the typed-in expression is read as a nested structure, let's write it as

    [LIST | [1 | [2 | [3 | [4 | NIL ]]]]]
    

    according to the usual representation of lists as pairs of data and the rest of the list (here showing a pair of a and b as [a | b]).

    Then the above structure is evaluated, and because its first element was LIST it causes the invocation of list with the arguments as specified, which causes a fresh list structure to be built, which can be represented as

    [1 | [2 | [3 | [4 | NIL ]]]]
    

    Then it is printed, usually as (1 2 3 4) but Racket chooses to print it as '(1 2 3 4). Incidentally, it can't be evaluated, because 1 can not be called.

    Next, the quoted expression '(1 2 3 4), which is read as (quote (1 2 3 4)). It is converted into

    [QUOTE | [ [1 | [2 | [3 | [4 | NIL ]]]] | NIL ]]
    

    which, when evaluated (according to the evaluation rule for quote), returns the data it received. Which we represent as

    [1 | [2 | [3 | [4 | NIL ]]]]
    

    That's why the two are similar. Whether we build a new list containing 1, 2, 3, and 4; or we cause it to be created as part of read process, so it gets returned verbatim by quote; the result is the same.

提交回复
热议问题