Skip to content

Instantly share code, notes, and snippets.

@meithecatte
Created July 30, 2020 21:49
Show Gist options
  • Save meithecatte/e443203253e00fb4518a0aa267e02621 to your computer and use it in GitHub Desktop.
Save meithecatte/e443203253e00fb4518a0aa267e02621 to your computer and use it in GitHub Desktop.
Checking if a number is even in Haskell
import Data.Semigroup
data Permutation = Permutation [Int] deriving (Eq, Show)
permute :: Permutation -> [a] -> [a]
permute (Permutation p) xs = (xs !!) <$> p
instance Semigroup Permutation where
a <> Permutation b = Permutation $ permute a b
stimes = stimesMonoid
instance Monoid Permutation where
mempty = Permutation [0..9]
evening = Permutation [6,9,4,2,0,1,8,5,7,3]
isEven x = head $ permute (stimes (5 * x) evening) $ True:False:undefined
main = mapM_ (\x -> putStrLn $ show x ++ " is " ++
if isEven x then "even" else "odd") [0..30]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment