3

I'm writing a small program with IO actions in Haskell here is

module StackQuestion where

import Data.Map (Map, insert, fromList)

type Name = String
type Value = String

readValue :: Name -> IO String
readValue name = do     putStrLn name
                        value <- getLine
                        return value

addPair :: Name -> Value -> Map Name Value -> Map Name Value
addPair = insert

names = map show [1..5]
values = map (\char -> [char]) ['a'..'d']
initialMap = fromList (zip names values)

As you can see I have some initial map with values, and function which adds a pair to map, functions which reads a value.

How I can get a clear String value from readValue and pass it to another function ?

Or should I change type Value = String to type Value = IO String and use map Map String (IO String) ?

And if I have Map String (IO String) how can i process this map, how I can get any value depening on data in IO containter (maybe some function func :: (a->b) -> IO a -> b) For instance is there any way to compare IO String with the clear String ?

If I have function func I would have written

map :: Map String (IO String)
...
func (==) (map ! "key")

What is the strategy of working with IO values ?

1
  • 1
    Style: map (\char -> [char]) ['a'..'d'] --> map (:[])­ ['a'.­.'d'] Commented Jul 30, 2011 at 9:57

1 Answer 1

7

How I can get a clear String value from readValue and pass it to another function?

You can't; you'll have to manipulate readValue's result while in the IO monad.

{- read value for name and store both in map -}
readAndStore :: Name -> Map Name Value -> IO (Map Name Value)
readAndStore name m  =  do value <- readValue name
                           return $ insert name value m

The return function takes the result of the insert and puts it back neatly into the IO monad. This code exemplifies a general pattern for manipulating values with ordinary functions inside IO.

Or should I change type Value = String to type Value = IO String

No; consider what that would mean. IO String means a computation with possible side-effects (IO) with String-typed result. You would be mapping names to computations. (That's possible, but it's not what you mean.)

The example I've shown above uses IO (Map Name Value) instead; i.e., a computation in the IO monad with Map-typed result.

Sign up to request clarification or add additional context in comments.

3 Comments

@Heinrich Indeed. How about adding those typical questions to the tag wiki?
So, if I have any structure of type a which elements are read from standard input, file etc, i need to wrap it into the IO monad - somevar :: IO a and work with this values via liftM* functions ?
@Max K.: I didn't dare suggest the use of liftM as I wasn't sure how advanced your Haskell skills are, but yes, that's the most elegant solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.