gpt4 book ai didi

Haskell 刚性类型?

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

Possible Duplicate:
Return specific type within Haskell

这段代码:

class Alpha a where
half :: Real f => a -> f

instance Alpha Double where
half a = a/2.0

产生此错误:

rigid.hs:6:13:
Couldn't match expected type `f' against inferred type `Double'
`f' is a rigid type variable bound by
the type signature for `half' at rigid.hs:3:17
In the expression: a / 2.0
In the definition of `half': half a = a / 2.0
In the instance declaration for `Alpha Double'
Failed, modules loaded: none.

为什么?

编辑:弄清楚刚性类型的业务,我想我已经找到了一个更有针对性的问题。如何将 Double 转换为满足 Ord 和 Num 的值?也就是说,一个让我得到 (Ord a, Num a) => Double -> a 的函数?

最佳答案

您声称 half 可以将类型 a 转换为任何 Real 类型。但你的 half 只能将 a (Double) 转换为 Double

关于Haskell 刚性类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3183984/

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