<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: one line binary reader in haskell</title>
	<atom:link href="http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/feed/" rel="self" type="application/rss+xml" />
	<link>http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/</link>
	<description>ramblings of a zen buddhist geek.</description>
	<lastBuildDate>Fri, 14 Aug 2009 03:11:53 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: huh. google knows about me now, go figure. &#171; tehgeekmeister&#8217;s blog</title>
		<link>http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-12</link>
		<dc:creator>huh. google knows about me now, go figure. &#171; tehgeekmeister&#8217;s blog</dc:creator>
		<pubDate>Mon, 14 Jan 2008 06:08:15 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-12</guid>
		<description>[...] &#8212; tehgeekmeister @ 10:08 pm   apparently submitting that last post to reddit has gotten my previous post to be in the first page of google results for &#8220;binary reader&#8220;. cool, i&#8217;m fine [...]</description>
		<content:encoded><![CDATA[<p>[...] &#8212; tehgeekmeister @ 10:08 pm   apparently submitting that last post to reddit has gotten my previous post to be in the first page of google results for &#8220;binary reader&#8220;. cool, i&#8217;m fine [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tehgeekmeister</title>
		<link>http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-9</link>
		<dc:creator>tehgeekmeister</dc:creator>
		<pubDate>Sun, 13 Jan 2008 02:30:19 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-9</guid>
		<description>Mike: that&#039;s true, i figured there was probably a way to get around that.  my head isn&#039;t alway so clear when i&#039;m going thru these exercises, and i&#039;ve not done that much programming, it being a hobby and certainly not my job.  that being said, i&#039;m glad to know where the problem in my algorithm was!  good point about using foldl&#039;, too.

Paczesiowa: that&#039;s very concise and elegant, but i &lt;b&gt;really&lt;/b&gt; don&#039;t like that it would successfully parse non-binary strings.  of course, in the case where the string has already been parsed (exactly what i&#039;m doing) that&#039;s not a problem.  that aside, i think i like that version the very best of all the versions i&#039;ve seen.</description>
		<content:encoded><![CDATA[<p>Mike: that&#8217;s true, i figured there was probably a way to get around that.  my head isn&#8217;t alway so clear when i&#8217;m going thru these exercises, and i&#8217;ve not done that much programming, it being a hobby and certainly not my job.  that being said, i&#8217;m glad to know where the problem in my algorithm was!  good point about using foldl&#8217;, too.</p>
<p>Paczesiowa: that&#8217;s very concise and elegant, but i <b>really</b> don&#8217;t like that it would successfully parse non-binary strings.  of course, in the case where the string has already been parsed (exactly what i&#8217;m doing) that&#8217;s not a problem.  that aside, i think i like that version the very best of all the versions i&#8217;ve seen.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paczesiowa</title>
		<link>http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-8</link>
		<dc:creator>Paczesiowa</dc:creator>
		<pubDate>Sun, 13 Jan 2008 02:17:08 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-8</guid>
		<description>import Data.Char
readBin = foldl (\a b -&gt; 2*a + digitToInt b) 0</description>
		<content:encoded><![CDATA[<p>import Data.Char<br />
readBin = foldl (\a b -&gt; 2*a + digitToInt b) 0</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Laiosa</title>
		<link>http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-7</link>
		<dc:creator>Mike Laiosa</dc:creator>
		<pubDate>Sat, 12 Jan 2008 10:01:31 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-7</guid>
		<description>As you might be able to glean from comment #2, the (`div` 2) in your version is unneeded if you change the fold around a bit:

readBin  = foldl (\x y -&gt; x*2 + y) 0 . map (\c -&gt; case c of {’0′ -&gt; 0; ‘1′ -&gt; 1; _ -&gt; error “Input is not a binary string.”})

And I would be inclined to put the body of the map into the fold too:

readBin = foldl (\x y -&gt; x*2 + case y of {&#039;0&#039; -&gt; 0; &#039;1&#039;-&gt; 1;_ -&gt; error &quot;Input is not a binary string&quot;}) 0

You should also use foldl&#039; from Data.List to cut down on lazyness.  (That buys you nothing - readBin always needs to read the whole list).  As cool as the one line thing is, I&#039;d really be inclined to do it in a couple more, since I don&#039;t care for either one-line case expressions or lambdas.</description>
		<content:encoded><![CDATA[<p>As you might be able to glean from comment #2, the (`div` 2) in your version is unneeded if you change the fold around a bit:</p>
<p>readBin  = foldl (\x y -&gt; x*2 + y) 0 . map (\c -&gt; case c of {’0′ -&gt; 0; ‘1′ -&gt; 1; _ -&gt; error “Input is not a binary string.”})</p>
<p>And I would be inclined to put the body of the map into the fold too:</p>
<p>readBin = foldl (\x y -&gt; x*2 + case y of {&#8216;0&#8242; -&gt; 0; &#8216;1&#8242;-&gt; 1;_ -&gt; error &#8220;Input is not a binary string&#8221;}) 0</p>
<p>You should also use foldl&#8217; from Data.List to cut down on lazyness.  (That buys you nothing &#8211; readBin always needs to read the whole list).  As cool as the one line thing is, I&#8217;d really be inclined to do it in a couple more, since I don&#8217;t care for either one-line case expressions or lambdas.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tehgeekmeister</title>
		<link>http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-6</link>
		<dc:creator>tehgeekmeister</dc:creator>
		<pubDate>Sat, 12 Jan 2008 05:09:29 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-6</guid>
		<description>Meneer: your first fold version is the one i prefer for readability and my purposes of all your versions (i&#039;m merely converting already parsed binary strings in the course of going thru &lt;a&gt;http://halogen.note.amherst.edu/~jdtang/scheme_in_48/tutorial/overview.html&lt;/a&gt;, so there&#039;s really no need for any error reporting at all, i only added it for the purposes of the post.).  also, a maybe based version is certainly more elegant than throwing an error, but i didn&#039;t know how to do that in a one-liner, so thanks for showing me a few versions.

also, i must admit that the version in the reddit comment is beautifully succint.  i&#039;m glad i posted this so that i could see some better versions.</description>
		<content:encoded><![CDATA[<p>Meneer: your first fold version is the one i prefer for readability and my purposes of all your versions (i&#8217;m merely converting already parsed binary strings in the course of going thru <a>http://halogen.note.amherst.edu/~jdtang/scheme_in_48/tutorial/overview.html</a>, so there&#8217;s really no need for any error reporting at all, i only added it for the purposes of the post.).  also, a maybe based version is certainly more elegant than throwing an error, but i didn&#8217;t know how to do that in a one-liner, so thanks for showing me a few versions.</p>
<p>also, i must admit that the version in the reddit comment is beautifully succint.  i&#8217;m glad i posted this so that i could see some better versions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Meneer R</title>
		<link>http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-5</link>
		<dc:creator>Meneer R</dc:creator>
		<pubDate>Sat, 12 Jan 2008 03:39:08 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-5</guid>
		<description>On reddit i found even a pretties version!

readBin = foldM (\a d -&gt; (a*2 +)  elemIndex d &quot;01&quot;) 0

Autheur: PjdePort
Link: http://programming.reddit.com/info/6597u/comments/c02vlf9</description>
		<content:encoded><![CDATA[<p>On reddit i found even a pretties version!</p>
<p>readBin = foldM (\a d -&gt; (a*2 +)  elemIndex d &#8220;01&#8243;) 0</p>
<p>Autheur: PjdePort<br />
Link: <a href="http://programming.reddit.com/info/6597u/comments/c02vlf9" rel="nofollow">http://programming.reddit.com/info/6597u/comments/c02vlf9</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Meneer R</title>
		<link>http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-4</link>
		<dc:creator>Meneer R</dc:creator>
		<pubDate>Sat, 12 Jan 2008 03:32:59 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/2008/01/11/one-line-binary-reader-in-haskell/#comment-4</guid>
		<description>The most readable version, unfortunately it works in reverse:

  rBin (&#039;0&#039;:xs) = 0 + 2 * rBin xs
  rBin (&#039;1&#039;:xs) = 1 + 2 * rBin xs
  rBin []       = 0

  readBin1 = rBin . reverse 

If you don&#039;t care for the sort of error reporting, the shortest code I could come up with is:

  readBin2 = foldl (\ x y -&gt; fromJust(elemIndex y &quot;01&quot;) + 2*x ) 0 

This will give you an pattern-match-failure with fromJust with badly formatted string. If you want a nicer error message:

  readBin4 = foldl (\ x y -&gt; fromMaybe (error &quot;Input is not binary&quot;) (elemIndex y &quot;01&quot;) + 2*x ) 0 

Still a pretty oneliner. But not points-free. Let&#039;s make it points-free!

  readBin5 = flip foldl 0 $ flip $  (+) . (*2) . (fromMaybe (error &quot;Input is not binary&quot;)) . (flip elemIndex &quot;01&quot;)

Just to show that you can truly program brainfuck style in Haskell if you want to.

If you want to actually deal with the error, rather than crash, you are better off using something like the Maybe datatype:

  readBin :: String -&gt; Maybe Integer
  readBin = foldl (\ x y -&gt; do { c &lt;- x; d &lt;- elemIndex y &quot;01&quot;; return (d + (2*c)) }) (return 0)

I&#039;ll leave the point-free version of this one as an excersize. You could either use Maybe utitily functions, or the monadic functions to achieve this.</description>
		<content:encoded><![CDATA[<p>The most readable version, unfortunately it works in reverse:</p>
<p>  rBin (&#8216;0&#8242;:xs) = 0 + 2 * rBin xs<br />
  rBin (&#8216;1&#8242;:xs) = 1 + 2 * rBin xs<br />
  rBin []       = 0</p>
<p>  readBin1 = rBin . reverse </p>
<p>If you don&#8217;t care for the sort of error reporting, the shortest code I could come up with is:</p>
<p>  readBin2 = foldl (\ x y -&gt; fromJust(elemIndex y &#8220;01&#8243;) + 2*x ) 0 </p>
<p>This will give you an pattern-match-failure with fromJust with badly formatted string. If you want a nicer error message:</p>
<p>  readBin4 = foldl (\ x y -&gt; fromMaybe (error &#8220;Input is not binary&#8221;) (elemIndex y &#8220;01&#8243;) + 2*x ) 0 </p>
<p>Still a pretty oneliner. But not points-free. Let&#8217;s make it points-free!</p>
<p>  readBin5 = flip foldl 0 $ flip $  (+) . (*2) . (fromMaybe (error &#8220;Input is not binary&#8221;)) . (flip elemIndex &#8220;01&#8243;)</p>
<p>Just to show that you can truly program brainfuck style in Haskell if you want to.</p>
<p>If you want to actually deal with the error, rather than crash, you are better off using something like the Maybe datatype:</p>
<p>  readBin :: String -&gt; Maybe Integer<br />
  readBin = foldl (\ x y -&gt; do { c &lt;- x; d &lt;- elemIndex y &#8220;01&#8243;; return (d + (2*c)) }) (return 0)</p>
<p>I&#8217;ll leave the point-free version of this one as an excersize. You could either use Maybe utitily functions, or the monadic functions to achieve this.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
