3

Does Haskell support the concept of unbound variables as in the Oz programming language?

Derek Mahar
  • 27,608
  • 43
  • 124
  • 174

1 Answers1

4

Haskell only supports variables in terms of values in (monadic) contexts. Once you look at those, there are certain ones like MVar which can indeed be empty.

If you want to represent simple nullability of a value, though, Maybe a is a perfect way to do that, separate from the actual value being a reference to something mutable or just an immutable something.


To expand and illustrate:

newIORef :: a -> IO (IORef a)

But we can easily write newEmptyIORef as such:

newEmptyIORef :: IO (IORef (Maybe a))
newEmptyIORef = newIORef Nothing

Take note that Maybe (IORef a) ≠ IORef (Maybe a).

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • Can a Haskell program bind an `MVar` to a value (assign a value to the variable) after it the program declares the variable? I call this *lazy binding* or *lazy assignment* (or *deferred* binding or assignment). – Derek Mahar Oct 30 '15 at 17:33
  • 1
    @DerekMahar This is kind of the point of MVars, but i am not sure if that was a good example – Bartek Banachewicz Oct 30 '15 at 17:35
  • 1
    @DerekMahar No. There is no way to initialize/declare a variable without defining it. – Rein Henrichs Oct 30 '15 at 18:05
  • The code example given here does not actually use `MVar`s (although if you're only using one thread I guess it's equivalent) – Jeremy List Oct 31 '15 at 01:32