Print empty list in Haksell

前端 未结 1 1284
感情败类
感情败类 2021-01-16 21:08

Here is the piece of code:

import System.Environment 
myReverse :: [a] -> [a]
myReverse [] = []
main = print (myReverse [])

When I compi

相关标签:
1条回答
  • 2021-01-16 21:37

    From myReverse [] (or [] in general), it is not possible to for the type inferencer to infer to list element type because it's an empty list. If you explicitly call e.g. myReverse ([] :: [Int]), it'll be able to find a Show instance for the list so that it can convert it to string before printing.

    This is because the Show instance for lists is defined with Show a => Show [a] meaning that [a] only has a Show instance for it if a has a Show instance for it. But there is no a to start with in the case of [].

    0 讨论(0)
提交回复
热议问题