gpt4 book ai didi

emacs - elisp 中的案例,如何与字符串进行比较?

转载 作者:行者123 更新时间:2023-12-01 07:26:11 28 4
gpt4 key购买 nike

(defun test:case (INPUT)
(case (quote INPUT)
("a" (message "bar"))
(otherwise (message "foo"))
))
(test:case "a")

我正在寻找一个案例结构,将“a”与 INPUT 进行比较并调用 (message "foo") .
除了 (message "foo"),我无法让上面的代码调用任何东西.

我一定做错了什么 ?

最佳答案

如果您有 Emacs 24,pcase也适用于比较字符串,并且可能比长 cond 更具可读性表达。

(defun test-case (input)
(message
(pcase input
("a" "bar")
("b" "baz")
("c" "quux")
(_ "foo"))))
pcase的匹配规则比 case 更复杂,但也更通用和灵活。 ,类似于 ML 或 Haskell 中的模式匹配。

关于emacs - elisp 中的案例,如何与字符串进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25092446/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com