【问题标题】:How to create a type with n possible values in Lisp [duplicate]如何在 Lisp 中创建具有 n 个可能值的类型
【发布时间】:2021-01-19 06:29:08
【问题描述】:

这周我一直在学习 Common Lisp,玩弄了一下。作为练习,我想翻译一些我为模拟平衡三进制而编写的 Haskell 代码。它的核心是类型:

数据 Trit = Pos |中性 |否定

如何在 Lisp 中声明这种类型?这个想法是这种类型应该只有三个可能的值。

【问题讨论】:

  • 当你想检查x的类型时,你可以(deftype ternary () '(member :pos :neutr :neg)),然后(check-type x ternary)
  • 是的,完美,谢谢!我现在就结束这个问题。

标签: class haskell types lisp ternary


【解决方案1】:

在 Common Lisp 程序中,您只需使用符号 'Pos'Neutr'Neg

如果你真的需要一个特定的类型,你可以参考: Common Lisp equivalent to C enums 但我建议保持简单并使用 'Pos'Neutr'Neg

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2018-07-09
    • 2012-03-19
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多