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.

2 Answers 2

3

You just need an extra dot:

return . transformContent . TaggedContent (id, attrs) =<< readFile f
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.