[Haskell-cafe] I saw this... and thought of you

Tillmann Rendel rendel at rbg.informatik.tu-darmstadt.de
Wed Jun 6 09:16:20 EDT 2007


Andrew Coppin wrote:
> http://dis.4chan.org/read/prog/1180896798/
> 
> (It's been a while since I touched Java, and I must confess I can't even 
> comprehend this code...)

Look's like a bad done extension of the well-known function object 
pattern in oo design to allow currying. I would prefer the following 
much more natural implementation:

// in Haskell:
// type Fun cod dom = dom -> cod
interface Fun<Cod, Dom> {
   Cod eval(Dom arg);
}

// in Haskell:
// adder :: Fun (Fun Int Int) Int
// adder = \a -> \b -> a + b
final class Adder extends Fun<Fun<Integer, Integer>, Integer> {
   public Fun<Integer, Integer> eval(final Integer a) {
     return new Fun<Integer, Integer>() {
       public Integer eval(Integer b) {
         return a + b;
       }
     };
   }
}

// in Haskell:
// main = do let plus :: Fun (Fun Int Int) Int = adder
//           let succ :: Fun Int Int = adder 1
//           let result = succ 41
//           print result
public class Test {
   public static void main(String[] args) {
     Fun<Fun<Integer, Integer>, Integer> plus = new Adder();
     Fun<Integer, Integer> succ = plus.eval(1);
     Integer result = succ.eval(41)
     System.out.println(result);
   }
}

If you're interested in such stuff, check out this Book:

   Thomas Kühne, "A Functional Pattern System for Object-Oriented Design"
   http://www.mm.informatik.tu-darmstadt.de/%7Ekuehne/fps/

But most probably, you are neither interested nor impressed, because you 
know "the real thing".

Tillmann


More information about the Haskell-Cafe mailing list