Why does this Haskell code produce the “infinite type” error?

后端 未结 4 1545
青春惊慌失措
青春惊慌失措 2020-12-13 03:27

I am new to Haskell and facing a \"cannot construct infinite type\" error that I cannot make sense of.

In fact, beyond that, I have not been able to find a good exp

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 03:53

    Often adding an explicit type definition can make the compiler's type error message make more sense. But in this case, the explicit typing makes the compiler's error message worse.

    Look what happens when I let ghc guess the type of intersperse:

    Occurs check: cannot construct the infinite type: a = [a]
      Expected type: [a] -> [[a]] -> [[a]]
      Inferred type: [a] -> [[a]] -> [a]
    In the second argument of `(:)', namely `intersperse s xs'
    In the second argument of `(:)', namely `y : intersperse s xs'
    

    That clearly points toward the bug in the code. Using this technique you don't have to stare at everything and think hard about the types, as others have suggested doing.

提交回复
热议问题