--bmicalculator
bmicalculator::IO()
bmicalculator=do bmicalculator::(RealFloat a)=>a->a->String
putStrLn "Please Input your weight"
weight<-getLine
putStrLn "Please Input your height"
height<-getLine
|bmi<=17.5="You are anorexia!"
|bmi<=20.7="You are Under weight"
|bmi<=26.4="You are in normal range"
|bmi<=27.8="You are marginally overweight"
|bmi<=31.1="You are overweight"
|bmi>31.1="You are super OBESE!!"
where bmi=weight/height^2
错误发生在
weight<-getLine
我如何提示用户输入“weight”和“height”然后计算并返回字符串,就像我创建数据类型bmicalculator::(RealFloat a)=>a->a->String
答案 0 :(得分:3)
首先你的缩进是错误的(也就是说:不存在)。 do-block的内容应该缩进。
然后你给了两个与bmicalculator不相符的类型签名。第二个是在do-block里面,它显然不属于。
然后你似乎在do-block中使用模式保护,在任何模式匹配结构之外。这在语法上是无效的。你可能错过了case bmi of
。您还需要将=
替换为->
。
最后,您不能在do
- 块之后的where-block中使用do-block本地的变量。您应该在let
- 块中使用do
。此外weight
和height
是字符串,因此如果不先将它们转换为数字,就不能对它们进行算术运算。
答案 1 :(得分:1)
另外你想要将bmi-calculator和main函数分开,因为它是一个没有任何副作用的纯函数
main :: IO ()
main = do
putStrLn "Please Input your weight"
w <- getLine
let weight = read w :: Float
putStrLn "Please Input your height"
h <- getLine
let height = read h :: Float
putStrLn $ bmicalc weight height
bmicalc :: Float -> Float -> String
bmicalc weight height | bmi<=17.5 = "You are anorexic!"
| bmi<=20.7 = "You are underweight"
| bmi<=26.4 = "You are in normal range"
| bmi<=27.8 = "You are marginally overweight"
| bmi<=31.1 = "You are overweight"
| otherwise = "You are super OBESE!!"
where bmi=weight/(height*height)