简体   繁体   English

球拍局部

[英]local in racket

I am reading about local definitions in the book, and I came across this example- 我正在阅读本书中的本地定义,我遇到了这个例子 -

(local ((define (f x) (+ x 5))
    (define (g alon)
      (cond
        [(empty? alon) empty]
        [else (cons (f (first alon)) (g (rest alon)))])))
  (g (list 1 2 3)))

what exactly does local do here? local到底做了什么?

local is documented either in here as part of one of the HtDP languages or in here as part of the local module. local作为HtDP语言之一记录在这里或在此处作为local模块的一部分记录。 Let's see each one in turn. 让我们依次看每一个。 First the one in HtDP: 首先是HtDP中的一个:

(local [definition ...] expression) Groups related definitions for use in expression. (local [definition ...] expression)用于(local [definition ...] expression)组相关定义。 Each definition can be either a define or a define-struct. 每个定义可以是define或define-struct。 When evaluating local, each definition is evaluated in order, and finally the body expression is evaluated. 在评估local时,将按顺序评估每个定义,最后评估body表达式。 Only the expressions within the local (including the right-hand-sides of the definitions and the expression) may refer to the names defined by the definitions. 只有本地(包括定义和表达式的右侧)中的表达式可以引用定义定义的名称。 If a name defined in the local is the same as a top-level binding, the inner one “shadows” the outer one. 如果在本地定义的名称与顶级绑定相同,则内部名称“遮蔽”外部名称。 That is, inside the local, any references to that name refer to the inner one. 也就是说,在本地内部,对该名称的任何引用都是指内部引用。

And next, the one in the local module: 接下来, local模块中的那个:

(local [definition ...] body ...+) Like letrec-syntaxes+values, except that the bindings are expressed in the same way as in the top-level or in a module body: using define, define-values, define-syntax, struct, etc. Definitions are distinguished from non-definitions by partially expanding definition forms (see Partial Expansion). (local [definition ...] body ...+)像letrec-syntaxes + values一样,除了绑定以与顶层或模块体中相同的方式表示:使用define,define-values, define-syntax,struct等。定义通过部分扩展定义形式与非定义区分开(参见Partial Expansion)。 As in the top-level or in a module body, a begin-wrapped sequence is spliced into the sequence of definitions. 与顶层或模块体一样,开始包装的序列被拼接到定义序列中。

So depending on the language/modules in use, you'll know which local was the one you found. 因此,根据使用的语言/模块,您将知道您找到的是哪个local And clearly, it's not a standard special form. 显然,它不是标准的特殊形式。

Local used to define some helper functions in the scope of a specific function. Local用于在特定函数的范围内定义一些辅助函数。 For example I am writing a function to add 5 to all elements of the given list , 例如,我正在编写一个函数来向给定列表的所有元素添加5,

(define (add-5-to-list list)
    (local
        ( ;; definition area start
         (define (f x) (+ x 5))
         (define (g alon)
            (cond
               [(empty? alon) empty]
               [else (cons (f (first alon))
                           (g (rest alon)))]))
        ) ;; definition area end
        (g list)
    ) ;; local end
) ;; define end

You can define as many function as you like in local. 您可以在本地定义任意数量的功能。 But you can use only in the scope of the main function (here the main function is add-5-to-list). 但是你只能在main函数的范围内使用(这里的main函数是add-5-to-list)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM