a monadic if or case?

Hal Daume III hdaume@ISI.EDU
Thu, 20 Feb 2003 13:03:43 -0800 (PST)


There is now a way to do this :).  What used to be the Haskell Array
Preprocessor is not the Haskell STate Preprocessor (STPP) and it supports
now many things:

  - sugared array reading/writing/updating
  - sugared hash table reading/writing/updating
  - monadic if
  - monadic case

Get it from http://www.isi.edu/~hdaume/STPP/

using stpp, you would write monadic case expressions as:

> mcase fun of
>   Nothing -> ...

etc, just as you wanted :).  mif works the same way (the 'm' prefix was
chosen to look like 'mdo').

Of course, it still supprots array reading/writing, such as:

  do a <- newArray (0,100) 0
     a[|5|]  <- 7
     a[|6|]  <- 8
     a[|6|] <<- a[|5|] * 2 + a[|6|]

furthermore, it supports hash table reading/writing (based on the hash
table implementation found at http://www.isi.edu/~hdaume/haskell/Util), as
in:

  do ht <- emptyHT
     ht{|"hello"|} <- "goodbye"
     print ht{|"hello"|}

All of this works both in the IO monad and the ST monad.

Comments/Suggestions/Bug reports to me please.

--
Hal Daume III

 "Computer science is no more about computers    | hdaume@isi.edu
  than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume

> > mp <- fun
> > case mp of
> >   Nothing -> deal with error
> >   Just p -> do something with p
> > 
> > where it would be much nicer to be able to just use
> > 
> > caseM fun of
> >   Nothing -> deal with error
> >   Just p -> do something with p
> > 
> > which would avoid confusion when reading the code as to whether the value
> > mp may be used later in the function.  Any ideas how to do something like
> > this?