Functional programming 创建n个本地绑定

Functional programming 创建n个本地绑定,functional-programming,scheme,racket,Functional Programming,Scheme,Racket,假设我们有这样的东西: ;list L of n procedure definitions, where a procedure definition is a list in form: ;<name> <body> ;for example: ((n1 (+ 1 1)) (n2 (- 1 0)) (n3 (* 2 2))), where n = 3, n1 is the name of ;first procedure in list L and (+ 1 1)

假设我们有这样的东西:

;list L of n procedure definitions, where a procedure definition is a list in form: 
;<name> <body>
;for example: ((n1 (+ 1 1)) (n2 (- 1 0)) (n3 (* 2 2))), where n = 3, n1 is the name of
;first procedure in list L and (+ 1 1) is its body.
(define foo
(let* ((n1 (lambda () (+ 1 1)
       (n2 (lambda () (- 1 0)
       (n3 (lambda () (* 2 2))

(n1) ;newly defined procedure calls
(n2)
))
这里是硬编码的,有没有办法不硬编码就创建n个本地绑定?就像读取n个过程的列表直到它为空一样,对于其中的每个过程定义,在let*中创建它的局部绑定


提前感谢。

下面是一个宏示例

#lang racket
(require (for-syntax syntax/parse))

(define-syntax (define-locals stx)
  (syntax-parse stx
    [(_define-locals ([name expr] ...) body ...)
     #'(let ([name (lambda () expr)] ...) body ...)]))


(define-locals ((n1 (+ 1 1)) (n2 (- 1 0)) (n3 (* 2 2)))
  (list (n1)
        (n2)
        (n3)))
结果是:

'(2 1 4)

列表
((n1(+1))(n2(-1 0))(n3(*2)))
在编译时可用还是由用户在运行时提供?@soegaard它是由用户在运行时提供的。如果是这样,您不能在运行时引入本地绑定。这不是一项计划/球拍限制,而是一项一般性限制。如果直到运行时才知道绑定的名称,那么如何在程序中引用该绑定。也就是说,也许你可以用哈希表代替?它们提供了一种从名称到值(这里是函数)的转换方式。@P.Lance您似乎是从另一种语言来到Racket的。如果你不尝试引入自己的风格和一套惯例,你会取得更好的成功。@soegaard omg我刚刚重新安装了
planet neil/sicp
,我看到DrRacket正在获取
soegaard/sicp
。。。这不可能是巧合。你对这个项目有什么参与?我甚至不知道自己该到哪里去找答案。