Haskell如何从文件中读取S表达式? [英] How can Haskell read S-expressions from files?
问题描述
文件中的S表达式是:
ArithBiOp Plus(AInt 5)(AInt 5)
,那么在haskell程序中,我可以匹配这个行为 ArithBiop操作码oprand1操作数2
。可能可以创建一个完全适合使用 read
的数据类型来从文件中获取数据。例如:
数据Exp = ArithBiOp Op Exp Exp | AInt整数派生(显示,读取)
数据Op = Plus | Minus派生(显示,读取)
然后您可以使用 readFile
获取一个字符串并将其转换为
read
。
There are some source files, which are in S-expressions. Can Haskell read those s-expressions? I need those s-expressions as patterns matched to execute the Haskell programs (suppose those s-expressions are already in the format of my haskell program data types) . For example,
The S-expression in the file is: ArithBiOp Plus (AInt 5) (AInt 5)
, then in the haskell program, I can match this line as ArithBiop opcode oprand1 operand2
.
If you are lucky you might be able to craft a data type that has exactly the right form to use read
to get you data from file. For you example:
data Exp = ArithBiOp Op Exp Exp | AInt Integer deriving (Show, Read)
data Op = Plus | Minus deriving (Show, Read)
Then you can just use readFile
to get a string and convert that with read
.
这篇关于Haskell如何从文件中读取S表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!