- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我希望能够像这样编写嵌套表达式:
(AND/OR expr1 op1 expr2 AND/OR expr3 op2 expr4 and so on)
其中 AND/OR 本质上是 AND 或 OR。但我希望能够写出无限多的。我正在使用 define-syntax
来尝试实现这一点,但我不确定如何接受无限量的嵌套表达式。
不要介意我示例中的 expr 和 op,那部分我可以自己处理。我只想知道如何接受无限嵌套。
例子:
(SELECT somecolumns
FROM sometable
WHERE something
AND/OR something
AND/OR (something AND/OR something)
AND/OR ...)
最佳答案
正如 Asumu 所说,一般来说,处理 s 表达式更简单,至少是为了确保正确的运算符优先级,但对于一些简单的情况,syntax-rules
的模式匹配(和 syntax-parse
和 co) 使用剩余参数和递归匹配使这变得容易:
#lang racket
(define-syntax parse-args
(syntax-rules (AND) ; treat AND as a literal
[(_)
; no more argument, return value:
'()]
[(_ (arg1 AND in-rst ...))
; Composed argument found, call parse-args recursively:
(parse-args arg1 AND in-rst ...)]
[(_ arg1 AND rst ...)
; AND operator found, parse left side and rest
(list 'and
; parse the argument (may be composed or not):
(parse-args arg1)
; then parse the rest of the arguments:
(parse-args rst ...))]
[(_ arg)
; in case the argument is not composed or does not contain AND, don't parse it
arg]))
;; TESTS:
(parse-args 'a AND ('b AND 'bb) AND 'c AND 'f)
; -> '(and a (and (and b bb) (and c f)))
(parse-args 'a AND ('b AND 'bb))
; -> '(and a (and b bb))
但是,请注意,在添加其他运算符时,上述代码可能变得不切实际。
编辑:与选择宏一起:
(define-syntax SELECT
(syntax-rules (FROM WHERE)
[(_ select FROM from WHERE where ...)
(list 'Select select 'From from 'Where (parse-args where ...))]))
; TEST:
(SELECT 'somecolumns
FROM 'sometable
WHERE 'something1
AND 'something2
AND ('something3 AND 'something4)
AND 'blop)
; ->
#;'(Select
somecolumns
From
sometable
Where
(and something1
(and something2
(and (and something3 something4) blop))))
同样,模式匹配允许在正确的点切割列表以获得其余参数
关于macros - Racket 中的嵌套宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21080734/
我意识到“强类型”和“软类型”的定义是松散且易于解释的,但我还没有找到关于无类型 Racket (根据我的理解意味着动态类型)和类型 Racket 的明确定义。 再一次,我确定它不是那么简单和干燥,但
我来自 Java 和 Python,很难理解面向对象代码在 Racket 中的工作方式。 给定 (define food% (class object% (super-new) (i
我安装了 Sublime Text - Racket Package,但没有用。出现此错误。我该怎么办。 [Errno 2] No such file or directory: 'racke
.rkt是“常规”Racket 源代码的常规文件扩展名。在文档中,我还看到了 .rktl 和 .rkts 正在使用。什么是.rktl和 .rkts用于,还有我不知道的更多 Racket 文件扩展名吗?
假设我为具有“中心”的集合定义了一个结构体。 (struct centered-set (center elems)) 我想保证以下条件。 elems是一个集合。 center是 elems 的成员(
我想要一个函数,它接受一个字符串,由一个 python 格式的树组成,就像这样 "[0, [1, 0]]" 并输出一个可用的 Racket 列表,像这样 '(0 (1 0)) 我的第一步是将字符串转换
是否有 Racket 内置函数列表?我正在寻找类似 this one 的列表对于 python 。我似乎无法在文档中找到一个。 最佳答案 这里有一些列表: Racket文档中所有函数的索引: http
虽然 Racket 允许创建 GUI 应用程序(甚至游戏),但我想知道它是否适合与 Things 一样在视觉上发布商业应用程序或 OmniOutliner .这样的应用程序会感觉原生还是看起来像一个不
和node的npm一样,我们使用package.json来解决依赖。 我试过raco pkg,它似乎只是一种开发/发布包的方式,而不是解决外部依赖。 最佳答案 如上所述,Racket 中的 info.
我是 Racket 的新手,尽管到目前为止我已经设法使用 servlet 并让一些东西发挥作用。我现在想做的是验证一个简单的用户名/密码 POST 请求。我已经通过“web-server/formle
我正在为现场编码表演创建一个程序,为此我需要一个基本的 S 表达式代码编辑器(我输入的内容将在适当的语法上下文中评估为 Racket 代码)。 由于 DrRacket 本身是用 Racket 编写的,
和node的npm一样,我们使用package.json来解决依赖。 我试过raco pkg,它似乎只是一种开发/发布包的方式,而不是解决外部依赖。 最佳答案 如上所述,Racket 中的 info.
我正在尝试在 Racket 中编写模块元语言 mylang ,它接受将修改后的主体传递给的第二种语言,例如: (module foo mylang typed/racket body) 相当于: (m
我没有运气找到一种方法来按照我想要的方式进行这项工作,所以如果有人能提供帮助,我将不胜感激。 我想要的是能够在终端上做到这一点: > racket 然后能够输入 Racket 命令并让它们编译。像,
我来自 Java 世界,对 Racket 充满热情。 想知道类似Maven的工具在哪里。如果你不熟悉Maven,还有Scala中的SBT,Android中的gradle,Elixir中的mix等,作为
在 typed/racket 我有一个像 [(? procedure? p ) (apply p xv*)]会导致错误: Type Checker: Function has no cases in:
我是 Lisp-scheme 的新手,对整个函数范式还很陌生,我目前正在做一个任务,要求我重载一个名称相同但参数集不同的函数 racket .下面是我试图实现的一个例子: #lang racket (
有没有办法在 Racket 中获取对象的唯一标识符?例如,当我们使用 Racket 的 eq?运算符来检查两个变量是否指向同一个对象,它使用什么标识符来实现这种比较? 我正在寻找类似 python 的
当我在 Dr. Racket 或从命令行运行我的测试时,失败的测试总是在一行上。 例如, (bad (interp-t-prog (list '(class posn extends object (
我有以下代码: (plot (function sin (- pi) pi #:label "y = sin(x)")) 但是在解决 SICP 时我没有遇到这样的结构:#:label "y = sin
我是一名优秀的程序员,十分优秀!