TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Monads in Python, with nice syntax

39 pointsby limistalmost 15 years ago

2 comments

Gooseyalmost 15 years ago
Maybe I am just not understanding the true purpose of Monads. I thought they were a mathematic construct which (for lack of a better word, pardon my ignorance) allows Haskell to 'cheat' some imperativeness into it's otherwise purely functional garden of eden. Given that Python is a an imperative language to begin with, what is this trying to solve?<p>Not trolling here. Just curious.
评论 #1532234 未加载
评论 #1532125 未加载
评论 #1532115 未加载
评论 #1532194 未加载
评论 #1532093 未加载
jerfalmost 15 years 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.)<p>I mention this because this is actually critical to understanding the nature of Monads, it's not just an incidental point.<p>One way of understanding Monads is that they turn everything that would be a "statement" in most languages into a "function", and these functions are nested in each other. (Well, arguably this is what it <i>is</i>, but it's not necessarily the <i>best</i> understanding. It is in my opinion the best start, though; at least it leaves you with a true understanding, even if all the clever tricks aren't obvious yet.) Statement one actually calls into statement two calls into statement three, etc. The Maybe monad works by taking these functions (the collection of which constitute the "monadic value"), but wrapping it in a check for Nothing, and if you have a Nothing it simply returns Nothing. This has two effects: It returns Nothing, and because it stopped calling down the function chain, it stops "executing" your monadic value. Otherwise it extracts the value from its wrapper and "keeps going" into (not down, <i>into</i>; each bind or &#60;- is a nested function call) the rest of the monadic value.<p>The List monad works by taking each element for a given line in sequence, then calling the remainder of the monadic value, hence the classic<p><pre><code> cartesian_closure = do a &#60;- [1, 2, 3] b &#60;- [4, 5, 6] return (a, b) </code></pre> which will return<p><pre><code> [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)] </code></pre> See how we start with 1 for "a", then "call" the remainder of the Monadic value, which itself starts with 4 for "b" and proceeds on, and so forth. The bind function for the list monad is responsible for poking through the list like this.<p>By turning "statements" into "functions" which are then something we can manipulate like any other functions, Monads provide the "programmable semicolon" functionality, though this isn't the <i>best</i> way to understand them.<p>(This is why the State monad is pure, not "cheated imperative". It takes the old functional paradigm of passing down a state value as a parameter to a series of recursive functions, and abstracts out that parameter. It's exactly the same as the "old" functional answer, just spelled differently.)<p>(Edit: This still glosses over some things because it's a post, not a place I can spend three hours polishing. I hesitate to add another "monad tutorial" to the intarwebs...)
评论 #1532300 未加载