I've been using computation expressions to simplify the construction of text parsers. It makes the code much more readable. Below is a simple example. (Probably a little too simple to show how much more readable this can make a more complex parser.)



By on 8/9/2010 9:57 AM ()

   By reading some descriptions they seem useful if I want to try and stay purely functional (which I do).  IE not allowing a mutable state variable.  Are monads just some trick to simulate mutability? 

Based on my understanding, I would say yes. A typical way of coding FP style is to use recursive call and pass the 'mutable state' as function parameter.

Monad just hide this mechanism and do it behind the scene(You can try to read the State Monad in Haskell to get a feel of it).

F#'s computation expression is basically doing the same thing, basically extract the boilerplate code out of certain computation.

I don't think monad in F# is that useful as one of the key benefit of monad(in Haskell) is many of those generalized helper functions that applies to ALL monads. This is simply not the case in F# due to the type system.

By on 8/8/2010 11:41 AM ()


chances are high, that you allready use them. They are called computation expressions (or so) and the best examples are seq {....} and of course the allmighty async{...} blocks in F# itself.

You see: async for example hides continuation-passing style composition from your eyes by using the nice let!, return!, ... (all with !) syntax.

Quite nice I think.

For your problem:

No I don't think monads will shine here - what you want is either recursive functions (pass the changed value instead of changing a variable) or mutating.

Really - for this example every functional approach will be slower - but if you just want to flex your functional-mind then go for recursion.

By on 8/4/2010 11:41 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us
Built with WebSharper