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

Marco Túlio Gontijo e Silva marcot at riseup.net
Mon Jul 21 00:40:49 EDT 2008


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 at riseup.net
XMPP: marcot at jabber.org
IRC: marcot at irc.freenode.net
Telefone: 25151920
Celular: 98116720
Endereço:
 Rua Turfa, 639/701
 Prado 30410-370
 Belo Horizonte/MG Brasil



More information about the Beginners mailing list