
Please, what is the matter with Map.lookup ? In ghc-6.4.1, I try -------------------------------
ghci -package base
/ _ \ /\ /\/ __(_) / /_\// /_/ / / | | GHC Interactive, version 6.4.1, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. Loading package base-1.0 ... linking ... done. Prelude> Data.Map.singleton 'a' 'b' {'a':='b'} Prelude> Data.Map.lookup 'a' $ Data.Map.singleton 'a' 'b' Prelude> ------------------------------- Must not it print Just 'b' instead of printing emptyness? Further: ---------- Prelude> Data.Map.lookup 'b' $ Data.Map.singleton 'a' 'b' *** Exception: user error (Data.Map.lookup: Key not found) ---------- Instead, must not it print Nothing ? Thank you in advance for explanation, ----------------- Serge Mechveliani mechvel@botik.ru

Hi Serge,
Prelude> Data.Map.singleton 'a' 'b' {'a':='b'}
Prelude> Data.Map.lookup 'a' $ Data.Map.singleton 'a' 'b'
Prelude> -------------------------------
Must not it print Just 'b' instead of printing emptyness? Further:
---------- Prelude> Data.Map.lookup 'b' $ Data.Map.singleton 'a' 'b'
*** Exception: user error (Data.Map.lookup: Key not found) ----------
Instead, must not it print Nothing ?
'Data.Map.lookup' has the return type 'm a', that is returns the value within an arbitrary monad of type m, which need not even be a member of the class 'Show'. You need to further specify the type of the monad. '(Data.Map.lookup 'a' $ Data.Map.singleton 'a' 'b')::Maybe Char' does what you expect. Regards, Jens

Serge D. Mechveliani schrieb:
Please, what is the matter with Map.lookup ?
Map.lookup results in any monad. At the ghci prompt that is the IO monad (and not Maybe). Prelude> :t Data.Map.lookup Data.Map.lookup :: forall a (m :: * -> *) k. (Ord k, Monad m) => k -> Data.Map.Map k a -> m a Prelude> x <- Data.Map.lookup 'a' $ Data.Map.singleton 'a' 'b' <interactive>:1:0: Warning: Defined but not used: `x' Prelude> x 'b' (N.B. this warning is confusing, but I've set -Wall) Supply a type signature if you want a different result: Prelude> Data.Map.lookup 'a' $ Data.Map.singleton 'a' 'b' :: Maybe Char Just 'b' HTH Christian
participants (3)
-
Christian Maeder
-
Jens Fisseler
-
Serge D. Mechveliani