1

I have the following:

import Data.List

data Content
  = TaggedContent (String, [String]) String
  | Null

processContent :: Content -> IO Content
processContent c@(TaggedContent (id, attrs) text) =
  case stripPrefix "include=" a of
       Just f     -> return . TaggedContent (id, attrs) =<< readFile f
       Nothing    -> return c
  where a = head(attrs)
processContent x = return x

transformContent :: Content -> Content
transformContent x = x -- (details of implementation not necessary)

I would like to compose transformContent with the TaggedContent constructor; that is, something like

       Just f     -> return . transformContent TaggedContent (id, attrs) =<< readFile f

However, this will not compile.

I am new to Haskell and am trying to understand the correct syntax.

cm007
  • 1,352
  • 4
  • 20
  • 40

2 Answers2

3

You just need an extra dot:

return . transformContent . TaggedContent (id, attrs) =<< readFile f
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
1

Daniel Wagner explained how to perform a minimal modification to get your code to compile. I will comment on a few common alternatives.

Code such as

return . g =<< someIOAction

is often also written as

fmap g someIOAction

or

g `fmap` someIOAction

or, after importing Control.Applicative

g <$> someIOAction

In your specific case, you could use:

transformContent . TaggedContent (id, attrs) <$> readFile f
chi
  • 111,837
  • 3
  • 133
  • 218