Cheryl's birthday: Difference between revisions
Content deleted Content added
Realize in F# |
→{{header|JavaScript}}: 2nd and 3rd filters generalised |
||
Line 196: | Line 196: | ||
// (A's month contains only one remaining day) |
// (A's month contains only one remaining day) |
||
// ( |
// (3 :: A "Then I also know") |
||
uniquePairing(true)(fst)( |
|||
// among the days with unique months, |
// among the days with unique months, |
||
// (B's day is paired with only one remaining month) |
// (B's day is paired with only one remaining month) |
||
// ( |
// (2 :: B "I know now") |
||
uniquePairing(true)(snd)( |
|||
// excluding months with unique days, |
// excluding months with unique days, |
||
// (A's month is not among those with unique days) |
// (A's month is not among those with unique days) |
||
// ( |
// (1 :: A "I know that Bernard does not know") |
||
monthsWithUniqueDays(false)( |
monthsWithUniqueDays(false)( |
||
// from the given month-day pairs: |
// from the given month-day pairs: |
||
// (Cheryl's list) |
// (0 :: Cheryl's list) |
||
map(x => tupleFromList(words(strip(x))), |
map(x => tupleFromList(words(strip(x))), |
||
splitOn(/,\s+/, |
splitOn(/,\s+/, |
||
Line 226: | Line 226: | ||
)) |
)) |
||
); |
); |
||
// monthsWithUniqueDays :: Bool -> [(Month, Day)] -> [(Month, Day)] |
// monthsWithUniqueDays :: Bool -> [(Month, Day)] -> [(Month, Day)] |
||
const monthsWithUniqueDays = blnInclude => xs => |
const monthsWithUniqueDays = blnInclude => xs => |
||
bindPairs(xs, |
|||
md => { |
|||
const |
|||
m = fst(md), |
|||
d = snd(md), |
|||
days = filter( |
|||
), |
k => 1 === length(d[k]), |
||
Object.keys(d) |
|||
), |
|||
months = filter( |
|||
) |
k => (blnInclude ? id : not)( |
||
0 < length(intersect(m[k], days)) |
|||
) |
), |
||
⚫ | |||
⚫ | |||
); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
// |
// uniquePairing :: Bool -> ((a, a) -> a) -> |
||
// -> [(Month, Day)] -> [(Month, Day)] |
|||
const daysWithUniqueMonths = blnInclude => xs => { |
|||
const uniquePairing = blnInc => f => xs => |
|||
⚫ | |||
bindPairs(xs, |
|||
md => { |
|||
const |
|||
dct = f(md), |
|||
matches = filter( |
|||
) |
k => (blnInc ? id : not)( |
||
1 === length(dct[k]) |
|||
) |
), |
||
⚫ | |||
⚫ | |||
); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
// |
// bindPairs :: [(Month, Day)] -> (Dict, Dict) -> [(Month, Day)] |
||
const |
const bindPairs = (xs, f) => f( |
||
Tuple( |
|||
dictFromPairs(fst)(snd)(xs), |
|||
dictFromPairs(snd)(fst)(xs) |
|||
) |
|||
k => (blnInclude ? id : not)( |
|||
); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return filter(tpl => elem(fst(tpl), months), xs); |
|||
⚫ | |||
// dictFromPairs :: ((a, a) -> a) -> ((a, a) -> a) -> [(a, a)] -> Dict |
// dictFromPairs :: ((a, a) -> a) -> ((a, a) -> a) -> [(a, a)] -> Dict |