首页 > 解决方案 > 匹配日期列表的月份

问题描述

我正在尝试编写一个 Haskell 函数,该函数接受日期和月份的列表,并返回列表中有多少日期与给定月份匹配。这是我到目前为止所拥有的,它似乎不起作用

numInMonth :: [(Int,Int,Int)] -> Int -> Int
numInMonth x [] = x
numInMonth x (y:ys)

minList1 (x:xs) = currentMin x xs

currentMax :: Int -> [Int] -> Int
currentMax x [] = x
currentMax x (y:ys)
| x >= y = currentMax x ys
| otherwise = currentMax y ys

maxList1 :: [Int] -> Int
maxList1 (x:xs) = currentMax x xs

标签: haskell

解决方案


假设元组是一个日期:

type Day = Int
type Month = Int
type Year = Int
type Date = (Year, Month, Day)

我们可以考虑您想要的逻辑,首先过滤与给定月份匹配的所有日期,然后计算它们:

numInMonth :: [Date] -> Month -> Int
numInMonth dates month = length . filter (\(_, m, _) -> m == month) $ dates

推荐阅读