嵌套表达式的方案宏

标签 scheme macros nested define-syntax

是否可以在Scheme中编写宏(例如,使用define-syntax),该宏将采用以下表达式:

(op a b c d e f g h i j)

并产生像这样的输出表达式吗?
(op (op (op (op (op (op (op (op (op a b) c) d) e) f) g) h) i) j) 

当然,对于任意长度。给定一些这样的模板,我想不出一种方法:
(define-syntax op
  (syntax-rules ()
    [(_) 'base-case]
    [(v1 v2 ...) 'nested-case??]))

最佳答案

(define bop list)

(define-syntax op
  (syntax-rules ()
    ((op a b) (bop a b))
    ((op a b c ...) (op (bop a b) c ...))))

例如,(op 1 2 3 4)扩展为(bop (bop (bop 1 2) 3) 4)并评估为(((1 2) 3) 4)

关于嵌套表达式的方案宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/340204/

相关文章:

macros - Scheme 语法规则 - (let) 和 (define) 之间变量绑定(bind)的区别

tcp - 如何在 Racket 中使用 TCP?

string - 将Scheme表达式转换为字符串

c - 如何将一个很长的 C 宏桥接到 Swift?

c# - 如何使用NEST Library在Elasticsearch中的嵌套对象中的同一字段上搜索多个值?

lisp - 有没有办法将列表转换为 Scheme 中的集合?

visual-studio - Visual Studio 宏查找字符串并删除匹配行

Clojure 宏作为函数/​​ 'Partial' 用于宏?

node.js - Sails.js 填充嵌套关联

ruby - 有没有办法使用两个 while 循环对 NDS 中子哈希的元素求和?