1

Haskell IO system is super hard to understand for me so i have question : How to read from standard input to list ? I know that there is function getLine :: IO String and interact. But i do not know how to convert the input to list so I can use it in this three functions :

powerset []     = [[]]
powerset (x:xs) = xss ++ map (x:) xss
                               where xss = powerset xs
main = print $ powerset([1,2])

import Control.Monad(filterM)
p = filterM(const[True,False])
main = p[1,2]

main = subsequences([1,2])

I want to be able to write 1 2 3 and pass this values to the function. Can you tell/show how to do it ?

Extra question

Haskell is full of magic so i was wondering if it possible to use input directly in the function like this :

main = subsequences(some input magic here)
2
  • So you want to read a list of something from stdin? A list of what? Two things that might be useful: the Read class and readList. Commented Jun 22, 2015 at 19:48
  • 2
    What kind of input do you want to convert? How is it separated? What kind of data it is containing? Commented Jun 22, 2015 at 19:48

1 Answer 1

6

You may write:

main = readLn >>= print . subsequences

You will need to nail down the type to be read, for example by having a monomorphic subsequences or by annotating readLn. In ghci:

Data.List> (readLn :: IO [Integer]) >>= print . subsequences
[1,2,3]
[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

(I typed in the first and second lines -- both followed by enter -- and the third line was the result.)

For more details, you may enjoy one of the excellent resources below:

Sign up to request clarification or add additional context in comments.

6 Comments

Thank You, your answer is really usefull ! But can You show me how to make it possible to run this in rextester.com/runcode ?
@Cosaquee Stick import Data.List; main = (readLn :: IO [Integer]) >>= print . subsequences in the code box and [1,2,3] in the input box, then hit "Run".
If i copy this into rextester it always show : Process killed, because it ran longer than 10 seconds. I`m starting to know how IO system is working thanks to your comments !
@Cosaquee I get that message if I don't put anything in the "input" box. Have you done that part?
Any option that i can just type 1 2 3 and it will use it as the input for subsequences ? Now i need to manually type the list.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.