【发布时间】:2017-11-03 21:09:42
【问题描述】:
我正在尝试制作一个需要 2 个 Realfloat 输入并通过此函数的主函数
bmiTell :: (RealFloat a) => a -> a -> String
bmiTell weight height
| bmi <= 18.5 = "You're underweight"
| bmi <= 25.0 = "You're normal"
| bmi <= 30.0 = "You're overweight"
| otherwise = "You're obese"
where bmi = weight / height ^ 2
我的解决方案是,我尝试获取体重和身高并将其传递给 bmiTell 函数
main = do
putStrLn "What's your weight?"
weight <- getLine
putStrLn "What's your height?"
height <- getLine
print (bmiTell( read weight ::Float, read height ::Float))
这就是这个错误
* No instance for (Show ((Float, Float) -> String))
arising from a use of `print'
(maybe you haven't applied a function to enough arguments?)
* In a stmt of a 'do' block:
print (bmiTell (read weight :: Float, read height :: Float))
In the expression:
do putStrLn "What's your weight?"
weight <- getLine
putStrLn "What's your height?"
height <- getLine
....
In an equation for `main':
main
= do putStrLn "What's your weight?"
weight <- getLine
putStrLn "What's your height?"
....
|
14 | print (bmiTell( read weight ::Float, read height ::Float))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
getbmi.hs:14:12: error:
* No instance for (RealFloat (Float, Float))
arising from a use of `bmiTell'
* In the first argument of `print', namely
`(bmiTell (read weight :: Float, read height :: Float))'
In a stmt of a 'do' block:
print (bmiTell (read weight :: Float, read height :: Float))
In the expression:
do putStrLn "What's your weight?"
weight <- getLine
putStrLn "What's your height?"
height <- getLine
....
|
14 | print (bmiTell( read weight ::Float, read height ::Float))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
我了解 RealFloat 是 Double 或 Float。任何人都可以如何获取 RealFloat 的输入并通过函数传递它。
【问题讨论】:
-
Haskell 中的函数调用不需要参数周围的括号或参数之间的逗号。将
bmiTell( read weight ::Float, read height ::Float)更改为bmiTell (read weight :: Float) (read height :: Float)。 -
谢谢,它有效。您能否提供任何资源让我可以了解有关 Haskell IO 的更多信息
-
这个问题与IO无关。我没有任何特别的建议,但Haskell tag wiki 确实提供了一些您可能会觉得有用的信息。