Skip to content

Instantly share code, notes, and snippets.

@exarkun
Created June 7, 2023 13:05
Show Gist options
  • Save exarkun/1e84491d1e3da145e571ba310e7c6245 to your computer and use it in GitHub Desktop.
Save exarkun/1e84491d1e3da145e571ba310e7c6245 to your computer and use it in GitHub Desktop.
module Main where
import System.Environment (getArgs)
data A = A deriving Show
data B = B deriving Show
parseA :: String -> Maybe A
parseA "A" = Just A
parseA _ = Nothing
parseB :: String -> Maybe B
parseB "B" = Just B
parseB _ = Nothing
data AorB = ItsA A | ItsB B deriving Show
parseAorB :: String -> Maybe AorB
parseAorB s = case parseA s of
Just a -> Just $ ItsA a
Nothing -> case parseB s of
Just b -> Just $ ItsB b
Nothing -> Nothing
concatA :: A -> String -> String
concatA A = ('A':)
concatB :: B -> String -> String
concatB B = ('A':)
main :: IO ()
main = do
[s] <- getArgs
case parseAorB s of
Nothing -> print "Sorry"
Just x -> print x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment