Re: [Haskell] Dynamic binding

Jan-Willem Maessen wrote:
On Jun 22, 2005, at 9:38 PM, Andrew Ward wrote:
Pal-Kristian Engstad wrote:
On Wednesday 22 June 2005 05:38 pm, Andrew Ward wrote:
What would be the normal way for a Haskell programmer to handle the typical shape example in beginner OO tutorials?
By not doing OO. You have to ask yourself, what is the purpose and/or benefit of using OO? In C++, OO is _useful_ because you restrict the problems of mutable data (by enclosing it in C++ classes).
ML type languages have other methods of doing things, and guess what, OO is not that needed for these languages. Sum-types, pattern-matching and data constructors make half of the need for OO go away. Higher order functions and make it even less needed. For the rest, there's always work-arounds.
PKE.
To handle the problem of drawing all shapes, in c++, I would have a list of shape pointers:
struct shape{ virtual void draw(...);}; struct circle : public shape {...}; struct square : public shape {...}; std::list
shapes; for(std::list ::iterator it = shapes.begin();it != shapes.end();++it) { (*it)->draw(...); } This general pattern of dynamic binding I use over and over again. Could you give me some example code of this type of thing handled in Haskell's way? Assuming that the number of classes deriving from shape might get quite large.
It seems to me that if this is the specific problem being addressed, a list of higher-order functions is exactly the right solution in any ML-like language, and this is why there have been several responses to that effect.
I do think it's fair to say "consider changing the way you think" to OO programmers trying to learn Haskell. If we were on an OOP mailing list, I could ask for days how to simulate pattern matching and algebraic types---and get a nonsensical runaround involving the visitor pattern and huge swaths of unreadable code.
Ralf, I think it's incumbent on you, having said several times "that isn't solving the problem", to more clearly explain what problem you think exists and cannot be solved gracefully (I suspect it has to do with extensible down-casting---which is indeed hard in Haskell, but many reasonable people might consider irrelevant or even overtly bad).
Andrew, if this isn't the problem you're actually trying to solve, can you explain why a simple list of functions doesn't help you?
I think I will need to learn a little more Haskell before I can appreciate all the different responses. However I will try to answer your question. Assuming I have a list of shapes somewhere in my program. The list would be heterogeneous, or at least contain data constructed using various different type constructors. The list would also be changing all the time, for example in a drawing program. So, using the suggestion of a list of drawing functions, would this not mean I would have to alter the list of drawing functions each time I alter the list of shapes? It does not seem an extensible solution. When it comes to adding new types or new functionality for existing types would it not be easy to miss updating a part of the program source that needed updating? The example I have found the simplest is the one given by Lennart, however this to me seems equivalent to the way people use switch statements in C to mimic dynamic binding. As I said before, I will have to learn more haskell and investigate OOHaskell properly. Thanks for all your responses.
-Jan-Willem Maessen
Andrew Ward.
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell

Hello Andrew, Friday, June 24, 2005, 12:35:16 AM, you wrote: AW> Assuming I have a list of shapes somewhere in my program. The list would AW> be heterogeneous, or at least contain data constructed using various AW> different type constructors. The list would also be changing all the AW> time, for example in a drawing program. So, using the suggestion of a AW> list of drawing functions, would this not mean I would have to alter the AW> list of drawing functions each time I alter the list of shapes? of course, we suggest this solution only for cases when all you need from shapes - to draw them. as i write in previous letter, one time i changed a list of regular expressions to list of functions that check matches with these regular expressions because it was the only operation i need if you require several operations, you can pack them in tuple or structure (really, tuple is a structure without field names) if you need fields/interfaces inheritance, then this method will not work. but in 90% of cases it's enough. in C++, creating classes is the most useful method of structuring program, so it is used for any problem - from very simple to most complex. when you translate such thing to Haskell, it's better to see which concrete problem are solved with help of classes, and not to emulate the classes itself (because in this case you will write many unneeded code) AW> It does AW> not seem an extensible solution. When it comes to adding new types or AW> new functionality for existing types would it not be easy to miss AW> updating a part of the program source that needed updating? adding new "class" needs adding one "constructor" function. it's hard to skip that part :) adding new function to interface need to change interface structure definition or adding new element to tuple. then type-checker of Haskell compiler will force you to add new function to all places where this structure/tuple are created. you just never worked with such strong type-checking as in Haskell AW> The example I have found the simplest is the one given by Lennart, AW> however this to me seems equivalent to the way people use switch AW> statements in C to mimic dynamic binding. only because it's C-like :) you just can't believe that Haskell program can be 3-10 times smaller while keeping the same functionality :))) -- Best regards, Bulat mailto:bulatz@HotPOP.com
participants (2)
-
Andrew Ward
-
Bulat Ziganshin