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, sortBysortOn)
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"
 
-- QUERY FUNCTIONS -----------------------QUERY FUNCTIONS----------------------
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
(\(m, _) ->
(if bln
then id
else not)
(m `elem` months))
xs
 
uniquePairing :: DatePart -> [(M, D)] -> [(M, D)]
Line 1,238 ⟶ 1,231:
case dp of
Month -> fst
_Day -> snd
in bindPairs(\md ->
xs let dct = f md
(\md -> uniques =
let dct :: M L.Map Text [Text]filter
dct ((1 ==) f. mdL.length . fromJust . flip M.lookup dct)
uniques = (keys dct)
in L.filter ((`elem` uniques) . f) L.filterxs)
(liftA2 ((1 ==,) .mapFromPairs L.length(mapFromPairs . fromJustfmap . flip M.lookupswap) dctxs)
(keys dct)
in L.filter ((`elem` uniques) . f) xs)
 
bindPairs :: [(M, D)]
-> ((M.Map Text [Text], M.Map Text [Text]) -> [(M, D)])
-> [(M, D)]
bindPairs xs f = f (mapFromPairs xs, mapFromPairs (swap <$> xs))
 
mapFromPairs :: [(M, D)] -> Map Text [Text]
Line 1,259 ⟶ 1,245:
M.fromList $
((,) . fst . L.head) <*> fmap snd <$>
L.groupBy (on (==) fst) (L.sortBy (comparingsortOn fst) xs)</lang>
{{Out}}
<pre>[("July","16")]</pre>
9,655

edits