by andolanra · 15y ago · 0 comments
sequence [putStrLn "foo", putStrLn "bar", putStrLn "baz"]
will print those three strings on their own lines. However, I can use the same function for Haskell's equivalent of null: sequence [Just 3, Just 8, Just 5] -- results in Just [3, 8, 5]
sequence [Just 3, Nothing, Just 5] -- results in Nothing, because Nothing is
-- propagated if it occurs
or for nondeterminism sequence [[1, 2], [3, 4], [5, 6]] -- results in every possible list whose first
-- element is drawn from the first list, whose
-- second element is drawn from the second
-- list, and whose third element is drawn from
-- the third list.
or for any other effect that Haskell has. A lot of learning Haskell has to do with looking at the abstractions Haskell offers (monads are only one; you also get things like arrows, functors, applicative functors, &c) and understand how you can phrase your problems in terms of them and consequently use Haskell's abstractly written functions to your favor. A great practical example here is parsing; parser combinators can be expressed as monads or as applicative functors, both of which make your parsing code very small and quite natural to read. For example, the following is a function to parse strings like "(552,864)" using the Parsec parser combinator library: parseDigit = oneOf ['0'..'9']
parseInt = many parseDigit
parsePairOfInts = do
string "("
p1 <- parseInt
string ","
p2 <- parseInt
string ")"
return (p1, p2)The original question was about the mechanisms, not the style or libraries.
I don't really know haskell, so correct me if I'm wrong.
They allow return-type polymorphism, which ordinary OO interfaces do not. And the Monad type-class requires this feature.
Basically, when you call the "return" function in Haskell, (e.g: return 5) -- the code being called depends on the type of the result. The type of the result is determined from the context (by type-inference or rarely, type annotations). In Ruby or Python, there's no easy/direct way to encode something like "return" such that it works with any Monad instance.
One thing is impossible, though: defining two methods that differ only in return type, something like
Most important point:
Something like
int i = parse(String source);
long l = parse(String source);
boolean b = parse(String source);
is possible in Haskell, but not in Python, and barely imaginable in Java.As for continuation passing, Python can do explicit continuation passing, but it has caveats:
* No TCO means it doesn't work well in larger cases
* Explicit CPS is ugly. yield-based continuation passing is restricted. You can't implement non-determinism via generators.
So using yield, you can implement most monad instances in Python (excluding non-determinism).
But Haskell can goes beyond a particular/useful monad instance (e.g: Parsing) and has a Monad generalization (basically a type-class that allows lots of libraries to be written such that they work with any monad, not just this parsing monad).
Additionally, monad transformers are a technique to compose monad types together to form a new monad that can do the things each of the monads can.
For example, I can compose the non-determinism monad with the parsing monad to get a non-determinism parser that yields various possible parse results.