我如何解析Haskell中的IO字符串? [英] How can I parse the IO String in Haskell?

查看:109
本文介绍了我如何解析Haskell中的IO字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到了Haskell的问题。我有这样的文本文件:

  5。 
7.
[(1,2,3),(4,5,6),(7,8,9),(10,11,12)]。

我不知道如何获得前两个数字(上面的2和7)和最后一行的列表。每行结尾都有点。



我试图构建一个解析器,但名为'readFile'的函数返回Monad,称为IO String。我不知道如何从这种类型的字符串中获取信息。



我更喜欢处理一组字符。也许有一个函数可以从'IO String'转换为[Char]?

解决方案

我认为你有一个基本的误解关于Haskell中的IO。特别是,你说这个:


也许有一个函数可以从'IO String'转换为[Char]?


不,不存在 1 ,事实上没有这样的函数是最重要的事情之一关于Haskell。



Haskell是一种非常有原则的语言。它试图保持纯功能(它没有任何副作用,并且在给予相同输入时总是返回相同的结果)和不纯功能(其具有从文件读取,打印到屏幕,写入磁盘等)。规则如下:


  1. 您可以在任何地方使用纯函数(在其他纯函数中或在不纯函数中)
  2. 您只能在其他不纯的函数中使用不纯的函数。

代码被标记为纯或不纯的方式是使用类型系统。当你看到一个函数签名像

  digitToInt :: String  - > Int 

你知道这个函数是纯粹的。如果你给它一个 String 它将返回一个 Int ,而且它总是返回相同的 Int ,如果你给它同样的 String 。另一方面,函数签名像

  getLine :: IO String 

不纯的,因为 String 的返回类型标记为 IO 。显然 getLine (它读取一行用户输入)并不总是返回相同的 String ,因为它取决于什么用户输入。不能在纯代码中使用此函数,因为即使添加最小的杂质也会污染纯代码。一旦你去 IO ,你永远不会回去。



你可以想象 IO 作为包装。当你看到一个特定的类型时,例如 x :: IO String ,你应该把它解释为 x 是在执行时执行一些任意I / O,然后返回类型为 String (注意在Haskell中, String [Char] 是完全一样的东西)。

访问来自 IO 动作的值?幸运的是,函数 main 的类型是 IO()(这是一个执行一些I / O并返回(),这与不返回任何内容相同)。因此,您可以在 main 中始终使用 IO 函数。当你执行一个Haskell程序时,你正在做的是运行 main 函数,这会导致程序定义中的所有I / O被真正执行 - 例如,你可以读取和写入文件,请求用户输入,写入标准输出等。

您可以考虑像这样构建一个Haskell程序:




  • 执行I / O的所有代码都会获得 IO 标记(基本上,您将它放在 do block)
  • 不需要执行I / O的代码不需要位于 do block - 这些是纯函数。
  • 您的 main 函数将I / O操作,您已按顺序定义了这些操作,使程序可以按照您的要求进行操作(穿插纯函数,无论您喜欢什么)。
  • 当您运行 main ,您可以执行所有这些I / O操作。





因此,考虑到所有这些,如何你写你的程序?那么,函数

  readFile :: FilePath  - > IO字符串

将文件读取为字符串 。所以我们可以使用它来获取文件的内容。函数

  lines :: String  - > [String] 

在换行符上拆分 String ,所以现在你有一个 String s的列表,每个对应一行文件。函数

  init :: [a]  - > [a] 

从列表中删除最后一个元素(这将消除最后的在每行上)。函数

  read ::(Read a)=>字符串 - > a 

取一个字符串并将它变成一个任意的Haskell数据类型,比如 Int Bool



请注意,您实际需要执行任何I / O操作的唯一时间是您正在读取文件的时间。因此,这是程序中唯一需要使用 IO 标签的部分。该程序的其余部分可以写成纯粹。



这听起来像你需要的是文章 IO Monad对于不在乎的人,这应该解释你的很多问题。不要被monad这个词所吓倒 - 你不需要明白monad编写Haskell程序是什么(注意,这段文字是我答案中唯一使用单词monad的,尽管我承认我现在已经使用了四次...)






这是我想要编写的程序

(Int,Int,Int)])
run = do
contents< - readFiletext.txt - 在此处使用'< - ',以便'contents'是一个String
let [a,b,c] =行内容 - 在换行符上分割
let firstLine = read(ini​​t a) - 'init'删除结尾期间
let secondLine = read(ini​​t b)
let thirdLine = read(ini​​t c) - 读取一个列表的元组
return(firstLine,secondLine,thirdLine)

回答 npfedwards 有关在 readFile text.txt 的输出中应用的注释,你需要 要知道 readFile text.txt 会给你一个 IO字符串,并且只有当你将它绑定到一个变量(使用内容< - ),您可以访问底层的 String ,以便您可以应用<$ c $



