I have a String like "1 2 3 4 5". How can I convert it into a list of integers like [1,2,3,4,5] in Haskell? What if the list is "12345"?
I have a String like 1 2 3 4 5 . How can I
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
Here we use
wordsto split"1 2 3 4 5"on whitespace so that we get["1", "2", "3", "4", "5"]. Thereadfunction can now convert the individual strings into integers. It has typeRead a => String -> aso it can actually convert to anything in theReadtype class, and that includesInt. It is because of the type variable in the return type that we need to specify the type above.For the string without spaces, we need to convert each
Charinto a single-element list. This can be done by applying(:"")to it — aStringis just a list ofChars. We then applyreadagain like before: