3 ms·
> This is yet another "monad" that can't do the list monad, > and is not therefore actually a monad. The function passed > to bind may be called zero times
by jmillikin 16y ago
> This is yet another "monad" that can't do the list monad,
> and is not therefore actually a monad. The function passed
> to bind may be called zero times, once, or n times. (Or
> perhaps rather, any number of times, but I think phrasing
> it this way underscores the place where many putative
> monads fall down.)
The author's implementation support lists just fine -- Monad is just a typeclass, which can be emulated using Python's inheritance.
Unfortunately dynamic typing makes the code much uglier than in Haskell, but that's to be expected.
class List(Monad):
def __init__(self, items):
self.__items = list(items)
def __str__(self):
return str(self.__items)
def bind(self, func):
items = []
for ii in map(func, self.__items):
items.extend(ii.__items)
return List(items)
print List([1, 2, 3, 4]) >> (lambda x: List([x * x]))
- jerf 16y agoCan you show me how to convert the example I showed, using the yield-based notation? I did try but I couldn't get it to do much, not even what I thought it should. (I did add the unit for list.) print List([1, 2, 3]) >> (lambda a: List([4, 5, 6]) >> lambda b: List([(a, b)]))) does work as I expect, but I was thinking about the yield syntax when I said it doesn't work. If you can provide it I will stand corrected.
- jmillikin 16y agoOh, I didn't look much at the yield-based stuff -- I think it looks worse than the lambdas, since it depends on magic exceptions and can't be used to write procedures which work for any monad. If it doesn't work, I'm not surprised.