[Haskell-cafe] how to write a function to send a string to a tuple

Deniz Dogan deniz.a.m.dogan at gmail.com
Mon Mar 8 08:13:52 EST 2010


2010/3/8 Pradeep Wickramanayake <pradeep at talk.lk>:
> Hi,
>
>
>
> Im having problems with sending a string to a tuple.
>
>
>
> My string contains integers and strings
>
>
>
> The whole string stay as (“test,dfdf”,3,”dfsf”)
>
>
>
> sortList2 :: String -> String
>
> sortList2 (x:xs)
>
>                 | x == ',' = ""
>
>                 | otherwise = [x] ++ sortList2 xs
>
>
>
> The above function separating each words from the string
>
>
>
> Now I need to put them to a tuple
>
>
>
> putList :: String -> (Int, String, String, Int, Int)
>
> putList (x:xs)
>
>                                                 |xs /="" = sortList2 ++
> putList xs
>
>

I'm not sure what you're doing with the "sorting", but you could sort
of hack it using `read' as such:

Prelude> read "(3, \"hello\")" :: (Int, String)
(3, "hello")

Of course, this will crash if the input string is not parsable.

Prelude> read "(3, 'a')" :: (Int, String)
*** Exception: Prelude.read: no parse

-- 
Deniz Dogan


More information about the Haskell-Cafe mailing list