I only know F#. I haven’t learned the other functional programming languages. All the examples that I have seen for monads only describe the bind and unit methods. F# has lots of keywords (e.g. let!, do!, etc.) that allow you to do different things within the same computational expression. This seemingly gives you more power than your basic bind and unit methods. Is this unique to F# or is it common across functional programming languages?
I only know F#. I haven’t learned the other functional programming languages. All the
Share
Yes, I think that the F# syntax for computation expressions is unique in that it provides direct syntactic support for different types of computations. It can be used for working with monoids, usual monads and also MonadPlus computations from Haskell.
I wrote about these in the introduction of my Master thesis. I believe it is quite readable part, so you can go to page 27 to read it. Anyway, I’ll copy the examples here:
Monoid is used just for concatenating values using some “+” operation (
Combine). You can use it for example for building strings (this is inefficient, but it demonstrates the idea):Monads are the familiar example that uses bind and return operations of comptuation expressions. For example maybe monad represents computations that can fail at any point:
Additive monads (aka
MonadPlusin Haskell) is a combination of the two. It is a bit like monadic computation that can produce multiple values. A common example is list (or sequence), which can implement both bind and combine:There are some additional keywords that do not directly correspond to any theoretical idea. The
usekeyword deals with resources andforandwhilecan be used to implement looping. The sequence/list comprehension actually useforinstead oflet!, because that makes much more sense from the syntactic point of view (andforusually takes some sequence – although it may be e.g. asynchronous).