Created
May 14, 2018 09:12
-
-
Save mjhopkins/db35134a4ad26967e1376f3d27caac01 to your computer and use it in GitHub Desktop.
And patterns in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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