FW: [Haskell-cafe] Is there anyone out there who can translate C# generics into Haskell?

Peter Verswyvelen bf3 at telenet.be
Wed Jan 2 15:35:15 EST 2008

I'm also new to Haskell, but I'm a C++/C# veteran, so I'll give it a shot. 

C#'s interfaces look a bit like Haskell's type classes. 

Although not exactly the same, you could try something like this:

-- C#: interface IX1 { String foo1(int); }
class IX1 obj where 
  foo1 :: Int -> obj -> String

-- C#: interface IX2<A> { String foo2(A); }
class IX2 obj a where 
  foo2 :: a -> obj -> String

--C#: interface IX3<A> where A : IY { String foo3(A); }
class IY a where {- ... -}
class IY a => IX3 obj a where 
  foo3 :: a -> obj -> String

--C#: interface IX4<A> : IZ where A : IY
class IZ a where {- ... -}
class (IY a, IZ obj) => IX4 obj a where
  foo4 :: a -> obj -> String

The C# "class" is called an "instance" in Haskell, so to implement an
interface, you would do e.g.

data X2 = X2 { x2Bla :: String, ... }
instance IX2 X2 String where
   foo2 s o = (x2Bla o) ++ s
This assumes your "objects" are immutable, otherwise you would have to
return (obj,String) instead of just String and then you most likely want to
use the state monad and "do notation" to make functional programming look
more like imperative programming.

You really have to drop the OO way of thinking, which I find the hardest :)
Haskell's type classes are more powerful in some sense than C# interfaces;
for example, in C# you can't attach an interface to any class (take for
example the Point struct), it has to be your own class, while in Haskell,
you can implement an instance of a type class for any datatype!

Hope this helps a bit. As I'm new myself to Haskell, so take this with a
"grain of salt".

Once you bite the bullet, I found Haskell a lot more fun than C#, although
C# of course comes with a gigantic .NET framework and tools...


-----Original Message-----
From: haskell-cafe-bounces at haskell.org
[mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Nicholls, Mark
Sent: Wednesday, January 02, 2008 5:41 PM
To: haskell-cafe at haskell.org
Subject: [Haskell-cafe] Is there anyone out there who can translate C#
generics into Haskell?

I'm trying to translate some standard C# constucts into Haskell... some
of this seems easy....



Interface IX


Interface IX<A>


Interface IX<A>
	Where A : IY


Interface IX<A> : IZ
	Where A : IY

I can take a punt at the first 2....but then it all falls apart
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org

More information about the Haskell-Cafe mailing list