Haskell IO(String)和String [英] Haskell IO (String) and String

查看:128
本文介绍了Haskell IO(String)和String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要函数:

阅读':: FilePath - >字符串

我使用:

 :t readFile 
readFile :: FilePath - > IO字符串

我做:

<$ p $读取':: IO()
读取'= do $ b $ str< - readFile/home/shk/workspace/src/test.txt
putStrLn str

我想问str是不是字符串?



我们知道:

 :t putStrLn 
putStrLn :: String - > IO()

然后为什么我不能:

  read':: String 
read'= do $ b $ str< - readFile/home/shk/workspace/lxmpp/src/test.txt
str

我得到的错误是:

 无法与实际类型为'IO String'的预期类型'[t0]'匹配
在`readFile'调用的返回类型中
在'do'表达式中:
str< - readFile/home/shk/workspace/lxmpp/src/test.txt
在表达式中:
do { str< - readFile/home/shk/workspace/src/test.txt;
str}

谢谢。

解决方案

我认为没有人回答过这个非常重要的问题,但是:


I想问str是不是字符串?


我会尽力的。



变量 str 的类型是 String ,是的。
但是,这个变量的范围非常有限。我认为解除注释是理解的必要条件:

  read'= readFile/ home / shk / workspace / src / test.txt>> =(\str  - > putStrLn str)

这里变得更加清楚为什么 str 不够好。它是您传递给>> = 的函数的参数。只有当有人调用你的函数时才会有它的值,只有在包含它的 IO 操作被执行时才会发生。



此外, read':: IO()的类型不是由 putStrLn str 确定的,而是由操作符>> = 的返回类型。看看它(专用于 IO monad):

  (>> =):: IO a  - > (a→IO b)→> IO b 

您可以看到结果始终是 IO b action,所以试图改变任何参数都无济于事。



如果你想了解为什么类型是它的方式。它背后的直觉是:你不能在没有执行动作的情况下执行某个动作。



在问题的实际方面,要使用由某个动作返回的值,而不是试图做 use(extractValue inputAction),这是没有意义的,因为 extractValue 是不可能的,试试 inputAction>> =使用如果您的使用确实涉及I / O或 fmap如果没有,使用inputAction


I want to write functions and put result to string.

I want function:

read' :: FilePath -> String

I use:

:t readFile
readFile :: FilePath -> IO String

I make:

read' :: IO ()
read' = do
     str <- readFile "/home/shk/workspace/src/test.txt" 
     putStrLn str

I want to ask str is string or not?

We know that:

:t putStrLn
putStrLn :: String -> IO ()

Then why i can't:

read' :: String
read' = do
     str <- readFile "/home/shk/workspace/lxmpp/src/test.txt" 
     str

I get error that:

 Couldn't match expected type `[t0]' with actual type `IO String'
    In the return type of a call of `readFile'
    In a stmt of a 'do' expression:
        str <- readFile "/home/shk/workspace/lxmpp/src/test.txt"
    In the expression:
      do { str <- readFile "/home/shk/workspace/src/test.txt";
           str }

Thank you.

解决方案

I think no one has answered this, very important question, yet:

I want to ask str is string or not?

I will try to.

The type of the variable str is String, yes. However, the scope of this variable is very limited. I think desugaring the do-notation is necessary for understanding:

read' = readFile "/home/shk/workspace/src/test.txt" >>= (\str -> putStrLn str)

I think here it becomes more clear why str is not good enough. It is an argument of the function you pass to >>=. Its value only becomes available when someone calls your function, which happens only when the IO action containing it is being executed.

Also, the type of read' :: IO () is determined not so much by the putStrLn str, but rather by the return type of the operator >>=. Have a look at it (specialized to the IO monad):

(>>=) :: IO a -> (a -> IO b) -> IO b

You can see that the result is always an IO b action, so trying to change any of arguments won't help.

You can read some monad tutorial if you want to understand why the type is the way it is. The intuition behind it is: you can't perform an action without performing an action.

And on the practical side of the question, to use the value returned by some action, instead of trying to do use (extractValue inputAction), which does not make sense because extractValue is not possible, try inputAction >>= use if your use does involve I/O, or fmap use inputAction if it does not.

这篇关于Haskell IO(String)和String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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