4 ms·
Hmm, I see. The problem with my understanding it is that people are describing its properties and I have to build the mental model from them. Are Python list c
by StavrosK 16y ago
Hmm, I see. The problem with my understanding it is that people are describing its properties and I have to build the mental model from them.
Are Python list comprehensions monads?
- chc 16y agoWell, it's the type itself that's a monad. So the question would be, are Python lists monads thanks to list comprehensions? And the answer is: Kind of. The idea of a monad in functional programming is that it's a generic type that can have a lot of different implementations. The two operations that must be defined for a monad are: • Return: You pass any value and it returns a monad encapsulating that value • Bind: You pass a function taking an argument of the type wrapped by the monad and returning another value wrapped in the monad, and it returns a value of the monad With these two operations, you can accomplish a surprising range of tasks. Python borrowed list comprehensions from Haskell, where they are based on monads — but in Python, they're kind of orphaned from their monadic roots.
- StavrosK 16y agoThis makes it much clearer, thanks. Those two operations weren't mentioned anywhere else that I saw, and they are very helpful in explaining what monads are.