Haskell 制作函数以确定输入的日期是否有效时出错
Haskell error making a function to determine if a date entered is Valid
这是我尝试使用的
validDate :: Int -> Int -> Bool
validDate d m
| d <= 31 && m elem highMonths = True
| d <= 30 && m elem lowMonths = True
| d <= 28 && m == 2 = True
| otherwise = False
where
highMonths = [1, 3, 5 , 7, 8, 10, 12]
lowMonths = [4, 6, 9, 11]
但是它向我抛出一个我不太明白的错误。
WS1.hs:73:22:
Couldn't match expected type ‘(a0 -> [a0] -> Bool) -> [t1] -> Bool’
with actual type ‘Int’
Relevant bindings include highMonths :: [t1] (bound at WS1.hs:78:9)
The function ‘m’ is applied to two arguments,
but its type ‘Int’ has none
In the second argument of ‘(&&)’, namely ‘m elem highMonths’
In the expression: d <= 31 && m elem highMonths
我不知道哪里出错了,感谢任何帮助。
您需要为 elem
使用中缀表示法:
m `elem` lowMonths
docs for elem
表明它不是一个中缀函数(我们知道这一点是因为它没有括号),所以要以中缀风格使用它,你必须用反引号包围它。
将此与括号中定义的 (!!)
进行对比。这意味着它自动是一个中缀函数,所以它可以在没有反引号的操作数之间使用:xs !! 2
。要制作运算符前缀,只需将其括在括号中即可:(!!) xs 2
这是我尝试使用的
validDate :: Int -> Int -> Bool
validDate d m
| d <= 31 && m elem highMonths = True
| d <= 30 && m elem lowMonths = True
| d <= 28 && m == 2 = True
| otherwise = False
where
highMonths = [1, 3, 5 , 7, 8, 10, 12]
lowMonths = [4, 6, 9, 11]
但是它向我抛出一个我不太明白的错误。
WS1.hs:73:22:
Couldn't match expected type ‘(a0 -> [a0] -> Bool) -> [t1] -> Bool’
with actual type ‘Int’
Relevant bindings include highMonths :: [t1] (bound at WS1.hs:78:9)
The function ‘m’ is applied to two arguments,
but its type ‘Int’ has none
In the second argument of ‘(&&)’, namely ‘m elem highMonths’
In the expression: d <= 31 && m elem highMonths
我不知道哪里出错了,感谢任何帮助。
您需要为 elem
使用中缀表示法:
m `elem` lowMonths
docs for elem
表明它不是一个中缀函数(我们知道这一点是因为它没有括号),所以要以中缀风格使用它,你必须用反引号包围它。
将此与括号中定义的 (!!)
进行对比。这意味着它自动是一个中缀函数,所以它可以在没有反引号的操作数之间使用:xs !! 2
。要制作运算符前缀,只需将其括在括号中即可:(!!) xs 2