Create a gist now

Instantly share code, notes, and snippets.

Embed
{-# LANGUAGE LambdaCase #-}
module Main
( main
) where
import Control.Monad (guard)
import Control.Monad.Trans.State (StateT (StateT), evalStateT)
import Data.List (unfoldr)
import Data.Maybe (listToMaybe)
import System.Environment (getArgs)
unconses :: [a] -> [(a, [a])]
unconses =
unfoldr $ \case
[] -> Nothing
(x:xs) -> Just ((x, xs), xs)
pythagoreans :: Int -> [(Int, Int, Int)]
pythagoreans n =
flip evalStateT [1 .. n] $ do
a <- StateT unconses
b <- StateT unconses
c <- StateT unconses
guard (a ^ 2 + b ^ 2 == c ^ 2)
return (a, b, c)
main :: IO ()
main = print =<< (pythagoreans . read <$>) . listToMaybe <$> getArgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment