Haskell 文件读取和数字相加
我有一个程序,它采用带有值的文本文件,例如:
20 30
23 5
200 3
我将其转换为列表并添加每一行以创建小计,然后创建总和。
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
我将如何更改此代码以接受不同数字的文本文件,例如:
10 9 29 40
1 34 2
1 2 55 89
创建每行的小计列表,然后创建总计。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样
How about