简体   繁体   English

如何在Frege中打印一个空的文字列表

[英]How to print an empty literal list in Frege

I tried 我试过了

println [ ] 

but got 但是得到了

unknown context: Show <3298 a>

Is that not supported by design or is my code wrong? 是设计不支持的,还是我的代码错误?

The point is that the expression 关键是表达

[]

does not give any information about the list element type. 不提供有关列表元素类型的任何信息。 But this information is needed to get it printed! 但是,需要此信息才能将其打印出来!

This seems absurd at first sight, but remember that the type class system allows us to print a list of A's differently than a list of B's. 乍一看,这似乎很荒谬,但请记住,类型类系统使我们可以打印出与B列表不同的A列表。 For example, in Haskell, a list of characters is not printed like 例如,在Haskell中,字符列表不会像

['n', 'o', 't', ' ', 's', 'o']

I think that in Haskell there is some type defaulting going on (at least in GHCi?), so it can be printed anyway. 我认为在Haskell中有某种类型的默认设置(至少在GHCi中是这样),因此无论如何都可以打印出来。 You may add the "haskell" tag to this question and ask for an explanation why it works in Haskell. 您可以在此问题上添加“ haskell”标签,并要求解释为什么它在Haskell中有效。

The solution, of course, is to add the missing type information: 解决方案当然是添加缺少的类型信息:

println ([] :: [()])     -- for example

--------------- EDIT ------------------------ ---------------编辑------------------------

I checked the following code with GHC 7.6.2: 我使用GHC 7.6.2检查了以下代码:

foo n = if n == 0 then print [] else print Nothing
main = foo 42

and it does give error messages: 并且确实给出错误消息:

Could not deduce (Show a0) arising from a use of `print'
...
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
...
In the expression: print []

Could not deduce (Show a1) arising from a use of `print'
...
The type variable `a1' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
...
In the expression: print Nothing

The bottom line is that ghci allows things that are not valid in Haskell source code. 底线是,ghci中允许的事情是不是在Haskell源代码有效。 In fact, you can type: 实际上,您可以输入:

let bar n = if n == 0 then print [] else print Nothing

but if you try to load the very same code it gives you the error messages above. 但是如果您尝试加载完全相同的代码,则会收到上述错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM