POV-Ray : Newsgroups : povray.off-topic : Programming language discussion Server Time
4 Sep 2024 01:22:27 EDT (-0400)
  Programming language discussion (Message 31 to 40 of 47)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 7 Messages >>>
From: Warp
Subject: Re: Programming language discussion
Date: 27 Oct 2010 13:00:16
Message: <4cc85aa0@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> In Haskell, "unions" are a fundamental part of the language. One of the 
> fundamental branching primitives is based on them. Nullable fields use 
> unions, lists are unions, trees are unions, just about everything ends 
> up being a union. [Insert witty Thatcher jokes here.]

  I'm not exactly sure if your concept of "union" is the same as C's
concept of "union". A union in C has a fixed size. It's not a dynamic
data container, like a list or a tree.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Programming language discussion
Date: 27 Oct 2010 14:27:33
Message: <4cc86f15$1@news.povray.org>
Warp wrote:
> It's not a dynamic data container, like a list or a tree.

Remember that in Haskell these are defined recursively. So a "tree" in this 
case is union { struct { tree*left, tree*right }, struct { node data } }

A list is
union { struct {node data, list* next} , struct {node data} }

I.e., what he's calling "tree" would be "node of a tree" in C.

-- 
Darren New, San Diego CA, USA (PST)
   Serving Suggestion:
     "Don't serve this any more. It's awful."


Post a reply to this message

From: Orchid XP v8
Subject: Re: Programming language discussion
Date: 27 Oct 2010 17:15:47
Message: <4cc89683$1@news.povray.org>
On 27/10/2010 07:27 PM, Darren New wrote:

> I.e., what he's calling "tree" would be "node of a tree" in C.

Yeah, I should have been clearer about that. A list *node* is a union. A 
tree *node* is a union.

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Warp
Subject: Re: Programming language discussion
Date: 28 Oct 2010 11:38:40
Message: <4cc998ff@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> On 27/10/2010 07:27 PM, Darren New wrote:

> > I.e., what he's calling "tree" would be "node of a tree" in C.

> Yeah, I should have been clearer about that. A list *node* is a union. A 
> tree *node* is a union.

  What would the harm be if eg. list nodes were structs instead?

-- 
                                                          - Warp


Post a reply to this message

From: Invisible
Subject: Re: Programming language discussion
Date: 28 Oct 2010 11:46:52
Message: <4cc99aec$1@news.povray.org>
>>> I.e., what he's calling "tree" would be "node of a tree" in C.
>
>> Yeah, I should have been clearer about that. A list *node* is a union. A
>> tree *node* is a union.
>
>    What would the harm be if eg. list nodes were structs instead?

Nothing. It's just that in Haskell, you can't implement it that way. 
[Correction: You can't implement *finite* lists that way.] And, with 
unions being a fundamental part of the language, you don't need to.


Post a reply to this message

From: Darren New
Subject: Re: Programming language discussion
Date: 28 Oct 2010 13:36:45
Message: <4cc9b4ad$1@news.povray.org>
Warp wrote:
>   What would the harm be if eg. list nodes were structs instead?

Since "null" is a union in Haskell to (i.e., it either has data in it or it 
has no data in it), this wouldn't really save you anything.

-- 
Darren New, San Diego CA, USA (PST)
   Serving Suggestion:
     "Don't serve this any more. It's awful."


Post a reply to this message

From: Warp
Subject: Re: Programming language discussion
Date: 28 Oct 2010 13:41:26
Message: <4cc9b5c6@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> >   What would the harm be if eg. list nodes were structs instead?

> Since "null" is a union in Haskell to (i.e., it either has data in it or it 
> has no data in it), this wouldn't really save you anything.

  It saves checking the node type for each single operation you do to any
node in the list. It the node is a struct, its contents are fixed and thus
you don't have to check anything.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Programming language discussion
Date: 28 Oct 2010 14:00:22
Message: <4cc9ba36$1@news.povray.org>
Warp wrote:
>   It saves checking the node type for each single operation you do to any
> node in the list. It the node is a struct, its contents are fixed and thus
> you don't have to check anything.

If the node is a struct, you still have to check if it's the end of the list 
or a leaf of the tree.  You still have to do the equivalent of the text in

if (node->next != NULL) ...

In any case, this checking process is built into the syntax of the language. 
That (and more) is what is meant by "pattern matching" constructs. So 
checking the tag on the union (so to speak) is done in a type-safe way with 
pretty much the same amount of work as checking for a NULL pointer.

-- 
Darren New, San Diego CA, USA (PST)
   Serving Suggestion:
     "Don't serve this any more. It's awful."


Post a reply to this message

From: Orchid XP v8
Subject: Re: Programming language discussion
Date: 28 Oct 2010 15:21:06
Message: <4cc9cd22$1@news.povray.org>
>>>    What would the harm be if eg. list nodes were structs instead?
>
>> Since "null" is a union in Haskell to (i.e., it either has data in it or it
>> has no data in it), this wouldn't really save you anything.
>
>    It saves checking the node type for each single operation you do to any
> node in the list. It the node is a struct, its contents are fixed and thus
> you don't have to check anything.

On the other hand, in a language where unions appear all over the place, 
the implementors go to great lengths to make it really efficient.

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.

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Orchid XP v8
Subject: Re: Programming language discussion
Date: 28 Oct 2010 15:26:37
Message: <4cc9ce6d$1@news.povray.org>
>> Yeah, I should have been clearer about that. A list *node* is a union. A
>> tree *node* is a union.
>
>    What would the harm be if eg. list nodes were structs instead?

Perhaps I should elaborate further:

I'm not attempting to argue that unions are such a fundamental feature 
that every programming language should have them. I'm just pointing out 
that *in Haskell* they are extremely fundamental. Which, clearly, is a 
different statement.

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 7 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.