[Haskell-beginners] Signature of monadic functions

divyanshu ranjan idivyanshu.ranjan at gmail.com
Thu Oct 17 10:47:26 UTC 2013


Hi,

Map.mapKeys has constraint (Ord k2) and IO a does not provide instance of Ord.
One way to make a way around this, I can come up with is :

import Data.Map

test_a :: Map Int Int
test_a = fromList [ (i, i) | i <- [1..10] ]

print_a :: Int -> IO Int
print_a a = ( putStrLn.show $ a )  >> return a

test_b :: IO (Map Int Int)
test_b = fmap fromList $ mapM (\ (a, b) -> (do
                                              t <- print_a a
                                              return (t, b) ))  $ toList test_a

Thanks
Divyanshu Ranjan

On Thu, Oct 17, 2013 at 2:43 PM, Lorenzo Tabacchini <lortabac at gmx.com> wrote:
> I think I am looking at the problem in a wrong way...
>
> What I want to do is using higher-order functions with IO (or an IO-based
> transformer).
>
> For example, let's say I want to apply a function to all the keys in a Map.
> With pure functions, I would do:
>   Map.mapKeys doSomething myMap
>
> But let's suppose the function has a signature:
>   doSomething :: a -> IO b
>
> Is there an easy way to apply it?
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list