在GHCi中工作>但没有加载时? [英] Works in GHCi> but not when loaded?
问题描述
我无法弄清楚为什么我会得到两个不同的结果,但我确信它与 IO
有关,我开始讨厌它!
例如:
ghci> x< - readFilefoo.txt
ghci>让y =读取x :: [Int]
ghci> :ty
y :: [Int]
现在,当我创建该文件并执行相同操作时它出来作为 IO [Int]
?
foo.txt
是一个txt文件,只包含这个: 12345
有人可以向我解释这个吗?
感谢您的宝贵意见! 解决方案
阅读有关 ghci 。引用
在GHCi提示符处接受的语句的语法与Haskell do表达式中语句的语法完全相同。但是,这里没有monad重载:在提示符处键入的语句必须位于IO monad中。
基本上当你用ghci写任何东西时,你就在 IO
Monad中。
I cant figure out why I get two different results but I'm sure it has to do with IO
, which I am beginning to hate!
For example:
ghci> x <- readFile "foo.txt"
ghci> let y = read x :: [Int]
ghci> :t y
y :: [Int]
Now when I create that file and do the same thing it comes out as IO [Int]
?
foo.txt
is a txt file containing only this: 12345
Someone that can explain this to me? As I'm about to snap it!
Thanks for any insight!
Read about ghci. To quote
The syntax of a statement accepted at the GHCi prompt is exactly the same as the syntax of a statement in a Haskell do expression. However, there's no monad overloading here: statements typed at the prompt must be in the IO monad.
Basically you are inside the IO
Monad when you are writing anything in ghci.
这篇关于在GHCi中工作>但没有加载时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!