Post by Kjell RilbePost by Craig Stuntz [TeamB]LINQ is a system for developing monads.
Could you please post a link or two about what a monad is?
Here's my jist: monads are a design pattern; any abstraction that obeys
the so-called monad rules is a monad, just like a class whose purpose in
life is to construct other classes using virtual methods is a factory.
Monads are all about being one-step removed from any operation you
perform on some data. Rather than performing operations on data
directly, you put the data into a wrapper object, and then ask the
wrapper object to do something to the data for you (in the form of a
function), and the wrapper object hands you back the wrapped result of
the transformation.
Because the wrapper object always mediates between the verb (the
function passed in) and the object of the verb (the data inside the
wrapper), it can subtly change the meaning the verb in an appropriate
way for its task.
For a List monad (suppose it's a list of integers), you might want to
"add 5", i.e. a function like 'x => x + 5' in C# lambda syntax. Well, a
list monad knows that it is wrapping a whole list of data rather than
just a single element, so it applies that function for every element in
the list, and returns a new list with that transformation applied.
For the famous Haskell IO monad, things are slightly different;
functional programming forbids side-effects, and IO is nothing if it's
not a side-effect. The IO monad side-steps this by logically
constructing an imperative program; i.e. when you pass functions into
the IO monad, and your mental model is that you are performing
imperative operations "now", the logical model of the monad is to take
in your functions and build up a nested set of instructions (in the form
of nested monads) such that when the entire Haskell program returns, the
return value is logically an imperative program which can then be
executed with side-effects. The type of every Haskell main is "-> IO
()", i.e. a monad wrapping Unit (which itself is roughly the same as
'void'), so it all works out type-wise.
So, with monadic IO, some function called putString("foo") returning "IO
()" [putString :: String -> IO ()], the putString function isn't
actually writing anything on-screen; it's returning some kind of
structure, wrapped inside an instance of IO (), that, when interpreted
after it is returned by 'main', will cause output on the console.
Of course, a Haskell implementation will probably do things differently
for efficiency, but it will take care not to break the type
abstractions.
-- Barry
--
http://barrkel.blogspot.com/