Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.haskell > #358 > unrolled thread

modifying a list

Started bypip7kids@gmail.com
First post2016-02-26 08:25 -0800
Last post2016-02-26 16:58 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.haskell


Contents

  modifying a list pip7kids@gmail.com - 2016-02-26 08:25 -0800
    Re: modifying a list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-26 16:58 +0000

#358 — modifying a list

Frompip7kids@gmail.com
Date2016-02-26 08:25 -0800
Subjectmodifying a list
Message-ID<6b2b6571-7768-4c59-9e12-94afc82ac110@googlegroups.com>
Hi

I want to be able to modify my imported data on variable "y". 

This works......
import System.IO

-- data.txt = [1,2,3,4,5,6,7,4,3,2,3,4,5,6]

main = do x <- readFile "data.txt"
	  y <- rList x
	  print (sum y)

rList :: String -> IO [Int]	  
rList = readIO

..............................................
This does not work ............
import System.IO

-- data.txt = [1,2,3,4,5,6,7,4,3,2,3,4,5,6]

main = do x <- readFile "data.txt"
	  y <- rList x
	  print (sum y)
	  print ([x * 2.6 | x <- y]) -- this is what I am trying to achieve

rList :: String -> IO [Int]	  
rList = readIO

Regards

[toc] | [next] | [standalone]


#359

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-02-26 16:58 +0000
Message-ID<87twkvo1cv.fsf@bsb.me.uk>
In reply to#358
pip7kids@gmail.com writes:

> I want to be able to modify my imported data on variable "y".

You are not really modifying it (quite rightly -- that's not how Haskell
works) but the problem is a simple arithmetic one.  Unlike most other
languages, Haskell does not have automatic type promotions when doing
arithmetic so you need to say how the multiplication of an Int by 2.6
should be done.  Presumably you want floating point multiplication so
either change

  rList :: String -> IO [Int]

to

  rList :: String -> IO [Float]

or use the utility function fromIntegral in the list comprehension:

  print ([fromIntegral x * 2.6 | x <- y])

<snip>
> This does not work ............
> import System.IO
>
> -- data.txt = [1,2,3,4,5,6,7,4,3,2,3,4,5,6]
>
> main = do x <- readFile "data.txt"
> 	  y <- rList x
> 	  print (sum y)
> 	  print ([x * 2.6 | x <- y]) -- this is what I am trying to achieve
>
> rList :: String -> IO [Int]	  
> rList = readIO

-- 
Ben.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.haskell


csiph-web