Skip to content

Instantly share code, notes, and snippets.

@seanstrom
Created September 7, 2015 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanstrom/4641b41b604905dea892 to your computer and use it in GitHub Desktop.
Save seanstrom/4641b41b604905dea892 to your computer and use it in GitHub Desktop.
Array Pattern Matching
module Main where
import Prelude
import Data.Maybe
import Data.Array
import Data.Either
import Data.Foreign
import Node.Yargs
import Node.Yargs.Setup
import Node.Yargs.Applicative
import Control.Monad.Eff
import Control.Monad.Eff.Console
data NoisyNum = Fizz | Buzz | FizzBuzz | Num Int
instance showNoisyNum :: Show NoisyNum where
show Fizz = "Fizz"
show Buzz = "Buzz"
show FizzBuzz = "FizzBuzz"
show (Num n ) = show n
fizzBuzz :: Int -> Int -> Int -> NoisyNum
fizzBuzz div1 div2 n
| isDivisible div1 n && isDivisible div2 n = FizzBuzz
| isDivisible div1 n = Fizz
| isDivisible div2 n = Buzz
| otherwise = Num n
isDivisible divisor n = n `mod` divisor == 0
app :: forall eff. Array String -> Eff (console :: CONSOLE | eff) Unit
app [] = return unit
app [x] = return unit
app [x, y] = return unit
app [x, y, z] = print $ show $ map (\n -> fizzBuzz 3 5 n) (1..100)
main = do
let setup = usage "$0 Word1 Word2"
<> example "$0 Hello -w World" "Say hello!"
runY setup $ app <$> yarg "w" ["word"] (Just "A word") (Right "At least one word is required") false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment