[Haskell-beginners] Type error when using splitOn function.

Francesco Ariis fa-ml at ariis.it
Wed Feb 22 13:31:16 UTC 2017


On Wed, Feb 22, 2017 at 09:02:22PM +0900, S. H. Aegis wrote:
> Thank you so much.
> 
> --makeRxDxList :: Functor f => f Text -> f [Text]
> Above signature comes from ghci using command :t
> My intention is
> makeRxDxList :: Text -> [[Text]]
> but, I got error, and try several times and below codes pass a complier.
> makeRxDxList rowRxDx = fmap (\x -> splitOn (pack ",") x) rowRxDx  -- This
> code pass a compile.
> and then, I run ghci, type :t, and got below signature.
> makeRxDxList :: Functor f => f Text -> f [Text]
> 
> Your kind answer says, I cannot help using fmap. right? ^^;
> Thanks again.

Then this:

    makeRxDxList :: Text -> [[Text]]
    makeRxDxList rowRxDx = fmap f (lines rowRxDx)
                -- you imported Prelude hiding map, so we will use fmap
        where
              f :: Text -> [Text]
              f x = splitOn (pack ",") x

should do (at least it typechecks).

GHC errors may not have the prettiest formatting ever, but they are
very useful, the most important bits being line & column of the offending
expression plus the "expected this but got that" part; get acquainted
with them!



More information about the Beginners mailing list