开发者

Help with Haskell IO typing

开发者 https://www.devze.com 2023-01-24 20:43 出处:网络
I need some help with types in Haskell... Here\'s the code I\'m working on: loadManyPeople :: [File开发者_如何转开发Path] →IO [Person]

I need some help with types in Haskell...

Here's the code I'm working on:

loadManyPeople :: [File开发者_如何转开发Path] →  IO [Person]
loadManyPeople fs = do
   return $ concat $ map loadPeople fs

loadPeople :: FilePath →  IO [Person]
loadPeople file = do
   lines ←  getLines file
   return $ map parsePerson lines

loadPeople is fine. I want loadManyPeople to load all the Persons from each file, then concat them into one list of Persons.

I'm new to Haskell and need help with getting the types to work out.

Thanks for the help. Alex


loadPeople gives you an IO [Person], so map loadPeople gives you a [IO [Person]], however to use concat, you'd need a [[Person]].

To fix this, you can use sequence, which is a function of type [IO a] -> IO [a], so you can do the following:

loadManyPeople fs = do
   manyPeople <- sequence $ map loadPeople fs
   return $ concat manyPeople

However there's a shortcut for using map and then sequence: mapM which has type (a -> IO b) -> [a] -> IO [b]. With mapM your code looks like this:

loadManyPeople fs = do
   manyPeople <- mapM loadPeople fs
   return $ concat manyPeople

This can be written more succinctly using Applicative:

import Control.Applicative
loadManyPeople fs = concat <$> mapM loadPeople fs
0

上一篇:

:下一篇

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号