记住:一旦你去 IO ,你永远不会返回。




1 我故意忽略 unsafePerformIO ,因为正如名称所暗示的那样,它非常不安全!除非你真的知道你在做什么,否则千万不要使用它。


I' ve got a problem with Haskell. I have text file looking like this:

5.
7. 
[(1,2,3),(4,5,6),(7,8,9),(10,11,12)].

I haven't any idea how can I get the first 2 numbers (2 and 7 above) and the list from the last line. There are dots on the end of each line.

I tried to build a parser, but function called 'readFile' return the Monad called IO String. I don't know how can I get information from that type of string.

I prefer work on a array of chars. Maybe there is a function which can convert from 'IO String' to [Char]?

解决方案

I think you have a fundamental misunderstanding about IO in Haskell. Particularly, you say this:

Maybe there is a function which can convert from 'IO String' to [Char]?

No, there isn't1, and the fact that there is no such function is one of the most important things about Haskell.

Haskell is a very principled language. It tries to maintain a distinction between "pure" functions (which don't have any side-effects, and always return the same result when give the same input) and "impure" functions (which have side effects like reading from files, printing to the screen, writing to disk etc). The rules are:

  1. You can use a pure function anywhere (in other pure functions, or in impure functions)
  2. You can only use impure functions inside other impure functions.

The way that code is marked as pure or impure is using the type system. When you see a function signature like

digitToInt :: String -> Int

you know that this function is pure. If you give it a String it will return an Int and moreover it will always return the same Int if you give it the same String. On the other hand, a function signature like

getLine :: IO String

is impure, because the return type of String is marked with IO. Obviously getLine (which reads a line of user input) will not always return the same String, because it depends on what the user types in. You can't use this function in pure code, because adding even the smallest bit of impurity will pollute the pure code. Once you go IO you can never go back.

You can think of IO as a wrapper. When you see a particular type, for example, x :: IO String, you should interpret that to mean "x is an action that, when performed, does some arbitrary I/O and then returns something of type String" (note that in Haskell, String and [Char] are exactly the same thing).

So how do you ever get access to the values from an IO action? Fortunately, the type of the function main is IO () (it's an action that does some I/O and returns (), which is the same as returning nothing). So you can always use your IO functions inside main. When you execute a Haskell program, what you are doing is running the main function, which causes all the I/O in the program definition to actually be executed - for example, you can read and write from files, ask the user for input, write to stdout etc etc.

You can think of structuring a Haskell program like this:

  • All code that does I/O gets the IO tag (basically, you put it in a do block)
  • Code that doesn't need to perform I/O doesn't need to be in a do block - these are the "pure" functions.
  • Your main function sequences together the I/O actions you've defined in an order that makes the program do what you want it to do (interspersed with the pure functions wherever you like).
  • When you run main, you cause all of those I/O actions to be executed.

So, given all that, how do you write your program? Well, the function

readFile :: FilePath -> IO String

reads a file as a String. So we can use that to get the contents of the file. The function

lines:: String -> [String]

splits a String on newlines, so now you have a list of Strings, each corresponding to one line of the file. The function

init :: [a] -> [a]

Drops the last element from a list (this will get rid of the final . on each line). The function

read :: (Read a) => String -> a

takes a String and turns it into an arbitrary Haskell data type, such as Int or Bool. Combining these functions sensibly will give you your program.

Note that the only time you actually need to do any I/O is when you are reading the file. Therefore that is the only part of the program that needs to use the IO tag. The rest of the program can be written "purely".

It sounds like what you need is the article The IO Monad For People Who Simply Don't Care, which should explain a lot of your questions. Don't be scared by the term "monad" - you don't need to understand what a monad is to write Haskell programs (notice that this paragraph is the only one in my answer that uses the word "monad", although admittedly I have used it four times now...)


Here's the program that (I think) you want to write

run :: IO (Int, Int, [(Int,Int,Int)])
run = do
  contents <- readFile "text.txt"   -- use '<-' here so that 'contents' is a String
  let [a,b,c] = lines contents      -- split on newlines
  let firstLine  = read (init a)    -- 'init' drops the trailing period
  let secondLine = read (init b)    
  let thirdLine  = read (init c)    -- this reads a list of Int-tuples
  return (firstLine, secondLine, thirdLine)

To answer npfedwards comment about applying lines to the output of readFile text.txt, you need to realize that readFile text.txt gives you an IO String, and it's only when you bind it to a variable (using contents <-) that you get access to the underlying String, so that you can apply lines to it.

Remember: once you go IO, you never go back.


1 I am deliberately ignoring unsafePerformIO because, as implied by the name, it is very unsafe! Don't ever use it unless you really know what you are doing.

这篇关于我如何解析Haskell中的IO字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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