gpt4 book ai didi

haskell - 编写我自己的最小功能,无法修复错误

转载 作者:行者123 更新时间:2023-12-02 00:18:19 24 4
gpt4 key购买 nike

我一直想编写自己的最小函数,只是为了探索可能性(我是 Haskell 的新手)。

minimum' :: (Ord a) => [a] -> a
minimum' [] = error "empty set - no minimum"
minimum' (x:xs) = if (x <= minimum' xs) then x

问题是,我收到解析器错误:

(possibly incorrect indentation or mismatched brackets)

有谁知道这里出了什么问题吗?这种递归方法可能吗?

非常感谢您的回答!

最佳答案

if (x <= minimum' xs) then x

在 haskell 中,if/else 结构作为一个整体必须返回一个值,因此你需要一个与 if 相匹配的 else 。

此外,阿莫斯·罗宾逊提出了一个很好的观点:

minimum' [] = error "empty set - no minimum"

当递归最终以空列表调用 minimum' 时,将会出错。由于您是自己进行练习,因此我不会尝试修复它,但请记住这一点。

关于haskell - 编写我自己的最小功能,无法修复错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18137282/

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