3 ms·
Definitely has some insightful commentary, but I feel like he hasn't gotten into the heart of the subject matter. Haskell has something like NULL, it's called
by jganetsk 18y ago
Definitely has some insightful commentary, but I feel like he hasn't gotten into the heart of the subject matter.
Haskell has something like NULL, it's called "undefined". At first, it feels like it's an ugly hack, something provided by the language designers as an afterthought. But it's theoretically sound, and pretty easy to construct manually. Just open up your Haskell REPL and type...
let x = x
This is an infinite loop. Another way to look at it is "let x = (let f x = f x in f ())". You will see x :: t, a single type variable, the most general polymorphic type. (This is also known as bottom type). In other words, it can be used anywhere, just like "NULL".
Shouldn't there be no values of bottom type in a statically typed language? How could this happen? Laziness. Try to force x, and you will find out that it never terminates. Because of laziness, any argument to any function could be a suspended infinite loop, or a suspended thrown exception. You just don't find out until you need the actual value bound to that name.
So, how is Haskell's undefined different from the NULL of so many languages? In one very key way... with NULL, one can check for NULLness. You can say "if (x == NULL)". With undefined, there's no way to express that. Any kind of expression that forces the value of undefined should itself be undefined.