Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 有没有办法在do/while/let块中打印出一种类型的变量?_Haskell_Ghci - Fatal编程技术网

Haskell 有没有办法在do/while/let块中打印出一种类型的变量?

Haskell 有没有办法在do/while/let块中打印出一种类型的变量?,haskell,ghci,Haskell,Ghci,有没有办法打印出ghci中嵌套变量的推断类型?考虑代码, let f = g where g (x :: Int) = x 然后,最好查询g的类型,例如:tf.g将打印出Int->Int您可以通过给出适当错误的类型注释并检查错误消息来哄出此信息 *Main> let f = g where g::a; g (x::Int) = x <interactive>:1:23: Couldn't match type `a1' with `Int -> Int

有没有办法打印出
ghci
中嵌套变量的推断类型?考虑代码,

let f = g where
    g (x :: Int) = x

然后,最好查询
g
的类型,例如
:tf.g
将打印出
Int->Int

您可以通过给出适当错误的类型注释并检查错误消息来哄出此信息

*Main> let f = g where g::a; g (x::Int) = x

<interactive>:1:23:
    Couldn't match type `a1' with `Int -> Int'
      `a1' is a rigid type variable bound by...
*Main>设f=g,其中g::a;g(x::Int)=x
:1:23:
无法将类型'a1'与'Int->Int'匹配
`a1'是一个刚性类型变量,由。。。

ghci调试器可以使用正确放置的断点为您打印它(但您需要在模块中加载定义):

然后在ghci中:

Prelude> :l tmp2.hs
[1 of 1] Compiling Main             ( tmp2.hs, interpreted )
Ok, modules loaded: Main.
*Main> :b 3 9
Breakpoint 0 activated at tmp2.hs:3:7-9
*Main> f undefined
Stopped at tmp2.hs:3:7-9
_result :: Int = _
a :: Int = _
g :: Int -> Int = _
[tmp2.hs:3:7-9] *Main>

难道
g
f
的类型不一样吗<代码>:tf。顺便问一下,您是如何让ghci接受多行语句的?这对我来说从来都不起作用。@snail用
{:
:}
在你的多行语句中加上它们自己的行。@pat Awesome,谢谢你的把戏!我实际上是在把它保存到一个文件中。你有一个打字错误,它是
:{
.oops,我甚至先去ghci检查一下……这绝对是更方便的解决方案。当你只有“a”,它似乎工作得很好。但是,我确实有一个特定的情况,在do块中有一个变量,使用范围变量,类型注释似乎没有显示任何内容。(奇怪,也许我应该提交一个bug).我发现这比另一个答案更容易,因为有时强制键入会导致不同的错误,但这样你总能找到类型。
Prelude> :l tmp2.hs
[1 of 1] Compiling Main             ( tmp2.hs, interpreted )
Ok, modules loaded: Main.
*Main> :b 3 9
Breakpoint 0 activated at tmp2.hs:3:7-9
*Main> f undefined
Stopped at tmp2.hs:3:7-9
_result :: Int = _
a :: Int = _
g :: Int -> Int = _
[tmp2.hs:3:7-9] *Main>