Haskell错误生成函数以确定输入的日期是否为有效

时间:2015-01-08 14:47:54

标签: haskell

这是我尝试使用的

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

我不知道我哪里出错了,感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您需要为elem使用中缀表示法:

m `elem` lowMonths

docs for elem显示是一个中缀函数(我们知道这是因为它没有括号),所以要在中缀样式中使用它,你必须用它来包围它反引号。

将此与(!!)对比,后者在括号中定义。这意味着它自动成为中缀函数,因此可以在没有反引号的操作数之间使用它:xs !! 2。要创建运算符前缀,只需将其括在括号中:(!!) xs 2