<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Using &#8220;foldr&#8221; in Haskell</title>
	<atom:link href="http://blog.inquirylabs.com/2008/07/17/using-foldr-in-haskell/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.inquirylabs.com/2008/07/17/using-foldr-in-haskell/</link>
	<description>Politics, Programming and Possibilities</description>
	<pubDate>Fri, 12 Mar 2010 07:10:35 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: cptfinch</title>
		<link>http://blog.inquirylabs.com/2008/07/17/using-foldr-in-haskell/#comment-27053</link>
		<dc:creator>cptfinch</dc:creator>
		<pubDate>Fri, 18 Jul 2008 10:50:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.inquirylabs.com/?p=347#comment-27053</guid>
		<description>Thanks chaps for that. I'm working my way through the YAHT pdf (Yet Another Haskell Tutorial) and the explanation of currying and parital application has cleared it up somewhat.</description>
		<content:encoded><![CDATA[<p>Thanks chaps for that. I&#8217;m working my way through the YAHT pdf (Yet Another Haskell Tutorial) and the explanation of currying and parital application has cleared it up somewhat.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BMeph</title>
		<link>http://blog.inquirylabs.com/2008/07/17/using-foldr-in-haskell/#comment-27052</link>
		<dc:creator>BMeph</dc:creator>
		<pubDate>Thu, 17 Jul 2008 18:52:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.inquirylabs.com/?p=347#comment-27052</guid>
		<description>Oh sorry for making another entry, but a help to see why/how your update works is to note the use of the function "flip." Here's the Haskell-style type signature (what you'd call a "function declaration" in C or Java, except in Haskell, they're almost always optional)

flip :: (a -&#62; b -&#62; c) -&#62; b -&#62; a -&#62; c

In short: what "flip" does, is take a two-argument function (more-or-less; there are points to make on whether "two-argument function" even makes sense - I'll assume you get my meaning), and gives back a two-argument function, just with the order of the arguments switched. So, look back at your original MyAppend:

myAppend a b = foldr (:) b a

You could, using that whole "partial application" thing, call the "foldr (:)" part a function in its own right (since it is), and use flip on it to flip the arguments back around:

myAppend a b = flip (foldr (:)) a b

Even better: you can see now, that the arguments (a b) are given to the functions in the same order - so that means they're the same function! In the world of Lambda Calculus, it's called "eta reduction," but I like to think of it as transitivity on functions. Or to put it into regular talk, "two things equal to the same thing are equal to each other," once more taking liberties with the language - please don't hurt me, Math Gods!

So, we can just drop the arguments out of the picture and write:

myAppend = flip (foldr (:))

and  Haskell accepts that as a perfectly valid definition for a function! Of course, we still have to remember that it needs two arguments given to it, to give an answer (useful to us, anyway), but that's our problem.

So, to quote your quote, that's another reason Haskell is “precision engineering for programmers”</description>
		<content:encoded><![CDATA[<p>Oh sorry for making another entry, but a help to see why/how your update works is to note the use of the function &#8220;flip.&#8221; Here&#8217;s the Haskell-style type signature (what you&#8217;d call a &#8220;function declaration&#8221; in C or Java, except in Haskell, they&#8217;re almost always optional)</p>
<p>flip :: (a -&gt; b -&gt; c) -&gt; b -&gt; a -&gt; c</p>
<p>In short: what &#8220;flip&#8221; does, is take a two-argument function (more-or-less; there are points to make on whether &#8220;two-argument function&#8221; even makes sense - I&#8217;ll assume you get my meaning), and gives back a two-argument function, just with the order of the arguments switched. So, look back at your original MyAppend:</p>
<p>myAppend a b = foldr (:) b a</p>
<p>You could, using that whole &#8220;partial application&#8221; thing, call the &#8220;foldr (:)&#8221; part a function in its own right (since it is), and use flip on it to flip the arguments back around:</p>
<p>myAppend a b = flip (foldr (:)) a b</p>
<p>Even better: you can see now, that the arguments (a b) are given to the functions in the same order - so that means they&#8217;re the same function! In the world of Lambda Calculus, it&#8217;s called &#8220;eta reduction,&#8221; but I like to think of it as transitivity on functions. Or to put it into regular talk, &#8220;two things equal to the same thing are equal to each other,&#8221; once more taking liberties with the language - please don&#8217;t hurt me, Math Gods!</p>
<p>So, we can just drop the arguments out of the picture and write:</p>
<p>myAppend = flip (foldr (:))</p>
<p>and  Haskell accepts that as a perfectly valid definition for a function! Of course, we still have to remember that it needs two arguments given to it, to give an answer (useful to us, anyway), but that&#8217;s our problem.</p>
<p>So, to quote your quote, that&#8217;s another reason Haskell is “precision engineering for programmers”</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BMeph</title>
		<link>http://blog.inquirylabs.com/2008/07/17/using-foldr-in-haskell/#comment-27051</link>
		<dc:creator>BMeph</dc:creator>
		<pubDate>Thu, 17 Jul 2008 16:59:25 +0000</pubDate>
		<guid isPermaLink="false">http://blog.inquirylabs.com/?p=347#comment-27051</guid>
		<description>I think when you say "currying," you really mean "partial application". Currying (which I associate with brushing down a horse), is where you have a function that takes, say, a pair of arguments (p,q), and "changes" it into a function that takes one argument, p, and gives back another function that takes the second argument, q, as an argument, and that function gives the same answer that just giving (p,q) to the original function gave. Partial application is where you take that other function (not the "changed" one, but the resulting function you get from giving p to the "changed" one), and use that function as a separate function in its own right (which, well, it is, it just isn't emphasized as such usually).

The point is, especially in "imperative" language use, you can't do partial application "on the fly" - you have to deliberately define a new function, with separate arguments, that calls the original function in its body. Another point is, that the arguments of a function aren't thought of as being a tuple (a pair, triple, quadruple, or whatever); the argument list is just a convenient way to show that those particular arguments are the ones being given to the function. Functions aren't thought of as being given a single, "tuple" argument; they're thought of as having multiple arguments, with a syntax that specifies those arguments which go to the function.

It helps if you have a language that facilitates returning a function as a result, as well as functions that accept other functions as arguments.</description>
		<content:encoded><![CDATA[<p>I think when you say &#8220;currying,&#8221; you really mean &#8220;partial application&#8221;. Currying (which I associate with brushing down a horse), is where you have a function that takes, say, a pair of arguments (p,q), and &#8220;changes&#8221; it into a function that takes one argument, p, and gives back another function that takes the second argument, q, as an argument, and that function gives the same answer that just giving (p,q) to the original function gave. Partial application is where you take that other function (not the &#8220;changed&#8221; one, but the resulting function you get from giving p to the &#8220;changed&#8221; one), and use that function as a separate function in its own right (which, well, it is, it just isn&#8217;t emphasized as such usually).</p>
<p>The point is, especially in &#8220;imperative&#8221; language use, you can&#8217;t do partial application &#8220;on the fly&#8221; - you have to deliberately define a new function, with separate arguments, that calls the original function in its body. Another point is, that the arguments of a function aren&#8217;t thought of as being a tuple (a pair, triple, quadruple, or whatever); the argument list is just a convenient way to show that those particular arguments are the ones being given to the function. Functions aren&#8217;t thought of as being given a single, &#8220;tuple&#8221; argument; they&#8217;re thought of as having multiple arguments, with a syntax that specifies those arguments which go to the function.</p>
<p>It helps if you have a language that facilitates returning a function as a result, as well as functions that accept other functions as arguments.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
