Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The article you link is based on sigfpe's article, which uses a flipped definition of bind:</p> <blockquote> <p>The first thing is that I've flipped the definition of <code>bind</code> and written it as the word 'bind' whereas it's normally written as the operator <code>&gt;&gt;=</code>. So <code>bind f x</code> is normally written as <code>x &gt;&gt;= f</code>.</p> </blockquote> <p>So, the Haskell <code>bind</code> takes a value enclosed in a monad, and returns a function, which takes a function and then calls it with the extracted value. I might be using non-precise terminology, so maybe better with code.</p> <p>You have:</p> <pre><code>sine x = (sin x, "sine was called.") cube x = (x * x * x, "cube was called.") </code></pre> <p>Now, translating your JS bind (Haskell does automatic currying, so calling <code>bind f</code> returns a function that takes a tuple, and then pattern matching takes care of unpacking it into <code>x</code> and <code>s</code>, I hope that's understandable):</p> <pre><code>bind f (x, s) = (y, s ++ t) where (y, t) = f x </code></pre> <p>You can see it working:</p> <pre><code>*Main&gt; :t sine sine :: Floating t =&gt; t -&gt; (t, [Char]) *Main&gt; :t bind sine bind sine :: Floating t1 =&gt; (t1, [Char]) -&gt; (t1, [Char]) *Main&gt; (bind sine . bind cube) (3, "") (0.956375928404503,"cube was called.sine was called.") </code></pre> <p>Now, let's reverse arguments of <code>bind</code>:</p> <pre><code>bind' (x, s) f = (y, s ++ t) where (y, t) = f x </code></pre> <p>You can clearly see it's still doing the same thing, but with a bit different syntax:</p> <pre><code>*Main&gt; bind' (bind' (3, "") cube) sine (0.956375928404503,"cube was called.sine was called.") </code></pre> <p>Now, Haskell has a syntax trick that allows you to use any function as an infix operator. So you can write:</p> <pre><code>*Main&gt; (3, "") `bind'` cube `bind'` sine (0.956375928404503,"cube was called.sine was called.") </code></pre> <p>Now rename <code>bind'</code> to <code>&gt;&gt;=</code> (<code>(3, "") &gt;&gt;= cube &gt;&gt;= sine</code>) and you've got what you were looking for. As you can see, with this definition, you can effectively get rid of the separate composition operator.</p> <p>Translating the new thing back into JavaScript would yield something like this (notice that again, I only reverse the argument order):</p> <pre><code>var bind = function(tuple) { return function(f) { var x = tuple[0], s = tuple[1], fx = f(x), y = fx[0], t = fx[1]; return [y, s + t]; }; }; // ugly, but it's JS, after all var f = function(x) { return bind(bind(x)(cube))(sine); } f([3, ""]); // [0.956375928404503, "cube was called.sine was called."] </code></pre> <p>Hope this helps, and not introduces more confusion — the point is that those two bind definitions are equivalent, only differing in call syntax.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload