5 ms·
This seems pretty similar to the Haskell Maybe Monad. "Learn You a Haskell for Great Good!" has an example[0]: routine = case Just (0,0) of
by Arguggi 11y ago
This seems pretty similar to the Haskell Maybe Monad.
"Learn You a Haskell for Great Good!" has an example[0]:
routine =
case Just (0,0) of
Nothing -> Nothing
Just start -> case landLeft 2 start of
Nothing -> Nothing
Just first -> case landRight 2 first of
Nothing -> Nothing
Just second -> landLeft 1 second
becomes (using some 'do' syntactic sugar):
routine = do
start <- return (0,0)
first <- landLeft 2
second <- landRight 2 first
landLeft 1 second
Checking for Nothing is taken care of 'automatically' and if any of start, first, second or (landLeft 1 second) are equal to Nothing routine is equal to Nothing too.
[0] http://learnyouahaskell.com/a-fistful-of-monads#walk-the-line http://learnyouahaskell.com/a-fistful-of-monads#walk-the-lin...
- acjohnson55 11y agoIt's basically the same thing, except with that syntax, you have to label all the intermediate results
- bbcbasic 11y agoTo avoid labeling: routine :: Int -> Maybe Int routine n = Just n >>= (divisorFilter 3) >>= (divisorFilter 2) >>= (divisorFilter 5) -- Supporting function divisorFilter :: Int -> Int -> Maybe Int divisorFilter d n | n `mod` d == 0 = Just n | otherwise = Nothing -- Using do syntax routine' :: Int -> Maybe Int routine' n = do first <- divisorFilter 3 n second <- divisorFilter 2 first divisorFilter 5 second
- bbcbasic 11y agoWhich goes to show that what requires a 'new feature' in C# is just a freebie in Haskell, due to the powerful type system. See my other comment ITT for how to do this in a tighter way that is more like the C# syntax. (You just use the bind operator rather than wrapping it in a do)
- kristianp 11y agoThis isn't really the same thing. Haskell (and Ocaml) make you deal with the null case, whereas C# just lets you propagate the nulls. Haskell's way is safer, c# is just handy syntax.