[Haskell-cafe] Re: How to put data from a string to a tuple

Tim Attwood timothyea at comcast.net
Fri Mar 5 07:19:32 EST 2010


> Hi,
>
> Im self learner in Haskell. And im stuck in a  small place which I tried
> searching in google but couldn't find
> Proper answer
>
> I have some values in a string, Im removing them one by one from the 
> string
> (word by word) and I want to put them in a tuple. Because it contain
> Integers and Strings.
>
> Can someone help me

Maybe something like this?

pairs :: String -> [(String,Integer)]
pairs s = f (words s) [] where
    f [] acc = reverse acc
    f (i:[]) acc = reverse ((i,undefined):acc)
    f (i:v:r) acc = f r ((i,read v):acc)

Key points to look at:
    accumulator (acc with cons : ),
    recursion (f),
    pattern matching (i:v:r),
    undefined (if the data is not pairs).




More information about the Haskell-Cafe mailing list