<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Haskell School of Expression: Exercises</title>
	<atom:link href="http://www.elbeno.com/haskell_soe_blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.elbeno.com/haskell_soe_blog</link>
	<description>solutions from the exercises in the book</description>
	<lastBuildDate>Mon, 30 Mar 2009 06:46:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Exercise 11.1</title>
		<link>http://www.elbeno.com/haskell_soe_blog/?p=59</link>
		<comments>http://www.elbeno.com/haskell_soe_blog/?p=59#comments</comments>
		<pubDate>Tue, 01 Jan 2008 19:35:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.elbeno.com/haskell_soe_blog/?p=59</guid>
		<description><![CDATA[1) Prove that (∀cs &#124; cs is finite) putCharList cs = map putChar cs where putCharList &#91;&#93; = &#91;&#93; putCharList &#40;c:cs&#41; = putChar c : putCharList cs &#160; map f &#91;&#93; = &#91;&#93; map f &#40;x:xs&#41; = f x : map f xs Base case: putCharList &#91;&#93; =&#62; &#91;&#93; =&#62; map putChar &#91;&#93; Inductive Step: [...]]]></description>
			<content:encoded><![CDATA[<p>1) Prove that <tt>(∀cs | cs is finite) putCharList cs = map putChar cs</tt></p>
<p>where</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">putCharList <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
putCharList <span style="color: green;">&#40;</span>c:cs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">putChar</span> c : putCharList cs
&nbsp;
<span style="font-weight: bold;">map</span> f <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
<span style="font-weight: bold;">map</span> f <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> f x : <span style="font-weight: bold;">map</span> f xs</pre></div></div>

<p>Base case:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">putCharList <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
  <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
  <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="font-weight: bold;">map</span> <span style="font-weight: bold;">putChar</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span></pre></div></div>

<p>Inductive Step:</p>
<p>assume that <tt>purCharList cs = map putChar cs</tt></p>
<p>then</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">putCharList <span style="color: green;">&#40;</span>c:cs<span style="color: green;">&#41;</span>
  <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="font-weight: bold;">putChar</span> c : putCharList cs
  <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="font-weight: bold;">putChar</span> c : <span style="font-weight: bold;">map</span> <span style="font-weight: bold;">putChar</span> cs
  <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="font-weight: bold;">map</span> <span style="font-weight: bold;">putChar</span> <span style="color: green;">&#40;</span>c:cs<span style="color: green;">&#41;</span></pre></div></div>

<p>QED.</p>
<p>2) Prove that <tt>(∀xs | xs is finite) listProd xs = fold (*) 1 xs</tt></p>
<p>where</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">listProd <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1</span>
listProd <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> x <span style="color: #339933; font-weight: bold;">*</span> listProd xs
&nbsp;
fold op <span style="font-weight: bold;">init</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">init</span>
fold op <span style="font-weight: bold;">init</span> <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> x `op` fold op <span style="font-weight: bold;">init</span> xs</pre></div></div>

<p>Base case:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">listProd <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
  <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: red;">1</span>
  <span style="color: #339933; font-weight: bold;">=&gt;</span> fold <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> <span style="color: red;">1</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span></pre></div></div>

<p>Inductive Step:</p>
<p>assume that <tt>listProd xs = fold (*) 1 xs</tt></p>
<p>then</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">listProd <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span>
  <span style="color: #339933; font-weight: bold;">=&gt;</span> x <span style="color: #339933; font-weight: bold;">*</span> listProd xs
  <span style="color: #339933; font-weight: bold;">=&gt;</span> x <span style="color: #339933; font-weight: bold;">*</span> fold <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> <span style="color: red;">1</span> xs
  <span style="color: #339933; font-weight: bold;">=&gt;</span> fold <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> <span style="color: red;">1</span> <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span></pre></div></div>

<p>QED.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elbeno.com/haskell_soe_blog/?feed=rss2&amp;p=59</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 10.4</title>
		<link>http://www.elbeno.com/haskell_soe_blog/?p=58</link>
		<comments>http://www.elbeno.com/haskell_soe_blog/?p=58#comments</comments>
		<pubDate>Tue, 01 Jan 2008 07:18:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.elbeno.com/haskell_soe_blog/?p=58</guid>
		<description><![CDATA[A quick look at Graphics.SOE reveals that getWindowEvent is the right function to trap mouse down, up, and move events separately. {- maybeClear is useful for avoiding flicker when not dragging objects. A real flicker-free solution should use double-buffering of course - this code still flickers terribly while dragging. -} &#160; maybeClear :: Bool -&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>A quick look at Graphics.SOE reveals that <tt>getWindowEvent</tt> is the right function to trap mouse down, up, and move events separately.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">{-
maybeClear is useful for avoiding flicker when not dragging objects.
A real flicker-free solution should use double-buffering of course -
this code still flickers terribly while dragging.
-}</span>
&nbsp;
maybeClear <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Bool</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> Window <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
maybeClear b w
    <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> b
      <span style="color: #06c; font-weight: bold;">then</span> <span style="color: #06c; font-weight: bold;">do</span> clearWindow w
      <span style="color: #06c; font-weight: bold;">else</span> <span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">{-
The program is in two halves: either we're waiting for a click (not dragging),
or we're waiting for a release (dragging). Waiting for a click is almost
identical to the previous definition of loop, except for the event
function.
-}</span>
&nbsp;
waitForClick <span style="color: #339933; font-weight: bold;">::</span> Window <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>Color<span style="color: #339933; font-weight: bold;">,</span> Region<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
waitForClick w regs b
    <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> maybeClear b w
         <span style="font-weight: bold;">sequence_</span> <span style="color: green;">&#91;</span>drawRegionInWindow w c r <span style="color: #339933; font-weight: bold;">|</span> <span style="color: green;">&#40;</span>c<span style="color: #339933; font-weight: bold;">,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="font-weight: bold;">reverse</span> regs<span style="color: green;">&#93;</span>
         event <span style="color: #339933; font-weight: bold;">&lt;-</span> getWindowEvent w
         <span style="color: #06c; font-weight: bold;">case</span> event <span style="color: #06c; font-weight: bold;">of</span>
           <span style="color: green;">&#40;</span>Button <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> True True<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span>
               <span style="color: #06c; font-weight: bold;">let</span> aux <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> r `containsR` <span style="color: green;">&#40;</span>pixelToInch <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">-</span> xWin2<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>
                                              pixelToInch <span style="color: green;">&#40;</span>yWin2 <span style="color: #339933; font-weight: bold;">-</span> y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
               <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #06c; font-weight: bold;">case</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">break</span> aux regs<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">of</span>
                    <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> closeWindow w
                    <span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">,</span> hit:bot<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> waitForRelease <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> w 
                                     <span style="color: green;">&#40;</span>hit : <span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">++</span>bot<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> True
           <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> waitForClick w regs False
&nbsp;
<span style="color: #5d478b; font-style: italic;">{-
While dragging, we keep track of the initial click coordinates and translate
the Region at the head of the list by the offset of the current mouse 
coordinates. When the mouse button is released, we go back to waiting 
for a click, leaving the head Region where it is.
-}</span>
&nbsp;
waitForRelease <span style="color: #339933; font-weight: bold;">::</span> Point <span style="color: #339933; font-weight: bold;">-&gt;</span> Point <span style="color: #339933; font-weight: bold;">-&gt;</span> Window <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>Color<span style="color: #339933; font-weight: bold;">,</span> Region<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
waitForRelease <span style="color: green;">&#40;</span>origx<span style="color: #339933; font-weight: bold;">,</span> origy<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> w regs b
    <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> maybeClear b w
         <span style="font-weight: bold;">sequence_</span> <span style="color: green;">&#91;</span>drawRegionInWindow w c r <span style="color: #339933; font-weight: bold;">|</span> <span style="color: green;">&#40;</span>c<span style="color: #339933; font-weight: bold;">,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="font-weight: bold;">reverse</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">tail</span> regs<span style="color: green;">&#93;</span>
         <span style="color: #06c; font-weight: bold;">let</span> <span style="color: green;">&#40;</span>c<span style="color: #339933; font-weight: bold;">,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">head</span> regs
             newHeadReg <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>Translate <span style="color: green;">&#40;</span>pixelToInch <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">-</span> origx<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> pixelToInch<span style="color: green;">&#40;</span>origy <span style="color: #339933; font-weight: bold;">-</span> y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> r<span style="color: green;">&#41;</span>
         drawRegionInWindow w c newHeadReg
         event <span style="color: #339933; font-weight: bold;">&lt;-</span> getWindowEvent w
         <span style="color: #06c; font-weight: bold;">case</span> event <span style="color: #06c; font-weight: bold;">of</span>
           <span style="color: green;">&#40;</span>Button <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> True False<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> waitForClick w <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>c<span style="color: #339933; font-weight: bold;">,</span>newHeadReg<span style="color: green;">&#41;</span> : <span style="color: green;">&#40;</span><span style="font-weight: bold;">tail</span> regs<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> True
           <span style="color: green;">&#40;</span>MouseMove pt<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> waitForRelease <span style="color: green;">&#40;</span>origx<span style="color: #339933; font-weight: bold;">,</span>origy<span style="color: green;">&#41;</span> pt w regs True
           <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> waitForRelease <span style="color: green;">&#40;</span>origx<span style="color: #339933; font-weight: bold;">,</span>origy<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> w regs False
&nbsp;
<span style="color: #5d478b; font-style: italic;">{-
All that remains is to drive the drag and drop functionality with some boilerplate
draw and main functions.
-}</span>
&nbsp;
draw3 <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">String</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> Picture <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
draw3 s p <span style="color: #339933; font-weight: bold;">=</span> runGraphics <span style="color: #339933; font-weight: bold;">$</span>
            <span style="color: #06c; font-weight: bold;">do</span> w <span style="color: #339933; font-weight: bold;">&lt;-</span> openWindow s <span style="color: green;">&#40;</span>xWin<span style="color: #339933; font-weight: bold;">,</span> yWin<span style="color: green;">&#41;</span>
               waitForClick w <span style="color: green;">&#40;</span>pictToList p<span style="color: green;">&#41;</span> True
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> draw3 <span style="background-color: #3cb371;">&quot;Drag and Drop&quot;</span> pic</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.elbeno.com/haskell_soe_blog/?feed=rss2&amp;p=58</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 10.3</title>
		<link>http://www.elbeno.com/haskell_soe_blog/?p=57</link>
		<comments>http://www.elbeno.com/haskell_soe_blog/?p=57#comments</comments>
		<pubDate>Tue, 01 Jan 2008 00:53:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.elbeno.com/haskell_soe_blog/?p=57</guid>
		<description><![CDATA[Prove is, to my mind, a strong word. But by substitution we can show that these functions are equivalent. First, the originals: adjust :: &#91;&#40;Color, Region&#41;&#93; -&#62; Coordinate -&#62; &#40;Maybe &#40;Color, Region&#41;, &#91;&#40;Color, Region&#41;&#93;&#41; adjust regs p = case &#40;break &#40;\&#40;_,r&#41; -&#62; r `containsR` p&#41; regs&#41; of &#40;top, hit:rest&#41; -&#62; &#40;Just hit, top++rest&#41; &#40;_, &#91;&#93;&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>Prove is, to my mind, a strong word. But by substitution we can show that these functions are equivalent. First, the originals:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">adjust <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>Color<span style="color: #339933; font-weight: bold;">,</span> Region<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> Coordinate
          <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Maybe</span> <span style="color: green;">&#40;</span>Color<span style="color: #339933; font-weight: bold;">,</span> Region<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>Color<span style="color: #339933; font-weight: bold;">,</span> Region<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>
adjust regs p
    <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">case</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">break</span> <span style="color: green;">&#40;</span>\<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> r `containsR` p<span style="color: green;">&#41;</span> regs<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">of</span>
        <span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">,</span> hit:rest<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Just hit<span style="color: #339933; font-weight: bold;">,</span> top<span style="color: #339933; font-weight: bold;">++</span>rest<span style="color: green;">&#41;</span>
        <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Nothing<span style="color: #339933; font-weight: bold;">,</span> regs<span style="color: green;">&#41;</span>
&nbsp;
loop <span style="color: #339933; font-weight: bold;">::</span> Window <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>Color<span style="color: #339933; font-weight: bold;">,</span> Region<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
loop w regs
    <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> clearWindow w
         <span style="font-weight: bold;">sequence_</span> <span style="color: green;">&#91;</span>drawRegionInWindow w c r <span style="color: #339933; font-weight: bold;">|</span> <span style="color: green;">&#40;</span>c<span style="color: #339933; font-weight: bold;">,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="font-weight: bold;">reverse</span> regs<span style="color: green;">&#93;</span>
         <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;-</span> getLBP w
         <span style="color: #06c; font-weight: bold;">case</span> <span style="color: green;">&#40;</span>adjust regs <span style="color: green;">&#40;</span>pixelToInch <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">-</span> xWin2<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>
                            pixelToInch <span style="color: green;">&#40;</span>yWin2 <span style="color: #339933; font-weight: bold;">-</span> y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">of</span>
           <span style="color: green;">&#40;</span>Nothing<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> closeWindow w
           <span style="color: green;">&#40;</span>Just hit<span style="color: #339933; font-weight: bold;">,</span> newRegs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> loop w <span style="color: green;">&#40;</span>hit:newRegs<span style="color: green;">&#41;</span></pre></div></div>

<p>We can easily alter <tt>adjust</tt> to:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">adjust regs p
    <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span> aux <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> r `containsR` p
      <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #06c; font-weight: bold;">case</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">break</span> aux regs<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">of</span>
           <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Nothing<span style="color: #339933; font-weight: bold;">,</span> regs<span style="color: green;">&#41;</span>
           <span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">,</span> hit:bot<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Just hit<span style="color: #339933; font-weight: bold;">,</span> top<span style="color: #339933; font-weight: bold;">++</span>bot<span style="color: green;">&#41;</span></pre></div></div>

<p>simply pulling the anonymous function into a <tt>let</tt>, renaming <tt>rest</tt> to <tt>bot</tt> and reordering the <tt>case</tt> clauses. Let&#8217;s further substitute the bound value of <tt>p</tt> into <tt>adjust</tt>:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">adjust regs
    <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span> aux <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> r `containsR` <span style="color: green;">&#40;</span>pixelToInch <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">-</span> wWin2<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>
                                     pixelToInch <span style="color: green;">&#40;</span>yWin2 <span style="color: #339933; font-weight: bold;">-</span> y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
      <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #06c; font-weight: bold;">case</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">break</span> aux regs<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">of</span>
           <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Nothing<span style="color: #339933; font-weight: bold;">,</span> regs<span style="color: green;">&#41;</span>
           <span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">,</span> hit:bot<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Just hit<span style="color: #339933; font-weight: bold;">,</span> top<span style="color: #339933; font-weight: bold;">++</span>bot<span style="color: green;">&#41;</span></pre></div></div>

<p>Now consider what <tt>loop</tt> does for each return value of <tt>adjust</tt>, and substitute.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Nothing<span style="color: #339933; font-weight: bold;">,</span> regs<span style="color: green;">&#41;</span>     <span style="color: #5d478b; font-style: italic;">{- from adjust -}</span>
<span style="color: green;">&#40;</span>Nothing<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> closeWindow w  <span style="color: #5d478b; font-style: italic;">{- from loop -}</span></pre></div></div>

<p><tt>(Nothing, regs)</tt> is bound to <tt>(Nothing, _)</tt><br />
therefore we can substitute:<br />
<tt>(_, []) -> closeWindow w</tt></p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">,</span> hit:bot<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Just hit<span style="color: #339933; font-weight: bold;">,</span> top<span style="color: #339933; font-weight: bold;">++</span>bot<span style="color: green;">&#41;</span>        <span style="color: #5d478b; font-style: italic;">{- from adjust -}</span>
<span style="color: green;">&#40;</span>Just hit<span style="color: #339933; font-weight: bold;">,</span> newRegs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> loop w <span style="color: green;">&#40;</span>hit : newRegs<span style="color: green;">&#41;</span> <span style="color: #5d478b; font-style: italic;">{- from loop -}</span></pre></div></div>

<p><tt>(Just hit, top++bot)</tt> is bound to <tt>(Just hit, newRegs)</tt><br />
therefore we can substitute:<br />
<tt>(top, hit:bot) -> loop w (hit : (top++bot))</tt></p>
<p>Our new <tt>adjust</tt> variant is now:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">adjust regs
    <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span> aux <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> r `containsR` <span style="color: green;">&#40;</span>pixelToInch <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">-</span> wWin2<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>
                                     pixelToInch <span style="color: green;">&#40;</span>yWin2 <span style="color: #339933; font-weight: bold;">-</span> y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
      <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #06c; font-weight: bold;">case</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">break</span> aux regs<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">of</span>
           <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> closeWindow w
           <span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">,</span> hit:bot<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> loop w <span style="color: green;">&#40;</span>hit : <span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">++</span>bot<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>Obviously at this point there are various unbound variables in this variant of <tt>adjust</tt>, so it cannot stand alone. But it can now itself be substituted into the body of <tt>loop</tt>:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">loop w regs
    <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> clearWindow w
         <span style="font-weight: bold;">sequence_</span> <span style="color: green;">&#91;</span>drawRegionInWindow w c r <span style="color: #339933; font-weight: bold;">|</span> <span style="color: green;">&#40;</span>c<span style="color: #339933; font-weight: bold;">,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="font-weight: bold;">reverse</span> regs<span style="color: green;">&#93;</span>
         <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;-</span> getLBP w
         <span style="color: #06c; font-weight: bold;">let</span> aux <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> r `containsR` <span style="color: green;">&#40;</span>pixelToInch <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">-</span> xWin2<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>
                                        pixelToInch <span style="color: green;">&#40;</span>yWin2 <span style="color: #339933; font-weight: bold;">-</span> y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
         <span style="color: #06c; font-weight: bold;">case</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">break</span> aux regs<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">of</span>
           <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> closeWindow w
           <span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">,</span> hit:bot<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> loop w <span style="color: green;">&#40;</span>hit : <span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">++</span>bot<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>We lose the <tt>in</tt> because we are inside the <tt>do</tt> form. The only thing that remains is to show that<br />
<tt>sequence_ [drawRegionInWindow w c r | (c,r) <- reverse regs]</tt><br />
is equivalent to<br />
<tt>sequence_ (map (uncurry (drawRegionInWindow w)) (reverse regs))</tt></p>
<p>The prelude defines <tt>uncurry</tt>:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">uncurry</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b <span style="color: #339933; font-weight: bold;">-&gt;</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a<span style="color: #339933; font-weight: bold;">,</span>b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> c<span style="color: green;">&#41;</span>
<span style="font-weight: bold;">uncurry</span> f p <span style="color: #339933; font-weight: bold;">=</span> f <span style="color: green;">&#40;</span><span style="font-weight: bold;">fst</span> p<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">snd</span> p<span style="color: green;">&#41;</span></pre></div></div>

<p><tt>(drawRegionInWindow w)</tt> is a function of type <tt>Color -> Region -> IO ()</tt>.<br />
<tt>(uncurry (drawRegionInWindow w))</tt> is a function of type <tt>(Color, Region) -> IO ()</tt>.<br />
<tt>(reverse regs)</tt> is of type <tt>[(Color, Region)]</tt>.</p>
<p>So by inspection we can see that the two forms are equivalent. <tt>map</tt> and <tt>uncurry</tt> do explicitly what the list comprehension does implicitly. Equivalency of the final form of <tt>loop</tt> follows.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">loop w regs
    <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> clearWindow w
         <span style="font-weight: bold;">sequence_</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">uncurry</span> <span style="color: green;">&#40;</span>drawRegionInWindow w<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">reverse</span> regs<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
         <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;-</span> getLBP w
         <span style="color: #06c; font-weight: bold;">let</span> aux <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span>r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> r `containsR` <span style="color: green;">&#40;</span>pixelToInch <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">-</span> xWin2<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>
                                        pixelToInch <span style="color: green;">&#40;</span>yWin2 <span style="color: #339933; font-weight: bold;">-</span> y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
         <span style="color: #06c; font-weight: bold;">case</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">break</span> aux regs<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">of</span>
           <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">_,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> closeWindow w
           <span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">,</span> hit:bot<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> loop w <span style="color: green;">&#40;</span>hit : <span style="color: green;">&#40;</span>top<span style="color: #339933; font-weight: bold;">++</span>bot<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.elbeno.com/haskell_soe_blog/?feed=rss2&amp;p=57</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 10.2</title>
		<link>http://www.elbeno.com/haskell_soe_blog/?p=56</link>
		<comments>http://www.elbeno.com/haskell_soe_blog/?p=56#comments</comments>
		<pubDate>Mon, 31 Dec 2007 20:25:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.elbeno.com/haskell_soe_blog/?p=56</guid>
		<description><![CDATA[Simply run the code given in the book. The Graphics.SOE package seems to be buggy with respect to updating the window properly. I had redraw bugs using both GHCI and Hugs (using Ubuntu 7.10 on x86_84).]]></description>
			<content:encoded><![CDATA[<p>Simply run the code given in the book. The Graphics.SOE package seems to be <a href="http://www.mail-archive.com/glasgow-haskell-bugs@haskell.org/msg12646.html">buggy with respect to updating the window</a> properly. I had redraw bugs using both GHCI and Hugs (using Ubuntu 7.10 on x86_84).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elbeno.com/haskell_soe_blog/?feed=rss2&amp;p=56</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 10.1</title>
		<link>http://www.elbeno.com/haskell_soe_blog/?p=55</link>
		<comments>http://www.elbeno.com/haskell_soe_blog/?p=55#comments</comments>
		<pubDate>Sat, 29 Dec 2007 04:54:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.elbeno.com/haskell_soe_blog/?p=55</guid>
		<description><![CDATA[This exercise is simply to execute the code given in the book. To draw the &#8220;five circles&#8221; example: draw &#34;five circles&#34; $ Region Blue $ Translate &#40;-2,0&#41; $ Scale &#40;0.5, 0.5&#41; fiveCircles Note: Technical errors 17, 18 and 19 apply to the code (in particular shapeToGRegion) in Chapter 10.]]></description>
			<content:encoded><![CDATA[<p>This exercise is simply to execute the code given in the book.</p>
<p>To draw the &#8220;five circles&#8221; example:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">draw <span style="background-color: #3cb371;">&quot;five circles&quot;</span> <span style="color: #339933; font-weight: bold;">$</span> Region Blue <span style="color: #339933; font-weight: bold;">$</span> Translate <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</span>
                    <span style="color: #339933; font-weight: bold;">$</span> Scale <span style="color: green;">&#40;</span><span style="color: red;">0.5</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">0.5</span><span style="color: green;">&#41;</span> fiveCircles</pre></div></div>

<p>Note: <a href="http://www.haskell.org/soe/Bug/erata.htm">Technical errors</a> 17, 18 and 19 apply to the code (in particular <tt>shapeToGRegion</tt>) in Chapter 10.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elbeno.com/haskell_soe_blog/?feed=rss2&amp;p=55</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 9.12</title>
		<link>http://www.elbeno.com/haskell_soe_blog/?p=54</link>
		<comments>http://www.elbeno.com/haskell_soe_blog/?p=54#comments</comments>
		<pubDate>Fri, 10 Aug 2007 04:13:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.elbeno.com/haskell_soe_blog/?p=54</guid>
		<description><![CDATA[Instances where we can curry and convert anonymous functions to sections are easy to find, for example from Exercise 5.5: doubleEach'' :: &#91;Int&#93; -&#62; &#91;Int&#93; doubleEach'' = map &#40;*2&#41;]]></description>
			<content:encoded><![CDATA[<p>Instances where we can curry and convert anonymous functions to sections are easy to find, for example from <a href="http://www.elbeno.com/haskell_soe_blog/?p=19">Exercise 5.5</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">doubleEach'' <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span>
doubleEach'' <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: red;">2</span><span style="color: green;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.elbeno.com/haskell_soe_blog/?feed=rss2&amp;p=54</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Exercise 9.11</title>
		<link>http://www.elbeno.com/haskell_soe_blog/?p=53</link>
		<comments>http://www.elbeno.com/haskell_soe_blog/?p=53#comments</comments>
		<pubDate>Fri, 10 Aug 2007 03:36:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.elbeno.com/haskell_soe_blog/?p=53</guid>
		<description><![CDATA[map f &#40;map g xs&#41; = map &#40;f.g&#41; xs &#160; map &#40;\x -&#62; &#40;x+1&#41;/2&#41; xs = map &#40;/2&#41; &#40;map &#40;+1&#41; xs&#41;]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">map</span> f <span style="color: green;">&#40;</span><span style="font-weight: bold;">map</span> g xs<span style="color: green;">&#41;</span>
<span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>f<span style="color: #339933; font-weight: bold;">.</span>g<span style="color: green;">&#41;</span> xs
&nbsp;
<span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">/</span><span style="color: red;">2</span><span style="color: green;">&#41;</span> xs
<span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">/</span><span style="color: red;">2</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span> xs<span style="color: green;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.elbeno.com/haskell_soe_blog/?feed=rss2&amp;p=53</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 9.10</title>
		<link>http://www.elbeno.com/haskell_soe_blog/?p=52</link>
		<comments>http://www.elbeno.com/haskell_soe_blog/?p=52#comments</comments>
		<pubDate>Fri, 10 Aug 2007 03:34:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.elbeno.com/haskell_soe_blog/?p=52</guid>
		<description><![CDATA[map &#40;&#40;/2&#41;.&#40;+1&#41;&#41; xs]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">/</span><span style="color: red;">2</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> xs</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.elbeno.com/haskell_soe_blog/?feed=rss2&amp;p=52</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 9.9</title>
		<link>http://www.elbeno.com/haskell_soe_blog/?p=51</link>
		<comments>http://www.elbeno.com/haskell_soe_blog/?p=51#comments</comments>
		<pubDate>Thu, 09 Aug 2007 06:52:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.elbeno.com/haskell_soe_blog/?p=51</guid>
		<description><![CDATA[fix f = f &#40;fix f&#41; First, f takes the result of fix f, and second, the return type of f must be the same as the return type of fix. fix :: &#40;a -&#62; a&#41; -&#62; a Research shows that fix is the Fixed point combinator (or Y-combinator). Apparently this allows the definition of [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">fix f <span style="color: #339933; font-weight: bold;">=</span> f <span style="color: green;">&#40;</span>fix f<span style="color: green;">&#41;</span></pre></div></div>

<p>First, <tt>f</tt> takes the result of <tt>fix f</tt>, and second, the return type of <tt>f</tt> must be the same as the return type of <tt>fix</tt>.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">fix <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> a</pre></div></div>

<p>Research shows that <tt>fix</tt> is the Fixed point combinator (or Y-combinator). Apparently this allows the definition of anonymous recursive functions. So let&#8217;s try to make <tt>remainder</tt> into an anonymous function. A good way to start is to simply remove one of the arguments and see if we can return an anonymous function. Something tells me that <tt>a</tt> is the argument to remove, since <tt>b</tt> is used in the base case.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">remanon b <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>\f x <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> x <span style="color: #339933; font-weight: bold;">&lt;</span> b <span style="color: #06c; font-weight: bold;">then</span> x
                     <span style="color: #06c; font-weight: bold;">else</span> f <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">-</span>b<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>That&#8217;s a good start. Note here that if we try to do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#40;</span>remanon <span style="color: red;">3</span><span style="color: green;">&#41;</span> remanon</pre></div></div>

<p>Haskell says &#8220;unification would give infinite type&#8221; &#8211; which is a good sign. This is what we would like to do with the help of <tt>fix</tt>. So now if we apply <tt>fix</tt> to this, we get:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">fix <span style="color: green;">&#40;</span>remanon b<span style="color: green;">&#41;</span>
<span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>remanon b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>fix <span style="color: green;">&#40;</span>remanon b<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
<span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> x <span style="color: #339933; font-weight: bold;">&lt;</span> b <span style="color: #06c; font-weight: bold;">then</span> x
         <span style="color: #06c; font-weight: bold;">else</span> <span style="color: green;">&#40;</span>fix <span style="color: green;">&#40;</span>remanon b<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">-</span>b<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>Which looks good. So we need to fix <tt>remanon</tt> and apply it to <tt>a</tt>.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">remainder' a b <span style="color: #339933; font-weight: bold;">=</span> fix <span style="color: green;">&#40;</span>remanon b<span style="color: green;">&#41;</span> a</pre></div></div>

<p>And that works.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elbeno.com/haskell_soe_blog/?feed=rss2&amp;p=51</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Exercise 9.8</title>
		<link>http://www.elbeno.com/haskell_soe_blog/?p=50</link>
		<comments>http://www.elbeno.com/haskell_soe_blog/?p=50#comments</comments>
		<pubDate>Thu, 09 Aug 2007 06:18:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.elbeno.com/haskell_soe_blog/?p=50</guid>
		<description><![CDATA[power :: &#40;a -&#62; a&#41; -&#62; Int -&#62; a -&#62; a power f 0 = &#40;\x -&#62; x&#41; power f n = &#40;\x -&#62; f &#40;power f &#40;n-1&#41; x&#41;&#41; Since the function is called power, there is one obvious application that springs to mind, i.e. raising to a power: raise :: Int -&#62; Int -&#62; [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">power <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> a
power f <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> x<span style="color: green;">&#41;</span>
power f n <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> f <span style="color: green;">&#40;</span>power f <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>Since the function is called <tt>power</tt>, there is one obvious application that springs to mind, i.e. raising to a power:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">raise <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Int</span>
raise x y <span style="color: #339933; font-weight: bold;">=</span> power <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span>x<span style="color: green;">&#41;</span> y <span style="color: red;">1</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.elbeno.com/haskell_soe_blog/?feed=rss2&amp;p=50</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.431 seconds -->
