Haskell如何从文件中读取S表达式? [英] How can Haskell read S-expressions from files?

查看:124
本文介绍了Haskell如何从文件中读取S表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些源文件在S表达式中。 Haskell可以读取这些s表达式吗?我需要这些s表达式作为匹配的模式来执行Haskell程序(假设这些s表达式已经是我的haskell程序数据类型的格式)。例如,
文件中的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