Instantly share code, notes, and snippets.

Embed
What would you like to do?
module Main
( main
) where
import Control.Arrow ((&&&))
import Control.Monad.Trans.State (StateT(StateT), evalStateT)
import Data.List (uncons, unfoldr)
import Data.Maybe (fromMaybe, listToMaybe)
import System.Environment (getArgs)
unconses :: [a] -> [(a, [a])]
unconses = unfoldr (((id &&& snd) <$>) . uncons)
triples :: Int -> [(Int, Int, Int)]
triples =
evalStateT ((,,) <$> StateT unconses <*> StateT unconses <*> StateT unconses) .
enumFromTo 1
pythagoreans :: Int -> [(Int, Int, Int)]
pythagoreans = filter (\(a, b, c) -> a ^ 2 + b ^ 2 == c ^ 2) . triples
main :: IO ()
main = do
n <- maybe 100 read . listToMaybe <$> getArgs
mapM_ print $ pythagoreans n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment