【发布时间】:2019-03-30 04:38:45
【问题描述】:
对于一项学校作业,我的任务是创建一个确定一年是否为闰年的函数。
以下是我目前所拥有的。例如,当我输入“days_in_Year 2018”时,它会返回“非闰年”。此功能仅适用于整数值输入。我需要一些额外的代码,当输入值为双精度或浮点值时将返回错误消息。
days_in_Year :: Integer -> String
days_in_Year year
| year `mod` 400 == 0 = "Leap Year"
| year `mod` 100 == 0 = "Non-Leap Year"
| year `mod` 4 == 0 = "Leap Year"
| otherwise = "Non-Leap Year"
当输入为双精度或浮点值时,我希望我的函数能够返回“无效输入”...
【问题讨论】:
-
您的输入验证需要在您输入的地方进行,您没有向我们展示。它不能进入这个功能;太晚了!
-
您在类型注解中指定了
Integer输入,因此如果您尝试传递其他任何内容,编译器已经给您一个无效的错误。
标签: haskell error-handling floating-point integer