[Haskell-cafe] Re: [off-topic] OOPer?

Larry Evans cppljevans at suddenlink.net
Tue Nov 18 13:31:59 EST 2008

On 11/17/08 18:24, Daniel Yokomizo wrote:
 > On Mon, Nov 17, 2008 at 9:49 PM, Maurí­cio <briqueabraque at yahoo.com
 > <mailto:briqueabraque at yahoo.com>> wrote:
 >      >  (...)  I don't recall where I found the following example, but
 >     copied
 >      > it locally as compelling evidence  that the functional solution
 >     can be
 >      > much clearer and  shorter than the same solution  modeled with
 >     objects
 >      > and inheritance.


 >      > -- Arithmetic expression forms data Expr = Num Int | Add Expr Expr
 >      >
 >      > -- Evaluate expressions
 >      > eval :: Expr -> Int
 >      > (...)
 >      > public abstract class Expr {
 >      >    public abstract int eval ();
 >      >    public abstract void modn(int v);


 >     when the first standard was set, and  I can tell you for sure 
that, even
 >     at that  time, no one who  knows at least  the basics of C++ 
would ever
 >     write that problem like this.

Mauri, I'm not sure what you mean. Do you mean:

   1) No C++er would ever "structure" the problem like:

        -- Arithmetic expression forms
        data Expr = Num Int | Add Expr Expr

        -- Evaluate expressions
        eval :: Expr -> Int
        eval (Num i) = i
        eval (Add l r ) = eval l + eval r

      If so, then I'm unsure what you could mean since
      the closest counterpart to:

        date Expr = Num Int | Add Expr Expr

      in c++ is an abstract Expr class with derived
      classes, Int and Add, just as shown in Greg's Java counterpart to
      the haskell Expr.

   2) No C++er would every solve the problem with a heirarchy of Expr
      classes with virtual functions.

      If so, then I'm really confused because that's exactly the way I
      would do it *except* if I wanted to avoid the overhead of
      virtual function dispatch.  In this case, I would use template
      metaprogramming (WARNING: not for c++ template metaprogramming


      In the proto metaprogramming, AFAICT, the 1st element of the
      proto::expr template, the tag, corresponds to Expr constructor's,
      Num and Add, of Greg's haskell example code.  The | separating
      the Expr constructor variants corresponds to the proto::or_

      So, if template metaprogramming were used, then there are some
      very good c++ programmer which would structure their c++ code
      like the haskell code (although, as seen by the
      reference, it's "a bit obscured" by all the "scaffolding" needed
      to make it work).

      Another reference which *may* reflect the haskell structure is:


      I must admit I don't really understand it, but it seems to have
      some similarities to haskell's structure.  The author even uses
      haskell code to compare with his corresponding extension to
      c#. In particular, page 9 contains an example use of his proposed
      switch statement extension which looks very similar to the way
      haskell would pattern match on an expression to dispatch to the
      appropriate case.


More information about the Haskell-Cafe mailing list