module Main where import Maybe (isJust) -- all possible moves moves :: Int-> [Int] moves x = filter ((<) 0) (map (\n-> x- n) [1,2,3]) -- wins x returns Just n if we can win by taking n wins :: Int-> Maybe Int wins x = case filter mustlose (moves x) of m:_ -> Just (x-m) [] -> Nothing -- as was mustlose :: Int-> Bool mustlose x = x==1 || all (isJust.wins) (moves x) -- only testing main = mapM_ (\n-> putStrLn (show n ++ case wins n of Just m -> " wins with "++ show m Nothing -> " loses")) [1..]