I have a program that takes a text file with values for example:
20 30
23 5
200 3
And I convert it to a list and add each line to create a subtotal and then a sum.
import System.IO
import Control.Monad
f :: [String] -> [Int]
f = map read
subsum :: [Int] -> [Int]
subsum [] = []
subsum [x] = []
subsum (x:(y:xs)) = (x+y) : (subsum xs)
calc fromf = do
let list = []
let list2 = []
handle <- openFile fromf ReadMode
contents <- hGetContents handle
let singlewords = words contents
list = f singlewords
list2 = subsum list
result = sum list2
print list2
print result
hClose handle
How would I change this code to take in a text file of different numbers ex:
10 9 29 40
1 34 2
1 2 55 89
Create a list of subtotals of each line and then a total.
list = []andlist2 = []statements are unnecessary. These are not mutable variables (as is the case with other languages), so you're not accomplishing anything like initialization, as you may imagine.