List Haskell从文件中读取整数和列表列表
假设我有一个文件List Haskell从文件中读取整数和列表列表,list,haskell,List,Haskell,假设我有一个文件 8 [[1,2,3,4],[1,2]] 其中包含列表的编号和列表 此代码允许我从中读取所有行或第n个元素 import System.IO main = do x <- openFile "file.txt" ReadMode cont <- hGetContents x let fLines = lines cont let numb = fLines !! 0 !! 0 print numb 它正
8 [[1,2,3,4],[1,2]]
其中包含列表的编号和列表
此代码允许我从中读取所有行或第n个元素
import System.IO
main = do
x <- openFile "file.txt" ReadMode
cont <- hGetContents x
let fLines = lines cont
let numb = fLines !! 0 !! 0
print numb
它正在为列表列表工作
但是,当我不能使它与单Int一起工作时
into :: String -> Int
into = read
不知道为什么,但它不适用于numb
t\t
更新2
好吧,这有点愚蠢numb
在这里是Char,因此要使其工作,只需编写即可
print (into [numb])
谢谢大家的回答。您可以使用
ord
函数::Char->Int
该函数可以在Hoogle搜索引擎上搜索Char->Int
类型签名:
文件:
例如:
import System.IO
import Data.Char (ord)
main = do x <- openFile "file.txt" ReadMode
cont <- hGetContents x
let fLines = lines cont
let numb = fLines !! 0 !! 0
let numbInt = ord numb - ord '0'
print numbInt
import System.IO
导入数据字符(ord)
main=do x您可以使用ord
函数::Char->Int
该函数可以在Hoogle搜索引擎上搜索Char->Int
类型签名:
文件:
例如:
import System.IO
import Data.Char (ord)
main = do x <- openFile "file.txt" ReadMode
cont <- hGetContents x
let fLines = lines cont
let numb = fLines !! 0 !! 0
let numbInt = ord numb - ord '0'
print numbInt
import System.IO
导入数据字符(ord)
main=do x可以用来解析Haskell数据结构的String
表示。这也适用于列表列表。您可以使用它来解析Haskell数据结构的字符串表示形式。这也适用于列表列表。索引是否总是一位数?在那个例子中,寻找第一个空格可能会更好。而不是用索引到flines
代码>,最好使用空格分隔。这样就更灵活了,您不必将前导的字符
转换回字符串
。索引是否总是一位数字?在那个例子中,寻找第一个空格可能会更好。而不是用索引到flines
代码>,最好使用空格分隔。这样,它更灵活,您不必将前导的字符
转换回字符串
。