1

I want to get my input to be a list of lists. Each row of text has to be a list of strings and the full text will be the list of lists. How could I do this? I have this as the main:

main :: IO ()
main = interact (lines >>> foo >>> unlines)

So if this is the input:

These are random
Words of text
example example example

The result should be this:

[[These, are, random], [Words, of, text],[example, example, example]]
2
  • 3
    It is not really clear how you would convert the [String] list into a [[String]] list. Based on what condition? Do you want the "words" of that line? Commented Sep 17, 2017 at 16:06
  • I have added an example Commented Sep 17, 2017 at 16:36

1 Answer 1

1

Haskell has a function words :: String -> [String]. For example:

Prelude> words "These are random"
["These","are","random"]

Now we can do this for a list of lines by using map :: (a -> b) -> [a] -> [b]:

Prelude> map words ["These are random","Words of text","example example example"]
[["These","are","random"],["Words","of","text"],["example","example","example"]]

You can combine this with lines to extract the lines of the string first:

Prelude> (map words . lines) "These are random\nWords of text\nexample example example"
[["These","are","random"],["Words","of","text"],["example","example","example"]]

So if foo has type foo :: [[String]] -> [[String]], you can use:

main = interact (unlines . map unwords . foo . map words . lines)
Sign up to request clarification or add additional context in comments.

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.