Grafting Functional Support on Top of an Imperative Language

Lars Noschinski lars-2008-1 at usenet.noschinski.de
Sat Apr 5 13:14:28 PDT 2008


* Jarrett Billingsley <kb3ctd2 at yahoo.com> [08-04-05 05:15]:
>I'm not one to be listened to, but I think the idea behind monads is to wrap 
>an inherently impure action (like IO) in a functional shell.  In an 
>imperative language, you do something like:

Monads are an imperative sublanguage. IO Monads (which are the ones able
to do impure things) are safe, because there is no way to extract the
information wrapped in the monad.

So if you do a read, you don't get a "String", you get an "IO String"
(i.e. an IO monad containing a string). But you can apply functions to
the IO monad, which operate on the wrapped value and return a new IO
monad (for example a method parsing the String to an integer).

For the functional part of the program (all things outside of the IO
monad), the return value of e.g. readLine is always the same - an IO
monad representing the action of reading a line of input and therefore
it is pure.



More information about the Digitalmars-d mailing list