|
![](/i/fill.gif) |
On 28/10/2010 08:36 PM, Warp wrote:
> Orchid XP v8<voi### [at] dev null> wrote:
>> Besides, normally just about any code that does stuff to a linked list
>> ends up doing something like
>
>> while (list != null)
>> do stuff;
>> list = next node;
>
>> So there's a check in there anyway. All we're doing by using a union is
>> making it a different check.
>
> Ostensibly there could be other types of nodes in the list besides regular
> nodes and end nodes. These could, for example, contain different types of
> data. Any operation you do on the data would have to first check that the
> node is of the correct type (if the node is a union).
Sure. But the compiler statically knows what all the possibilities are,
and it can decide on some suitable arrangement for doing this efficiently.
Instead of writing the above, in Haskell you end up doing something like
do_stuff list =
case list of
Node data next -> ... [recursively call do_stuff next] ...
End -> ...[do something else]...
If there are more possibilities than just "Node" or "End", you just list
them as extra possibilities. Either way, the compiler is going to build
a multi-way jump. (I believe GHC handles this using indirect code
pointers rather than actual integer comparisons against an ID tag.)
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
![](/i/fill.gif) |