Skip to content

Instantly share code, notes, and snippets.

@mmagm
Last active August 29, 2015 14:14
Show Gist options
  • Save mmagm/0285f44ee51ddc2190c0 to your computer and use it in GitHub Desktop.
Save mmagm/0285f44ee51ddc2190c0 to your computer and use it in GitHub Desktop.
import Control.Monad
import Data.List
distinct :: Eq a => [a] -> Bool
distinct [] = True
distinct (x:xs) = case x `elem` xs of
True -> False
False -> distinct xs
multipleDwelling :: [[(String, Integer)]]
multipleDwelling = [ [ ("Baker", baker)
, ("Cooper", cooper)
, ("Fletcher", fletcher)
, ("Miller", miller)
, ("Smith", smith) ] | baker <- [1..5]
, cooper <- [1..5]
, fletcher <- [1..5]
, miller <- [1..5]
, smith <- [1..5]
, distinct [baker, cooper, fletcher, miller, smith]
, (baker /= 5)
, (cooper /= 1)
, (fletcher /= 5)
, (fletcher /= 1)
, (miller > cooper)
, abs (smith - fletcher) /= 1
, abs (fletcher - cooper) /= 1 ]
multipleDwellingDo :: [[(String, Int)]]
multipleDwellingDo = do
baker <- [1..5]
cooper <- [1..5]
fletcher <- [1..5]
miller <- [1..5]
smith <- [1..5]
guard (distinct [baker, cooper, fletcher, miller, smith])
guard (baker /= 5)
guard (cooper /= 1)
guard (fletcher /= 5)
guard (fletcher /= 1)
guard (miller > cooper)
guard (abs (smith - fletcher) /= 1)
guard (abs (fletcher - cooper) /= 1)
return [
("Baker" , baker)
, ("Cooper" , cooper)
, ("Fletcher", fletcher)
, ("Miller" , miller)
, ("Smith" , smith)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment