gpt4 book ai didi

haskell - 如何定义嵌套列表

转载 作者:行者123 更新时间:2023-12-02 11:03:22 25 4
gpt4 key购买 nike

尝试解决 this page 处的练习 7我想定义数据类型来写入一些值,例如:

(List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 5]])

嵌套列表,任意长度、任意深度。

我尝试使用此代码:

data List a = Elem a | List [List a]

但它无法编译:

Parse error: naked expression at top level

如何才能做到呢?

最佳答案

而不是包含以下内容的文件:

data List a = Elem a | List [List a]
(List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 5]])

尝试包含以下内容的文件:

data List a = Elem a | List [List a]
sampleListValue = List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 5]]

顺便说一下,还有一个非常相似的类型 in the standard libraries (我知道您不想使用它,因为它是一个学习练习,但请记住它是可用的)。

关于haskell - 如何定义嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12092650/

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