[Haskell-cafe] Trying to return a map from State monad

michael rice nowgate at yahoo.com
Thu May 19 18:03:52 CEST 2011


I'm not sure what's going on BELOW. Was working with mapAccumL earlier and decided to move over to State monad for more control.
:m + Data.Map:m + Data.ListPrelude Data.List Data.Map> let f key new old = new ++ oldPrelude Data.List Data.Map> let moby2 = "Moby Dick is a great book. Moby Dick was a white whale."Prelude Data.List Data.Map> fst $ mapAccumL (\ (m,p) w -> ((insertWithKey' f p [w] m, (snd p, w)),"")) (singleton ("\n", "\n") [], ("\n","\n")) (words moby2)(fromList [(("\n","\n"),["Moby"]),(("\n","Moby"),["Dick"]),(("Dick","is"),["a"]),(("Dick","was"),["a"]),(("Moby","Dick"),["was","is"]),(("a","great"),["book."]),(("a","white"),["whale."]),(("book.","Moby"),["Dick"]),(("great","book."),["Moby"]),(("is","a"),["great"]),(("was","a"),["white"])],("white","whale."))
Michael

BELOW===========================
import Control.Monad.Stateimport Data.Map
type Prefix = (String,String)type GeneratorState = (Map Prefix [String],Prefix,[String])
non_word = "\n"
f key new old = new ++ old 
buildMap :: GeneratorState (Map Prefix [String])  buildMap = do (mp,(pfx1,pfx2),all@(w1:words)) <- get              if (Prelude.null all)                then  {- No more words. Return final map (adding non_word for prefix). -}                  return (insertWithKey' f (pfx1,pfx2) [non_word] mp)                else  {- Add word to map at prefix. Continue. -}                  put (insertWithKey' f (pfx1,pfx2) [w1] mp, (pfx2,w1), words)                  buildMap
==========================
Prelude> :l markov3.hs[1 of 1] Compiling Main             ( markov3.hs, interpreted )
markov3.hs:11:12:    `GeneratorState' is applied to too many type arguments    In the type signature for `buildMap':      buildMap :: GeneratorState (Map Prefix [String])Failed, modules loaded: none.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110519/c3776afc/attachment.htm>


More information about the Haskell-Cafe mailing list