how does 'undefined' work in Haskell

后端 未结 6 1545
广开言路
广开言路 2021-02-05 04:23

I\'m curious about the \'undefined\' value in Haskell. Its interesting because you can put it just about anywhere, and Haskell will be happy. The following are all a-ok

6条回答
  •  甜味超标
    2021-02-05 04:50

    Well, basically undefined = undefined — if you try evaluate it, you get an endless loop. But Haskell is a non-strict language, so head [1.0, 2.0, undefined] doesn't evaluate all elements of the list, so it prints 1.0 and terminates. But if you print show [1.0, 2.0, undefined], you'll see [1.0,2.0,*** Exception: Prelude.undefined.

    As of how it can be of all types... Well, if expression is of type A, it means that evaluating it either will yield value of type A, or the evaluation will diverge, yielding no value at all. Now, undefined always diverge, so it fits into definition for every imaginable type A.

    Also, a good blog post on the related topics: http://james-iry.blogspot.ru/2009/08/getting-to-bottom-of-nothing-at-all.html

提交回复
热议问题