gpt4 book ai didi

common-lisp - case 宏中使用的定义参数和相等谓词

转载 作者:行者123 更新时间:2023-12-04 02:57:57 31 4
gpt4 key购买 nike

我有一些使用 defparameter 创建的整数类型的全局变量。我使用这些作为 CASE 子句中的键来匹配 test-key,在我的程序中的某个时候,它被设置为这些全局变量之一。

据我所知, CASE 宏使用 EQL ( http://clhs.lisp.se/Body/26_glo_s.htm#same ),但我没有得到匹配项:所有内容都被转储到了 else 子句中。下面的代码总结了令我困惑的事情。

  • (defparameter snafu 123) => SNAFU
  • (let ((x snafu)) (and (eq x snafu) (eql x snafu) (equal x snafu))) => T
  • (let ((x snafu)) (case x (snafu 'yes) (t 'no))) => NO
  • (let ((x snafu)) (cond ((eql x snafu) 'yes) (t 'no))) => YES

  • 我不明白为什么我在 CASE 中没有得到 YES。

    最佳答案

    Case 子句不被评估,它们是按字面意思理解的。
    这个:

    (case x
    (snafu 'yes)
    (t 'no))

    相当于:
    (if (eql x 'snafu) 'yes 'no)

    注意 snafu 之前的引用: x与符号 snafu 进行比较而不是它的值(value)。

    关于common-lisp - case 宏中使用的定义参数和相等谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56718158/

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