[Haskell-begin] Using read, reads...

Hi folks, I am trying to make a program that reads a file which lines contain a series of 6 integers. -------------sample.in------------- 01 02 03 04 05 06 10 11 15 18 29 45 19 22 10 01 23 14 ----------------------------------------- right now I am using hGetLine and getting a string. Is is possible to use read to convert this in a list of Num? -- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc.

Em Dom, 2008-07-20 às 22:30 -0300, Rafael Gustavo da Cunha Pereira Pinto escreveu:
Hi folks,
Hello.
I am trying to make a program that reads a file which lines contain a series of 6 integers.
-------------sample.in------------- 01 02 03 04 05 06 10 11 15 18 29 45 19 22 10 01 23 14 -----------------------------------------
right now I am using hGetLine and getting a string. Is is possible to use read to convert this in a list of Num?
I don't know if it's the best option, but I'd do something as:
import Data.Char
getIntegers :: String -> [Int] getIntegers string | number == [] = [] | otherwise = read number : getIntegers rest where number :: String number = takeWhile isDigit string_ rest :: String rest = dropWhile isDigit string_ string_ :: String string_ = dropWhile (not . isDigit) string
Hope it helps. -- Marco Túlio Gontijo e Silva Página: http://marcotmarcot.googlepages.com/ Blog: http://marcotmarcot.blogspot.com/ Correio: marcot@riseup.net XMPP: marcot@jabber.org IRC: marcot@irc.freenode.net Telefone: 25151920 Celular: 98116720 Endereço: Rua Turfa, 639/701 Prado 30410-370 Belo Horizonte/MG Brasil

On Sun, Jul 20, 2008 at 10:30 PM, Rafael Gustavo da Cunha Pereira
Pinto
-------------sample.in------------- 01 02 03 04 05 06 10 11 15 18 29 45 19 22 10 01 23 14 -----------------------------------------
So, you got something like "10 11 15 18 29 45". So, we may want first to separate them into ["10", "11", "15", "18", "29", "45"]. Having this list, we simply apply read, constrained to String -> Int, to all its members, so we obtain [10, 11, 15, 18, 29, 45] :: [Int]. Now, translating what I said to Haskell: convert :: String -> [Int] convert = map read . words read :: FilePath -> IO [[Int]] read fp = (map convert . lines) `fmap` readFile fp (I've taken some "shortcuts" in the code above because that's how I'd write it. If you have trouble to understand it on a first glance, try to read carefully the docs of each function and mentally execute an example.) HTH, -- Felipe.

Felipe Lessa wrote:
Rafael Gustavo da Cunha Pereira Pinto wrote:
-------------sample.in------------- 01 02 03 04 05 06 10 11 15 18 29 45 19 22 10 01 23 14 -----------------------------------------
convert :: String -> [Int] convert = map read . words
read :: FilePath -> IO [[Int]] read fp = (map convert . lines) `fmap` readFile fp
Naming the new function read is a bad idea :)
Right now I am using hGetLine and getting a string. Is is possible to use read to convert this in a list of Num?
As Felipe demonstrates, putting the entire file into a String via readFile is often easier than reading it piecemeal with hGetLine . Regards, apfelmus

On Mon, Jul 21, 2008 at 4:25 AM, apfelmus
Naming the new function read is a bad idea :)
Hahaha :)! You're completely right, I tried to avoid clashing with readFile and forgot about the read function I've just used =). By the way, that's one of the reasons I always pass -Wall as GHC flag when compiling. -- Felipe.

On Mon, 21 Jul 2008 06:51:27 +0200, Felipe Lessa
On Sun, Jul 20, 2008 at 10:30 PM, Rafael Gustavo da Cunha Pereira Pinto
wrote: convert :: String -> [Int] convert = map read . words
read :: FilePath -> IO [[Int]] read fp = (map convert . lines) `fmap` readFile fp
If a single list of Ints is OK, the code can be much simpler:
read' :: FilePath -> IO [Int] read' fp = (map read . words) `fmap` readFile fp
-- Met vriendelijke groet, Henk-Jan van Tuyl -- http://functor.bamikanarie.com http://Van.Tuyl.eu/ --

Thanks, but what I needed was a line-by-line analyser.
I ended up with Felipe's solution.
Thank you all!
On Mon, Jul 21, 2008 at 16:14, Henk-Jan van Tuyl
On Mon, 21 Jul 2008 06:51:27 +0200, Felipe Lessa
wrote: On Sun, Jul 20, 2008 at 10:30 PM, Rafael Gustavo da Cunha Pereira Pinto
wrote: convert :: String -> [Int] convert = map read . words
read :: FilePath -> IO [[Int]] read fp = (map convert . lines) `fmap` readFile fp
If a single list of Ints is OK, the code can be much simpler:
read' :: FilePath -> IO [Int]
read' fp = (map read . words) `fmap` readFile fp
-- Met vriendelijke groet, Henk-Jan van Tuyl
-- http://functor.bamikanarie.com http://Van.Tuyl.eu/ --
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc.
participants (5)
-
apfelmus
-
Felipe Lessa
-
Henk-Jan van Tuyl
-
Marco Túlio Gontijo e Silva
-
Rafael Gustavo da Cunha Pereira Pinto