有一些类型Record
:
type Day = Integer
type Description = String
type Name = String
type PhoneNumber = String
type Year = Integer
data Month = January | February | March | April | May | June | July
| August | September | October | November | December
deriving (Eq, Ord, Enum, Show)
data Birthday = Birthday Month Day
deriving (Eq, Show)
data DatingDate = DatingDate Year Month Day
deriving (Eq, Show)
data Record = BirthdayRecord Name Birthday
| PhoneRecord Name PhoneNumber
| DatingRecord DatingDate Description
deriving (Eq, Show)
以及按日期过滤这些记录的函数:
getAssignment :: (Year, Month, Day) -> [Record] -> [Record]
getAssignment (year, month, day) = filter matchDate
where matchDate (BirthdayRecord _ (Birthday month day)) = True
matchDate (DatingRecord (DatingDate year month day) _) = True
matchDate _ = False
由于错误,此定义getAssignment
不正确:
warning: Defined but not used: `year'
实际上,令我惊讶的是year
,模式匹配部分getAssignment
和year
模式匹配部分matchDate
并不相同。
那么,year
变量的范围界限在哪里开始和结束呢?它发生是因为where
部分?
顺便说一句,这个错误可以通过一些冗余的大量使用(year, month, day)
变量来避免。
getAssignment' :: (Year, Month, Day) -> [Record] -> [Record]
getAssignment' date = filter (matchDate date)
where matchDate (_, m, d) (BirthdayRecord _ (Birthday month day)) =
month == m && day == d
matchDate (y, m, d) (DatingRecord (DatingDate year month day) _) =
year == y && month == m && day == d
matchDate _ _ = False
怎么可能改写?