Skip to content

Instantly share code, notes, and snippets.

@chrisdone
Last active March 1, 2025 02:14
Show Gist options
  • Save chrisdone/f02bd7dbcc688d59a35d515ccfe07174 to your computer and use it in GitHub Desktop.
Save chrisdone/f02bd7dbcc688d59a35d515ccfe07174 to your computer and use it in GitHub Desktop.
Apple

Sample types:

isDigit :: Monad f => Char -> f Bool
any :: Monad f => (a -> f Bool) -> [a] -> f Bool
reverse :: Monad f => [a] -> f [a]
map :: Monad f => (a -> f b) -> [a] -> f [b]
"hello" :: Monad f => f String
putStrLn :: (MonadIO f) => String -> f ()
show :: (Show a,Monad f) => a -> f String
identify :: Monad m => Identity a -> m a
identify = return . runIdentity

Translation:

apply     | haskell

f x y     | do x' <- x
          |    y' <- y
          |    f x y

if f x    | do x' <- f x
 then y   |    if x'
 else z   |       then y
          |       else z

case f x of | do x' <- f x
  _ -> _    |    case x of ...

do x <- a   | do x <- fmap return a
   y <- b   |    y <- fmap return b

Examples:

main :: IO ()
main = putStrLn (show (if any isDigit "hello"
                          then map isDigit "hello"
                          else reverse "hello"))
main :: IO ()
main = putStrLn (identify (show (if any isDigit "hello"
                                    then map isDigit "hello"
                                    else reverse "hello")))
main :: IO ()
main = do
  name <- getLine
  age <- getLine
  putStrLn ("Your name is " ++ name ++ " and your age is " ++ age)
main :: IO ()
main = do
  putStrLn ("Your name is " ++ getLine ++ " and your age is " ++ getLine)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment