[Haskell-cafe] Can't access map value with key.

michael rice nowgate at yahoo.com
Sat May 21 13:56:42 CEST 2011

That works, but I don't understand how I can print the map mp on one line but then when I try to extract a value out of it it's no longer a map but a triple. How can mp be a map on one line but not on another?
[1 of 1] Compiling Main             ( markov4.hs, markov4.o )Linking markov4 ...[michael at hostname ~]$ echo "I am lost." | ./markov4(fromList [(("\n","\n"),["I"]),(("\n","I"),["am"]),(("I","am"),["lost."])],("am","lost."),[])["I"][michael at hostname ~]$ ghciGHCi, version 6.12.3: http://www.haskell.org/ghc/  :? for helpLoading package ghc-prim ... linking ... done.Loading package integer-gmp ... linking ... done.Loading package base ... linking ... done.Loading package ffi-1.0 ... linking ... done.Prelude> :m + Data.MapPrelude Data.Map> let mp = fromList [(1,'a'),(2,'b'),(3,'c')]Loading package array- ... linking ... done.Loading package containers- ... linking ... done.Prelude Data.Map> :t mpmp :: Map Integer CharPrelude Data.Map> mpfromList [(1,'a'),(2,'b'),(3,'c')]Prelude Data.Map> mp ! 2'b'Prelude Data.Map> 

--- On Fri, 5/20/11, Arlen Cuss <celtic at sairyx.org> wrote:

From: Arlen Cuss <celtic at sairyx.org>
Subject: Re: [Haskell-cafe] Can't access map value with key.
To: "michael rice" <nowgate at yahoo.com>
Cc: haskell-cafe at haskell.org
Date: Friday, May 20, 2011, 11:36 PM

On Fri, 2011-05-20 at 19:04 -0700, michael rice wrote:
> markov4.hs:35:27:     Couldn't match expected type `Map k a'
> against inferred type `(Map Prefix [String], Prefix, [String])' 

ghc seems to believe `mp' here is not just a map, but the entire state
(which is what execState is meant to give you).

> type GeneratorState = State ((Map Prefix [String]),Prefix,[String])

The state is a triple, so you need to match to get the real map out:

> let (themap,_,_) = mp
> putStrLn $ show $ themap ! (non_word,non_word)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110521/5e4e3537/attachment.htm>

More information about the Haskell-Cafe mailing list