Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Haskell将字符串划分为列表_String_Haskell_Readfile - Fatal编程技术网

String Haskell将字符串划分为列表

String Haskell将字符串划分为列表,string,haskell,readfile,String,Haskell,Readfile,我已经在这个项目上工作了很长一段时间,只想问一下是否可以将一个字符串划分为一个集合列表。目前我有 type Rate = (String,Int) type Film = (String,String,Int,[Rate]) testDatabase :: [Film] testDatabase = [("Blade Runner","Ridley Scott",1982,[("Amy",5), ("Bill",8), ("Ian",7), ("Kevin",9), ("Emma",4),

我已经在这个项目上工作了很长一段时间,只想问一下是否可以将一个字符串划分为一个集合列表。目前我有

type Rate = (String,Int)
type Film  =  (String,String,Int,[Rate])

testDatabase :: [Film]
testDatabase = [("Blade Runner","Ridley Scott",1982,[("Amy",5), ("Bill",8), ("Ian",7), ("Kevin",9), ("Emma",4), ("Sam",7), ("Megan",4)]),("The Fly","David Cronenberg",1986,[("Megan",4), ("Fred",7), ("Chris",5), ("Ian",0), ("Amy",6)])...
我还定制了许多功能,以便能够使用
[Film]
类型

当我试图从文本文件导入数据时,出现了问题

使用
readFile
我只能阅读

[("Blade Runner","Ridley Scott",1982,[("Amy",5), ("Bill",8), ("Ian",7), ("Kevin",9), ("Emma",4), ("Sam",7), ("Megan",4)]),("The Fly","David Cronenberg",1986,[("Megan",4), ("Fred",7), ("Chris",5), ("Ian",0), ("Amy",6)]) 
作为

我不知道是否有办法将
readFile
读取为不同的数据类型,而不是字符串,或者将该字符串转换为适合该数据类型

任何建议都将不胜感激。对不起,我没有把问题说清楚。总之,有没有一种方法可以将字符串转换为数据,以便能够实现为类型,或者有没有一种方法可以将
读取文件
转换为
字符串
以外的内容

提前感谢。

您可以使用:

main=do

字符串谢谢您的发帖,但我以前也尝试过类似的方法,我收到了消息\\Prelude.read:no parse//。你的例子对我很有用:
读“[(“刀锋杀手”,“雷德利·斯科特””,1982年,[(“艾米”,[5],(“比尔”,[8],[Ian”,[7],[Kevin”,[9],[Emma”,[7],[Sam”,[Megan”,[4],[Megan],[1986],[David Cronenberg],[1986],[Megan”,[4],[Fred”,[7],[Chris”,[5],[0],[0],[0],[6],[1]你是一部传奇电影,我的数据一定有问题,我制作了一个单独的txt文件,并用数据进行了尝试,结果成功了,谢谢,我希望你这一周过得愉快。是打字错误还是文件末尾缺少了一个
]
?如果是这样,这就是
read
抛出错误解析的原因。确保提供给
read
的字符串是有效的Haskell代码。不抱歉,数据库中还有更多内容,我只是将其剪切以显示部分内容。但是谢谢你的关注。
"[("Blade Runner","Ridley Scott",1982,[("Amy",5), ("Bill",8), ("Ian",7), ("Kevin",9), ("Emma",4), ("Sam",7), ("Megan",4)]),("The Fly","David Cronenberg",1986,[("Megan",4), ("Fred",7), ("Chris",5), ("Ian",0), ("Amy",6)])" 
main = do
  string <- readfile "data.txt"
  let testDatabase = read string :: [Film]