[Haskell-beginners] foldM with tuple and maybe
Dominik Bollmann
dominikbollmann at gmail.com
Mon Feb 3 15:55:12 UTC 2014
Or maybe what you really want is mapM instead of foldM? In this case,
your function f2 will look like this:
f2 :: [(String, String)] -> IO [Maybe String]
f2 xs = mapM f1 xs
And this will first map (=apply) your function f1 over all elements in
the given input list xs and then it will sequence this list, thus
creating a resulting list of type IO [Maybe String]
David McBride <toad3k at gmail.com> writes:
> You have a foldM function which takes a " a -> b -> m a " as a first
> argument. But you are passing it f1 which has a type (a, b) -> m a.
>
> Unfortunately I'm not sure what you were trying to do. The function
> you have just doesn't fit. :(
>
> Maybe you intended to have f1 :: String -> (String, String) -> IO
> String, and then go
>
> :t foldM f1 "" x
> foldM f1 "" [("a","b"),("c","d")] :: IO String
>
>
> ?
>
> On Mon, Dec 30, 2013 at 6:42 PM, Miro Karpis <miroslav.karpis at gmail.com> wrote:
>> Hi, please can you help me with following?
>>
>> I have a following function:
>> f1 :: (String, String) -> IO (Maybe String)
>>
>> which I would like to apply to a list of tuples.I have defined the function
>> like this, but that gives me error (below):
>>
>> f2 :: [(String, String)] -> IO (Maybe String)
>> f2 x = foldM f1 0 x
>>
>>
>> error:
>> Couldn't match expected type `IO (Maybe String)' with actual type `[b0] ->
>> m0 (String, String)' In the return type of a call of `foldM'
>>
>> thanks,
>> m.
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list