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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Rafael Gustavo da Cunha Pereira Pinto
  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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20080720/8f36c043/attachment.htm
Reply | Threaded
Open this post in threaded view
|

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

Marco Túlio Gontijo e Silva
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: [hidden email]
XMPP: [hidden email]
IRC: [hidden email]
Telefone: 25151920
Celular: 98116720
Endere?o:
 Rua Turfa, 639/701
 Prado 30410-370
 Belo Horizonte/MG Brasil

Reply | Threaded
Open this post in threaded view
|

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

Felipe Lessa
In reply to this post by Rafael Gustavo da Cunha Pereira Pinto
On Sun, Jul 20, 2008 at 10:30 PM, Rafael Gustavo da Cunha Pereira
Pinto <[hidden email]> wrote:
>
> -------------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.
Reply | Threaded
Open this post in threaded view
|

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

Heinrich Apfelmus
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

Reply | Threaded
Open this post in threaded view
|

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

Felipe Lessa
On Mon, Jul 21, 2008 at 4:25 AM, apfelmus <[hidden email]> wrote:
> 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.
Reply | Threaded
Open this post in threaded view
|

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

Henk-Jan van Tuyl
In reply to this post by Felipe Lessa
On Mon, 21 Jul 2008 06:51:27 +0200, Felipe Lessa <[hidden email]>
wrote:
> On Sun, Jul 20, 2008 at 10:30 PM, Rafael Gustavo da Cunha Pereira
> Pinto <[hidden email]> 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/
--

Reply | Threaded
Open this post in threaded view
|

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

Rafael Gustavo da Cunha Pereira Pinto
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 <[hidden email]> wrote:

> On Mon, 21 Jul 2008 06:51:27 +0200, Felipe Lessa <[hidden email]>
> wrote:
>
>> On Sun, Jul 20, 2008 at 10:30 PM, Rafael Gustavo da Cunha Pereira
>> Pinto <[hidden email]> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>



--
Rafael Gustavo da Cunha Pereira Pinto
Electronic Engineer, MSc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20080721/cd21faf7/attachment.htm