The composite design pattern implemented using record types, <br>where the named elements are the interface to the object<br><br>Overall, I think I agree with Tim that the record types are simpler to code. <br><br>I&#39;m not sure, though, what would happen if I tried to add state to the types. With the previous example, using existentials to create a reference type that holds elements of a type class that matches the interface, I think that it would be natural to hold state by having that element stored in a mutable state variable, and replacing the held values.
<br><br>In any case:<br><br>Two methods, add and draw<br><br>&gt; data Component = Component {<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; draw :: String,<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; add ::&nbsp; Component -&gt; Component<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>A constructor for the leaf type, which holds a string
<br><br>&gt; leaf :: String -&gt; Component<br>&gt; leaf s = <br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; Component draw1 add1<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where draw1 = show s<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add1 _ = leaf s<br><br>the draw method for the composite type 
<br>(because I was having trouble with layout and formating for 72 cols)<br><br>&gt; compositeDraw :: [Component] -&gt; String<br>&gt; compositeDraw []&nbsp; = &quot;()&quot;<br>&gt; compositeDraw leaves&nbsp; = &quot;(&quot; ++ (foldr1 (++) $ map draw leaves) ++ &quot;)&quot;
<br><br><br>A constructor for the composite type, which holds a list of components<br>and dispatches to the contained elements<br><br>&gt; composite :: [Component] -&gt; Component<br>&gt; composite cs =<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; Component draw1 add1
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where draw1 = compositeDraw cs<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add1 c = composite $ c:cs <br><br><br><br><div><span class="gmail_quote">On 2/27/07, <b class="gmail_sendername">Tim Docker</b> &lt;<a href="mailto:twd_gg@dockerz.net">
twd_gg@dockerz.net</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Steve Downey wrote:<br>&gt; interesting. it leads to something that feels much more like an object
<br>based, as opposed to a class based, system.<br>&gt; as far as haskell is concerned, everything has the same type, even<br>though different instances have very different behavior.<br>&gt; ....<br>&gt; the question is, which plays nicer with the rest of haskell? that is, if
<br>i&#39;m not committing to a closed dsl, which style is more likely to be<br>reusable against other libraries.<br><br>I suspect there&#39;s no right answer - it&#39;s a case of choosing the<br>best approach for the problem. As an example, my charting library
<br>(<a href="http://dockerz.net/software/chart.html">http://dockerz.net/software/chart.html</a>) uses the record of functions<br>approach for composing drawings:<br><br>data Renderable = Renderable {<br>&nbsp;&nbsp;&nbsp;&nbsp;minsize :: (Render RectSize)
<br>&nbsp;&nbsp;&nbsp;&nbsp;render :: (Rect -&gt; Render ())<br>}<br><br>Tim<br><br><br><br><br></blockquote></div><br>