C and Java can't represent monads at all, since they don't support closures.
C# and Python can, sort of, but their syntax and object-oriented semantics make it somewhat difficult. I don't know C# well, and Python needs a lot of magic[1] to get reasonable-looking code.
Here's a Python example. It's not a direct translation, but is close enough that you should be able to figure out the original. I've included the Haskell code in comments.
import abc
# class Monad m where
# build :: a -> m a
# bind :: m a -> (a -> m b) -> m b
class Monad (metaclass = abc.ABCMeta):
@classmethod
def build (cls, x):
raise NotImplementedError
def bind (self, f):
raise NotImplementedError
# data Maybe a = Just a | Nothing
# deriving (Show)
class Maybe (Monad):
def __init__ (self, x):
self.__x = x
def __repr__ (self):
if self is Nothing:
return "Nothing"
return ("Just %r" % self.__x)
# instance Monad Maybe where
# build x = Just x
#
# bind Nothing _ = Nothing
# bind (Just x) f = f x
@classmethod
def build (cls, x):
return Just (x)
def bind (self, f):
if self is Nothing:
return Nothing
return f (self.__x)
def Just (x):
return Maybe (x)
Nothing = Maybe (None)
# clamp limit x = if x > limit then Nothing else Just x
def clamp (limit, x):
if x > limit:
return Nothing
return Just (x)
# multiclamp2 lim1 lim2 x = bind
# (clamp lim1 x)
# (\x' -> clamp lim2 x')
def multiclamp2 (lim1, lim2, x):
return clamp (lim1, x).bind (lambda x2: clamp (lim2, x2))
[1]
http://www.valuedlessons.com/2008/01/monads-in-python-with-n...