1

I'm actually faced to a problem in Scheme. And I just don't have any idea about how to solve it. It is pretty simple to understand and I guess kind of easy for any Scheme expert. I just have to simulate the where expression of haskell with the defmac function in scheme defining a macro "operation". For example, to execute a code like the following

> (operation (+ x y) 
   where ([x 1]
          [y (+ x 32)]))
34

I'm kind of familiar with the way to represent simple objects in scheme with macros (defmac) but now i'm really stuck with this problem.

Any help or idea would be really welcome. Thank you in advance.

4

2 回答 2

2

如果我理解正确,您想将该代码转换为类似

(let* ((x 1)
       (y (+ x 32))
  (+ x y))

(define-syntax operation
 (syntax-rules (where)
   ((operation expression where body)
    (let* body expression))))

应该这样做,但只有在“where”在表达式之后的地方

于 2013-08-23T02:53:16.933 回答
1

听起来像这样应该可以解决问题(使用这个定义defmac):

(defmac (operation expr
          where (binding ...))
        #:keywords where
  (let* (binding ...)
    expr))

它只是将您的operation表单转换为等效的let*,因此您的示例将变为:

(let* ((x 1)
       (y (+ x 32)))
  (+ x y))
于 2013-08-23T02:39:26.287 回答