Cheryl's birthday: Difference between revisions
m
→{{header|Haskell}}: (some tidying)
m (→{{header|Haskell}}: (some tidying)) |
|||
Line 1,175:
<lang haskell>{-# LANGUAGE OverloadedStrings #-}
import Data.List as L (filter, groupBy, head, length,
import Data.Map.Strict as M (Map, fromList, keys, lookup)
import Data.Text as T (Text, splitOn, words)
import Control.Applicative (liftA2)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.Function (on)
import Data.Tuple (swap)
import Data.Bool (bool)
data DatePart
Line 1,199 ⟶ 1,201:
-- (3 :: A "Then I also know")
uniquePairing Month $
-- among the days with unique months,
--
Line 1,205 ⟶ 1,206:
-- (2 :: B "I know now")
uniquePairing Day $
-- excluding months with unique days,
--
Line 1,211:
-- (1 :: A "I know that Bernard does not know")
monthsWithUniqueDays False $
-- from the given month-day pairs:
--
Line 1,221 ⟶ 1,220:
\July 14, July 16, Aug 14, Aug 15, Aug 17"
monthsWithUniqueDays :: Bool -> [(M, D)] -> [(M, D)]
monthsWithUniqueDays bln xs =
let months = fst <$> uniquePairing Day xs
in L.filter (bool not id bln . (`elem` months) . fst) xs
uniquePairing :: DatePart -> [(M, D)] -> [(M, D)]
Line 1,238 ⟶ 1,231:
case dp of
Month -> fst
in
in L.filter ((`elem` uniques) . f)
mapFromPairs :: [(M, D)] -> Map Text [Text]
Line 1,259 ⟶ 1,245:
M.fromList $
((,) . fst . L.head) <*> fmap snd <$>
L.groupBy (on (==) fst) (L.
{{Out}}
<pre>[("July","16")]</pre>
|