POV-Ray : Newsgroups : povray.off-topic : Monads in C# : Re: Monads in C# Server Time
29 Jul 2024 02:19:50 EDT (-0400)
  Re: Monads in C#  
From: clipka
Date: 15 Mar 2013 02:25:48
Message: <5142beec$1@news.povray.org>
Am 14.03.2013 23:39, schrieb Orchid Win7 v1:
> On 14/03/2013 08:24 PM, Warp wrote:
>> Orchid Win7 v1<voi### [at] devnull>  wrote:
>>> Again, the problem is that we need to write a method which examines TWO
>>> objects which must be of THE SAME class.
>>
>> I don't understand at all what that even means.
>>
>> Start by explaining (briefly) what "examines" means in this context.
>
> Consider another example: Let's say you want to do something like
>
>    public abstract class Tree
>    {
>      ...
>      public abstract Tree Union(Tree other);
>      ...
>    }
>
> So we can now write SplayTree, HeapTree, SearchTree, etc as subclasses
> of Tree. But the thing is... you can only Union() two trees together if
> they are THE SAME TYPE of tree. So you can take the union of two
> SplayTrees, or the union of two HeapTrees, but NOT the union of a
> SplayTree and a HeapTree.

Well, this can easily be solved by making Union non-abstract and having 
it call more primitive abstract functions; e.g (using pseudocode as I 
currently don't have C# syntax memorized):

   public abstract class Tree
   {

     public abstract Tree copy();
     public abstract Iterator<Leaf> leaves();
     public abstract void addLeaf(Leaf l);

     public final Tree Union(Tree other)
     {
       Tree newTree = this.copy();
       foreach(Leaf i in other.leaves())
         newTree.addLeaf(i);
     }
   }

There. Problem solved.

Note that in OO languages it is customary to use non-lazy evaluation 
(because objects may change, so lazy evaluation is prone for effects 
that OO programmers don't expect), so there's nothing conceptually wrong 
with copying the leaves rather than creating some "meta-tree" that 
provides tree-like operations on a compound of two tree objects.


Post a reply to this message

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