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

Ozgur Akgun ozgurakgun at gmail.com
Fri Mar 5 11:32:34 EST 2010


Let's first discuss what a tuple is.
Here[1] it says, "A tuple is a fixed-size collection of values, where each
value can have a different type."

So they are of fixed-size and they can have values of different types. I am
guessing the reason you want to use a tuple os that you want your collection
object to be able to handle different types. But what about the fixed size
bit?

If you want to just have those values in a collection, the easiest thing you
can do is putting them into a list. The problem with lists is that the
elements in them need to be of same type. If you just want every word, as a
string, it is easy. If you want to have _either strings or ints_ within the
list, then you need to define a data type which holds either strings or
ints, and have a list of this type.

Just an example,

import Data.Char(isDigit)

testString = "hello world 13 i am a new 37 developer 82"

data StringOrInt = S String | I Int
    deriving (Eq,Ord,Show)

readInt :: String -> Int
readInt = read

toStringOrInt :: String -> StringOrInt
toStringOrInt x
    | all isDigit x = I (readInt x)
    | otherwise     = S x


test = map toStringOrInt (words testString)

-- Output: [S "hello",S "world",I 13,S "i",S "am",S "a",S "new",I 37,S
"developer",I 82]



Note: I changed the separator from commas to spaces, for the sake of
easiness and understandablity. Hope this helps in some way.


[1] http://book.realworldhaskell.org/read/types-and-functions.html

On 5 March 2010 15:52, Pradeep Wickramanayake <pradeep at talk.lk> wrote:

>  As luke requested
>
>
>
> Here is the codes and some more brief explanation. Please help me
>
>
>
>  getItemFile :: IO String
>
>  getItemFile  =
>
>                                  do
>
>                                                  test <- readFile
> "input.txt"
>
>                                                  return test
>
>
>
> im taking a file data to a string
>
>
>
> it contains string like “hello,world,I,am,a,new,developer”
>
>
>
> so I need to put these each to a tuple. Because the content have Integers
> and Strings
>
>
>
> sortList2 :: String -> String
>
> sortList2 (x:xs)
>
>                 | x == ',' = ""
>
>                 | otherwise = [x] ++ sortList2 xs
>
>
>
> Im breaking word by word from this above function
>
>
>
> Now I need to send it to a tuple. Can someone help me how to do it. Is this
> possible. Using recursion im checking each word till “,” occurs
>
> And taking that string and passing it to the tuple.
>
>
>
> Can someone please help me to do it.
>
>
>
> Please
>
>
>
>
>
>
>
>
> __________ Information from ESET NOD32 Antivirus, version of virus
> signature database 4918 (20100305) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


-- 
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100305/3356976f/attachment.html


More information about the Haskell-Cafe mailing list