Skip to content

Instantly share code, notes, and snippets.

View teivah's full-sized avatar
🧮
Current status: teivah/majorana

Teiva Harsanyi teivah

🧮
Current status: teivah/majorana
View GitHub Profile
plusOne :: Maybe (Int -> Int)
plusOne = Just (+ 1)
plusOne :: Int -> Maybe Int
plusOne x = Just (x + 1)
plusOne :: Int -> Int
plusOne x = x + 1
withMonad :: Int -> String -> Maybe String
withMonad age name = do
validateAge age
s <- return (greet name)
return (greet s) -- Just "Hello Hello John"
withMonad :: Int -> String -> Maybe String
withMonad age name = do
validateAge age
return (greet name)
withMonad :: Int -> String -> Maybe String
withMonad age name = validateAge age >>= \_ -> return (greet name)
withApplicative :: Int -> String -> Maybe String
withApplicative age name = greet <$> (Just name) <*> validateAge age
validateAge :: Int -> Maybe Int
validateAge age
| age >= 18 = return age
| otherwise = Nothing
greet :: String -> String
greet name = "Hello " ++ name
divide :: Float -> Float -> Maybe Float
divide _ 0 = Nothing
divide x y = return (x / y)
plusOne :: Int -> Maybe Int
plusOne x = return (x + 1)
monadEx :: Maybe Int
monadEx = Just 42 >>= plusOne -- Just 43