Time -> DateTime nextMinute date tim-6ren">
gpt4 book ai didi

haskell 错误: "No instance fo (Eq Time) arising from a use of ' =='

转载 作者:行者123 更新时间:2023-12-02 17:55:21 24 4
gpt4 key购买 nike

我对 Haskell 还很陌生,我不明白为什么我不能比较这两个变量。我有这个功能:

nextMinute :: Date -> Time -> DateTime
nextMinute date time = if time == Time 23 59
then DateTime (tomorrow date) newDay
else DateTime date (timeSucc time)

我收到此错误消息:

Time.hs:88:32:
No instance for (Eq Time) arising from a use of ‘==’
In the expression: time == Time 23 59
In the expression:
if time == Time 23 59 then
DateTime date newDay
else
DateTime date (timeSucc time)
In an equation for ‘nextMinute’:
nextMinute date time
= if time == Time 23 59 then
DateTime date newDay
else
DateTime date (timeSucc time)

最佳答案

您可以只使用模式匹配,而不是使用 ==:

nextMinute :: Date -> Time -> DateTime
nextMinute date (Time 23 59) = DateTime (tomorrow date) newDay
nextMinute date time = DateTime date (timeSucc time)

Eq 添加到您的 Time 类型中可能没问题,但是当存在同样好的模式匹配解决方案时,没有特别需要。

关于 haskell 错误: "No instance fo (Eq Time) arising from a use of ' ==',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33289107/

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