我仍然非常想进入haskell,但我注意到一些让我很恼火的事情。
在“Learn You a Haskell for Great Good!”一书中 有这部分显示了在模式匹配中使用警卫,在这本书的情况下,它是一个计算一个人的 bmi 的小函数,它有点像这样(部分略有改变,不侵犯版权或其他) :
bmiCalc :: (RealFloat a) => a -> a -> String
bmiCalc weight height
| bmi <= 18.5 = "skinny"
| bmi <= 25.0 = "normal"
| bmi <= 30.0 = "fat"
| otherwise = "obese"
where bmi = weight / height ^ 2
这一切都很好,代码像宣传的那样工作,但我想,如果它也显示它计算的 bmi 和文本怎么办?
所以我重写了代码:
bmiCalc :: (RealFloat a) => a -> a -> String
bmiCalc weight height
| bmi <= 18.5 = "skinny, " ++ show bmi
| bmi <= 25.0 = "normal, " ++ show bmi
| bmi <= 30.0 = "fat, " ++ show bmi
| otherwise = "obese, " ++ show bmi
where bmi = weight / height ^ 2
期望“show”像 .toString 在 java 和 c#
Boy 中一样工作,我错了。
ghci 给了我这个严重的错误信息:
Could not deduce (Show a) arising from a use of `show'
from the context (RealFloat a)
bound by the type signature for
bmiCalc :: RealFloat a => a -> a -> String
at file.hs:1:16-48
Possible fix:
add (Show a) to the context of
the type signature for bmiCalc :: RealFloat a => a -> a -> String
In the second argument of `(++)', namely `show bmi'
In the expression: "skinny, " ++ show bmi
In an equation for `bmiCalc':
bmiCalc weight height
| bmi <= 18.5 = "skinny, " ++ show bmi
| bmi <= 25.0 = "normal, " ++ show bmi
| bmi <= 30.0 = "fat, " ++ show bmi
| otherwise = "obese, " ++ show bmi
where
bmi = weight / height ^ 2
Failed, modules loaded: none.
这是为什么?为什么它不允许我将似乎返回字符串的内容附加到字符串?我的意思是据我所知"skinny, " ++ show bmi
是一个字符串......这正是类型签名所说的我必须返回的内容
那么我在这里做错了什么?