Re:Exploring the C# 6 Null Propagation Operator
by kristianp on Sunday April 19, 2015 @06:44PM
(#9405189)
This 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.
Re:Exploring the C# 6 Null Propagation Operator
by bbcbasic on Sunday April 19, 2015 @05:14PM
(#9404906)
Which 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)
Re:Exploring the C# 6 Null Propagation Operator
by acjohnson55 on Sunday April 19, 2015 @02:44PM
(#9404258)
It's basically the same thing, except with that syntax, you have to label all the intermediate results
Re:Exploring the C# 6 Null Propagation Operator
by bbcbasic on Sunday April 19, 2015 @05:06PM
(#9404874)
To 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
"Learn You a Haskell for Great Good!" has an example[0]:
becomes (using some 'do' syntactic sugar): 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-lin...