Skip to content

Instantly share code, notes, and snippets.

@mjhopkins
Created May 14, 2018 09:12
Show Gist options
  • Save mjhopkins/db35134a4ad26967e1376f3d27caac01 to your computer and use it in GitHub Desktop.
Save mjhopkins/db35134a4ad26967e1376f3d27caac01 to your computer and use it in GitHub Desktop.
And patterns in Haskell
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
module AndPatterns where
pattern DivBy3 :: Integral i => i
pattern DivBy3 <- ((`mod` 3) -> 0)
pattern DivBy5 :: Integral i => i
pattern DivBy5 <- ((`mod` 5) -> 0)
pattern (:&:) :: a -> a -> a
pattern (:&:) i j <- (\x -> (x,x) -> (i,j))
fizzbuzz :: Int -> String
fizzbuzz = \case
DivBy3 :&: DivBy5 -> "fizzbuzz"
DivBy3 -> "fizz"
DivBy5 -> "buzz"
i -> show i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment