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
alt2 :: Int -> Maybe Int
alt2 41 = Nothing
alt2 x = return (x + 1)
plusOne :: Int -> Maybe Int
plusOne x = return (x + 1)
plusOne :: Int -> Maybe Int
plusOne x = Just (x + 1)
applicativeEx :: Maybe Int
applicativeEx = (+ 1) <*> Just 42 -- Just 43
fmapEx :: Maybe Int
fmapEx = fmap (Just (+ 1)) (Just 42) -- Does not compile
fmapEx :: [Int] -- A function returning a list
fmapEx = fmap (+ 1) [1, 2, 3] -- [2, 3, 4]
fmapEx :: Maybe Int
fmapEx = fmap (+ 1) Nothing -- Nothing
fmapEx :: Maybe Int -- A function returning a Maybe Int
fmapEx = fmap (+ 1) (Just 42) -- Just 43
applicativeEx :: Maybe Int
applicativeEx = (+ 1) <$> Just 42 -- Just 43
applicativeEx :: [Int]
applicativeEx = [(+ 1)] <*> [1, 2, 3] -- [2, 3, 4]