This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
```Frank A. Christoph <christo@nextsolution.co.jp> writes:

> But monads are also used in Opal, which is an eager language, to keep the
> base language pure from side-effects.
>
> Also, I often use monads in Haskell which have no side-effects at all. For
> example, I might use a non-imperative state transformer to "lay out the
> plumbing" for an algorithm, i.e., to avoid passing variables around
> explicitly; the error monad, which is a monad over what in Ocaml corresponds
> to the option type (functor), is also extremely useful, and has no
> side-effects either.

I spent some time last year working with monadic parsers--this is
another really nice way to use monads (especially if you have monad
comprehensions.)  An example, using something like what I wrote to do

let char c = << x | x <- item; x = c >>
let digit = << x | x <- item; x >= '0' && x <= '9' >>
let rec many p = << x::xs | x <- p; xs <- many p >>
let number = many digit

let bracket l p r = << x | _ <- l; x <- p; _ <- r >>
let rec seq p s = << x :: xs | x <- p; _ <- s; xs <- seq p s >>
|-| << [x] | x <- p >>

let number_list = bracket (char '[') (seq number (char ',')) (char ']')

And then number_list would parse something like "[1,2,3,4,56]" into
the Caml value [1; 2; 3; 4; 56].

Unfortunately, this kind of thing (along with other higher-order
combinator stuff for, as an example, formatted printing) doesn't work
that well in ML because of the value restriction.  :(

John.

```