<?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>Ryan Rampersad &#187; Scheme</title>
	<atom:link href="http://blog.ryanrampersad.com/category/scheme/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ryanrampersad.com</link>
	<description>Thoughts, Ideas &#38; Opinions</description>
	<lastBuildDate>Thu, 17 May 2012 17:11:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-beta4-20825</generator>
		<item>
		<title>★ scheme: fold</title>
		<link>http://blog.ryanrampersad.com/2012/03/11/scheme-fold/</link>
		<comments>http://blog.ryanrampersad.com/2012/03/11/scheme-fold/#comments</comments>
		<pubDate>Sun, 11 Mar 2012 23:23:29 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[fold]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://blog.ryanrampersad.com/?p=4781</guid>
		<description><![CDATA[Let&#8217;s say you have a list of points that are put together with cons. You can car to get the x-value and cdr for the y-value. Now let&#8217;s say you need to add the values up, for instance, all the x-values. You can do it really easily with a neat function that&#8217;s included in Scheme. [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you have a list of points that are put together with <code>cons</code>. You can <code>car</code> to get the x-value and <code>cdr</code> for the y-value.</p>
<p>Now let&#8217;s say you need to add the values up, for instance, all the x-values. You can do it really easily with a <em>neat</em> function that&#8217;s included in Scheme. It&#8217;s called <code>fold</code>.</p>
<p>The <a href="http://www.gnu.org/software/guile/manual/html_node/SRFI_002d1-Fold-and-Map.html">documentation on fold</a> allows you to to chain a procedure such that once run, it will do it again with the return value of the previous run as the next iteration&#8217;s input. That sounds confusing but this example demonstrates it quite well.</p>
<pre class="brush: plain; title: ; notranslate">
(define (sum-x-values lst)
  (fold (lambda (p b) (+ (car p) b)) 0 lst)
)
</pre>
<p>What does this do? The <code>p</code> is the value from the list you&#8217;re working with. So in this case it would be a <code>cons</code> pair of x-y values. The <code>b</code> value is the <em>return</em> value from the previous run. The <code>car</code> will pull the x-value from the object, and if you wanted y-values, you can do <code>car</code> on p. Then it&#8217;s summed up and returned until it&#8217;s done. The <code>0</code> is the initial value the first sum is added to &#8211; so that&#8217;s pretty important.</p>
<p>This is a great way to compact down an annoying series of iterators and recursions.</p>
<p>And a thanks to <a href="http://twitter.com/samebertz">Sam</a> for pointing this method out.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryanrampersad.com/2012/03/11/scheme-fold/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>★ scheme: Inconsistency detected &#8211; MIT-Scheme Installation Mac OSX 10.7 Lion</title>
		<link>http://blog.ryanrampersad.com/2012/03/07/scheme-inconsistency-detected-mit-scheme-installation-mac-osx-10-7-lion/</link>
		<comments>http://blog.ryanrampersad.com/2012/03/07/scheme-inconsistency-detected-mit-scheme-installation-mac-osx-10-7-lion/#comments</comments>
		<pubDate>Thu, 08 Mar 2012 04:49:28 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Errors]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://blog.ryanrampersad.com/?p=4413</guid>
		<description><![CDATA[SSH&#8217;ing through my MacBook Air to Ubuntu is great and all, but nothing beats running Scheme locally and natively. When I followed the instructions scattered around the Internet on Scheme installation, I would usually end up with this error. scheme: can’t find a readable default for option –band. searched for file all.com in these directories: [...]]]></description>
			<content:encoded><![CDATA[<p>SSH&#8217;ing through my MacBook Air to Ubuntu is great and all, but nothing beats running Scheme locally and natively. When I followed <a href="http://renevanbelzen.wordpress.com/2008/11/13/installing-mitgnu-scheme-on-mac-os-x-leopard/">the instructions</a> scattered <a href="https://wiki.umn.edu/CSCI1901/InstallingMITScheme">around the Internet</a> on Scheme installation, I would usually end up with this error.</p>
<blockquote><p>scheme: can’t find a readable default for option –band.<br />
searched for file all.com in these directories:<br />
/usr/lib/mit-scheme</p>
<p>Inconsistency detected.</p></blockquote>
<p>I tried nearly ever combination of symbolic linking from the <em>Applications</em> directory into <em>usr/lib</em> and I even created the infamous <em>usr/local/lib</em> despite its original non-existence.</p>
<p>One source suggested I set the path attribute in my profile. Since I had been copying willy-nilly the symbolic links to just about every location in /bin, /usr/bin/ and usr/lib, nothing made sense. I eliminated those references to start clean and simply updated my <em>.profile</em> with the following:</p>
<blockquote><p>export MITSCHEME_LIBRARY_PATH=/Applications/mit-scheme.app/Contents/Resources</p></blockquote>
<p>That will allow the Scheme executable to find the library when it runs. But how can you have it get recognized in the terminal? You actually do need a copy of the Scheme binary in there, or at least a symbolic link. So, just <code>cd</code> into the <em>/usr/bin</em> directory and then make the symlink.</p>
<p><blockquoute>sudo ln -s /Applications/mit-scheme.app/Contents/Resources/mit-scheme scheme</p></blockquote>
<p>If you try entering <em>scheme</em> into the terminal now, you&#8217;ll either get nothing (e.g. it&#8217;s not a proper bash command) or there&#8217;s inconsistency detected. What you&#8217;ll have to do is exit completely from the terminal. The paths have to update and restarting the terminal is the easy way to do it. After the restart though, you&#8217;ll see that <em>scheme</em> should work just fine now when called from the terminal on OSX.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryanrampersad.com/2012/03/07/scheme-inconsistency-detected-mit-scheme-installation-mac-osx-10-7-lion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>★ scheme: (clear)</title>
		<link>http://blog.ryanrampersad.com/2012/02/09/scheme-clear/</link>
		<comments>http://blog.ryanrampersad.com/2012/02/09/scheme-clear/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 17:41:58 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[clear]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://blog.ryanrampersad.com/?p=4408</guid>
		<description><![CDATA[When I was using scheme in class, I found it annoying to use the Scheme interpeter, make a bunch of mistakes and have a heap of error messages clogging the top part of the window. Traditionally, if this was done in the terminal, you could clear the screen with a command. In bash, everyone knows [...]]]></description>
			<content:encoded><![CDATA[<p>When I was using scheme in class, I found it annoying to use the Scheme interpeter, make a bunch of mistakes and have a heap of error messages clogging the top part of the window. Traditionally, if this was done in the terminal, you could clear the screen with a command. In <em>bash</em>, everyone knows that <code>clear</code> clears the screen. Well, it does that in Scheme as well.</p>
<p><a href="http://blog.ryanrampersad.com/wp-content/uploads/2011/09/scheme-clear.png"><img src="http://blog.ryanrampersad.com/wp-content/uploads/2011/09/scheme-clear-580x411.png" alt="Clear the screen in scheme with (clear)" title="scheme: (clear)" width="580" height="411" class="aligncenter size-medium wp-image-4409" /></a></p>
<p>Just run <code>(clear)</code>, as it is a procedure, you need the parenthesis.</p>
<p>This works in MIT Scheme. If you see a <code>;Unspecified return value</code>, don&#8217;t worry about it. It simply means that the <code>(clear)</code> procedure returns nothing.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryanrampersad.com/2012/02/09/scheme-clear/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>★ Scheme: get-nearest+</title>
		<link>http://blog.ryanrampersad.com/2011/12/06/scheme-get-nearest/</link>
		<comments>http://blog.ryanrampersad.com/2011/12/06/scheme-get-nearest/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 19:59:57 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://blog.ryanrampersad.com/?p=4576</guid>
		<description><![CDATA[Logic often requires you to find the lowest or highest value in an array. That&#8217;s easy just about everywhere. Sometimes though, you need to get the nearest number, and that can be a slight challenge, espeically in Scheme. I wrote a little procedure called get-nearest+ that given a value, it will attempt to find the [...]]]></description>
			<content:encoded><![CDATA[<p>Logic often requires you to find the lowest or highest value in an array. That&#8217;s easy just about everywhere. Sometimes though, you need to get the nearest number, and that can be a slight challenge, espeically in Scheme.</p>
<p>I wrote a little procedure called <code>get-nearest+</code> that given a value, it will attempt to find the next largest value in that array. For example, given 24 and array of <code>10, 9, 35, 2, 29, 44</code>, this procedure will return 29, because 29 comes right after 24.</p>
<p>I needed this logic in Scheme, but it&#8217;s easy enough to translate into C-based language too.</p>
<pre class="brush: plain; title: ; notranslate">
(define (get-nearest+ value lst)
  (define ordered-lst (sort lst &lt;))
  (define (swap-value current found)
    (if (&gt; current found) current found))
  (define (swap-indicator current found)
    (if (&gt; current found) #t #f))
  (define (iterate fragment found break)
    (if (or (null? fragment) break)
      (if break
        found
        (car lst)
      )
      (iterate
        (cdr fragment)
        (swap-value (car fragment) found)
        (swap-indicator (car fragment) found)
      )
    )
  )
  (iterate ordered-lst value #f)
)
</pre>
<p>The magic of this is the early return. By setting <code>break</code> to <em>true</em> at the same time as setting the new element in <code>found</code>, we return early and even larger elements are not returned.</p>
<h3>Update</h3>
<p>In my initial testing, I used this example given 24 and array of <code>10, 9, 35, 2, 29, 44</code>. The problem with this is that if 25 comes after the 29, it would not be picked up. The early return sees to that. To remedy this, I use a <a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/private/list..rkt)._sort))">sort-function</a> to order everything. In this way, 25 will always come before 29 does.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryanrampersad.com/2011/12/06/scheme-get-nearest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>★ scheme: split a list</title>
		<link>http://blog.ryanrampersad.com/2011/11/01/scheme-split-a-list/</link>
		<comments>http://blog.ryanrampersad.com/2011/11/01/scheme-split-a-list/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 17:40:54 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[built-in]]></category>
		<category><![CDATA[procedure]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[split]]></category>

		<guid isPermaLink="false">http://blog.ryanrampersad.com/?p=4513</guid>
		<description><![CDATA[Scheme has a built in procedure for splitting lists, but usually you can&#8217;t use unlearned methods in labs or in homework yet. So this is a partial solution for splitting a list at a particular index. Here&#8217;s the algorithmic process. Add the first element from the old list to the new list Remove the first [...]]]></description>
			<content:encoded><![CDATA[<p>Scheme has a <a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/list..rkt)._split-at))">built in procedure for splitting lists</a>, but usually you can&#8217;t use <em>unlearned</em> methods in labs or in homework yet. So this is a partial solution for splitting a list at a particular index.</p>
<pre class="brush: plain; title: ; notranslate">
  ; split's a list
  ; lst - a list to be split
  ; at - a numeric index at which to split the list
  (define (split lst at)
    ; splits the list
    ; n - new list
    ; l - an old
    ; i - a counter
    (define (iter n l i)
      (if (or (null? l) (= i at))
        (cons (reverse n) l)
        (iter (cons (car l) n) (cdr l) (+ i 1))
      )
    )
    (iter () lst 0)
  )  
</pre>
<p>Here&#8217;s the algorithmic process.</p>
<ol>
<li>Add the first element from the <em>old</em> list to the <em>new</em> list</li>
<li>Remove the first element from the <em>old</em> list</li>
<li>Repeat with the <em>new</em> and <em>old</em> list</li>
<li>Until either the list is <em>null</em> or the counter as reached the designated index</li>
</ol>
<p>There are two arguments for this procedure. The first is the list itself to be split. The second is a simple integer. To get the split parts of the list, you&#8217;ll have to <code>car</code> or <code>cdr</code> the result. For example, <code>(car (split a-list 3))</code> will return the first portion of the list while a <code>(cdr (split a-list 3))</code> will return the latter half of the list.</p>
<p>Here&#8217;s some sample output.</p>
<pre class="brush: plain; title: ; notranslate">
original: (0 1 2 3 4 5 6)
split at index: 3
first: (0 1 2)
tail: (3 4 5 6)

original: (hello world how are you doing today ?)
split at index 4
first: (hello world how are)
tail: (you doing today ?)
</pre>
<p>Splitting lists in Scheme is really handy.</p>
<p class="signoff"><em>Happy splitting.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryanrampersad.com/2011/11/01/scheme-split-a-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>★ scheme: (cons) &#8211; list error &#8211; (&#8220;list&#8221; #x3) #x14</title>
		<link>http://blog.ryanrampersad.com/2011/10/27/scheme-cons-list-error-list-x3-x14/</link>
		<comments>http://blog.ryanrampersad.com/2011/10/27/scheme-cons-list-error-list-x3-x14/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 17:27:06 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Errors]]></category>
		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://blog.ryanrampersad.com/?p=4523</guid>
		<description><![CDATA[If one Scheme error wasn&#8217;t enough, I&#8217;ve got another one. This time it&#8217;s from cons. ;The procedure #[compiled-procedure 13 ("list" #x3) #x14 #x103498714] has been called with 1 argument; it requires exactly 2 arguments. My code looked something like this: In Scheme, it&#8217;s not so easy to realize you&#8217;ve missed an argument when you&#8217;re coming [...]]]></description>
			<content:encoded><![CDATA[<p>If one Scheme error wasn&#8217;t enough, I&#8217;ve got another one. This time it&#8217;s from <em>cons</em>.</p>
<blockquote><p>
;The procedure #[compiled-procedure 13 ("list" #x3) #x14 #x103498714] has been called with 1 argument; it requires exactly 2 arguments.</p></blockquote>
<p>My code looked something like this:</p>
<pre class="brush: plain; title: ; notranslate">
(define (get-last-item lst)
  (define (recurse tsl l)
    (if (null? l)
      (car tsl)
      (recurse (cons (car l)) (cdr l))
    )
  )
  (recurse () lst)
)
</pre>
<p>In Scheme, it&#8217;s not so easy to realize you&#8217;ve missed an argument when you&#8217;re coming from a language where anything inside <em>parenthesis</em> are arguments. But in this case, it&#8217;s <code>cons</code>.</p>
<p>Why? It needs two arguments, something to <code>car</code> and something to <code>cdr</code>. That recurse line should look something more like this:</p>
<pre class="brush: plain; title: ; notranslate">
      (recurse (cons (car l) tsl) (cdr l))
</pre>
<p>Now, you may ask, &#8220;how did you know it was <code>cons</code>?&#8221; Aside from looking for something where I messed up arguments (1 instead of 2), I looked at the internal names. When you see something like <strong>#x14 #x103498714</strong> in an error message, and as long as it&#8217;s a scheme procedure itself and not one of those fancy <em>special forms</em>, it will always have that representation internally. I ran <code>cons</code> point-blank in the Scheme interpreter and it returned the representation that shown in the error:</p>
<blockquote><p>#[compiled-procedure 13 ("list" #x3) #x14 #x103498714]</p></blockquote>
<p>I&#8217;m getting better at debugging Scheme code, but it&#8217;s still an uphill climb.</p>
<p class="signoff"><em>Happy cons&#8217;ing</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryanrampersad.com/2011/10/27/scheme-cons-list-error-list-x3-x14/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>★ scheme: (list-ref) (&#8220;list&#8221; #x26) error</title>
		<link>http://blog.ryanrampersad.com/2011/10/26/scheme-list-ref-list-x26-error/</link>
		<comments>http://blog.ryanrampersad.com/2011/10/26/scheme-list-ref-list-x26-error/#comments</comments>
		<pubDate>Wed, 26 Oct 2011 17:20:40 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Errors]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[lists]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://blog.ryanrampersad.com/?p=4510</guid>
		<description><![CDATA[Coding in scheme requires a different mindset with not only composing backwards expressions but also with debugging errors. Today, I was coding a simple sorting algorithm for a list. I was reciving an error message that mentioned list while there weren&#8217;t any direct list calls. ;The procedure #[compiled-procedure 13 ("list" #x26) #x1a #x10349b102] has been [...]]]></description>
			<content:encoded><![CDATA[<p>Coding in scheme requires a different mindset with not only composing backwards expressions but also with debugging errors. Today, I was coding a simple sorting algorithm for a list. I was reciving an error message that mentioned <em>list</em> while there weren&#8217;t any direct list calls.</p>
<blockquote><p>;The procedure #[compiled-procedure 13 ("list" #x26) #x1a #x10349b102] has been called with 1 argument; it requires exactly 2 arguments.</p></blockquote>
<p>One thing to notice: <em>called with 1 argument; it requires exactly 2 arguments.</em> That is a give away that you&#8217;ve missed argument somewhere, even if the code isn&#8217;t what is mentioned previously, as in this case, <em>list</em>.</p>
<pre class="brush: plain; title: ; notranslate">
  (cond
    ((&lt; (distance (list-ref (- (length pt-list) 1) ) origin) delta-distance)
      (append pt-list p-list)
    )
    ((&gt; (distance (list-ref 0) origin) delta-distance)
      (append p-list pt-list)
    )
    (else
      (append (car parts) p-list (cdr parts))  
    )
  )
</pre>
<p>Looking through that code, it isn&#8217;t really obvious what&#8217;s wrong. Distance takes two &#8220;points&#8221; and calculates the distance between them. Origin is point (0,0) and delta distance is previously calculated. The <code>pt-list</code> was a list of points to sort through and the <code>p-list</code> was a list-ized singular point for insertion. Confused yet? I was too.</p>
<p>Look at the innocent <code>list-ref</code> though? How many arguments does that have? <a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list-ref))">The documentation</a> says it has <em>two arguments</em>. I realized I missed the <em>list</em> argument in the call to <code>list-ref</code>.</p>
<p class="signoff"><em>Happy list-ref&#8217;ing.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryanrampersad.com/2011/10/26/scheme-list-ref-list-x26-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>★ scheme: Ill-formed special form</title>
		<link>http://blog.ryanrampersad.com/2011/10/05/scheme-ill-formed-special-form/</link>
		<comments>http://blog.ryanrampersad.com/2011/10/05/scheme-ill-formed-special-form/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 17:12:48 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Errors]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[special]]></category>

		<guid isPermaLink="false">http://blog.ryanrampersad.com/?p=4419</guid>
		<description><![CDATA[Scheme is notorious for returning unintelligible error messages. For instance, I was working on an iteration and recursion lab this morning and I ran into this oddity and the code that caused it. ;Ill-formed special form: (define sum-range a b count) The error message mentions special form. If you&#8217;re learning Scheme, you should remember that [...]]]></description>
			<content:encoded><![CDATA[<p>Scheme is notorious for returning unintelligible error messages. For instance, I was working on an iteration and recursion lab this morning and I ran into this oddity and the code that caused it.</p>
<blockquote><p>;Ill-formed special form: (define sum-range a b count)</p></blockquote>
<pre class="brush: plain; title: ; notranslate">
(define sum-range a b count)
 ; no procedure code yet
)
</pre>
<p>The error message mentions <em>special form</em>. If you&#8217;re learning Scheme, you should remember that <code>define</code> is a special form. It&#8217;s special in that it does not use the <em>applicative</em> ordering method for evaluation, among other things I suspect. So, somehow, the special form for <code>define</code> is incorrect. Any ideas? Can you spot the <em>syntax error</em>?</p>
<p>Notice that <code>sum-range</code> does not have a leading parenthesis? The procedure needs to be lead by a parenthesis so that it is its own expression (in terms of the special-formy-ness that is <code>define</code>). In short, the code should have read like so:</p>
<pre class="brush: plain; title: ; notranslate">
(define (sum-range a b count)
 ; no procedure code yet
)
</pre>
<p>It was really as easy as that.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryanrampersad.com/2011/10/05/scheme-ill-formed-special-form/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>★ scheme: more on nested procedures</title>
		<link>http://blog.ryanrampersad.com/2011/09/30/scheme-more-on-nested-procedures/</link>
		<comments>http://blog.ryanrampersad.com/2011/09/30/scheme-more-on-nested-procedures/#comments</comments>
		<pubDate>Fri, 30 Sep 2011 17:10:57 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[abstraction]]></category>
		<category><![CDATA[nested]]></category>
		<category><![CDATA[procedures]]></category>
		<category><![CDATA[readability]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://blog.ryanrampersad.com/?p=4400</guid>
		<description><![CDATA[A few days ago, I offered an primarily technical example on why Scheme&#8217;s nested procedure are actually pretty great. While it&#8217;s true the technical application applies, and probably more so in the more difficult exercises, I wanted to discuss the organizational methodology too. Let&#8217;s say you write code like this. It&#8217;s code for tax bracket [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago, I offered an primarily <a href="http://blog.ryanrampersad.com/2011/09/26/scheme-nested-procedures/">technical example</a> on why Scheme&#8217;s nested procedure are actually pretty great. While it&#8217;s true the technical application applies, and probably more so in the more difficult exercises, I wanted to discuss the organizational methodology too.</p>
<p>Let&#8217;s say you write code like this. It&#8217;s code for tax bracket eerily similar to real life. For our purposes though, it looks like a complete and utter mess. </p>
<pre class="brush: plain; title: ; notranslate">
(define (tax amount)
	(define total
		(cond 
			((&lt;= amount 35000) (* amount .15))
			((and (&gt; amount 35000) (&lt;= amount 100000)) (+ (* 35000 .15) (* (- amount 35000 ) .25 ) ) )
			((&gt; amount 100000) (+ (* 35000 .15) (* 65000 .25 ) (* .35 (- amount 100000) ) ) )
		)
	)
	(if (&gt; amount 50000) (+ total (* .05 (- amount 50000))) total )
)
</pre>
<p>This example goes like this: define the total as the result of the <code>cond</code>, where the <code>cond</code> will decide which bracket to use. Conceptually, it&#8217;s not really important to know how the tax is calculated to discuss organization. Plainly, this code is repetitive and obviously busy.</p>
<p>Using nested procedures though, we can clean it up. Since there are three brackets, let&#8217;s make three nested procedures. Granted, this may not look any <em> simpler</em>, but it is. That&#8217;s because the logic driving the calculations are abstracted away from the conditionals from which they originate. Indeed, this would definitely be better with some <em>variables</em>, but you can&#8217;t have everything.</p>
<pre class="brush: plain; title: ; notranslate">
(define (tax2 amount)
  (define (bracket1)
    (* amount .15)
  )
  (define (bracket2)
    (+ (* 35000 .15) (* (- amount 35000 ) .25 ) )
  )
  (define (bracket3)
    (+ (* 35000 .15) (* 65000 .25 ) (* .35 (- amount 100000) ) )
  )
  
  (define total
    (cond 
      ((&lt;= amount 35000) (bracket1) )
      ((and (&gt; amount 35000) (&lt;= amount 100000)) (bracket2) )
      ((&gt; amount 100000) (bracket3) )
    )
  )

 (if (&gt; amount 50000) (+ total (* .05 (- amount 50000))) total ) 
)
</pre>
<p>Notice that last if-statement though? It still has some messy junk in there that&#8217;s still doing calculations. You could easily trim part of that down. But there&#8217;s a problem with trimming it down completely. Notice its use of <code>total</code>? It was previously defined as the value returned from the <code>cond</code> above it. If I were to refer to it in a previously defined procedure-within-a-procedure, possibly called <code>state-bracket</code>, I would get an error stating that <code>total</code> doesn&#8217;t exist yet, and that&#8217;s true, it doesn&#8217;t. What could I do? I could pass it in as an argument!</p>
<p>Notice the change in the last if-statement and the addition of a new nested procedure.</p>
<pre class="brush: plain; title: ; notranslate">
(define (tax3 amount)
  (define (bracket1)
    (* amount .15)
  )
  (define (bracket2)
    (+ (* 35000 .15) (* (- amount 35000 ) .25 ) )
  )
  (define (bracket3)
    (+ (* 35000 .15) (* 65000 .25 ) (* .35 (- amount 100000) ) )
  )
  (define (state-bracket)
    (+ total (* .05 (- amount 50000)))
  )
  
  (define total
    (cond 
      ((&lt;= amount 35000) (bracket1) )
      ((and (&gt; amount 35000) (&lt;= amount 100000)) (bracket2) )
      ((&gt; amount 100000) (bracket3) )
    )
  )

 (if (&gt; amount 50000) (state-bracket total) total )

)
</pre>
<p>This code is abstracted more than it was originally. That means it&#8217;s more flexible and likely easier to read at a glance. I will admit that though that are improvements to be made. For instance, the concurrent repeating of constants could be eliminated. Personally, I feel setting a define up so that it has a conditional value is a hack, but <a href="http://blog.ryanrampersad.com/2011/09/22/scheme-cant-bind-name-in-null-syntactic-environment/" title="scheme: can’t bind name in null syntactic environment">apparently that&#8217;s the best way</a>.</p>
<p>And that&#8217;s all about logical organization and abstraction with the nested procedures Scheme offers.</p>
<p class="signoff"><em>Happy abstracting!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryanrampersad.com/2011/09/30/scheme-more-on-nested-procedures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>★ scheme: factorial infinite recursion</title>
		<link>http://blog.ryanrampersad.com/2011/09/29/scheme-factorial-infinite-recursion/</link>
		<comments>http://blog.ryanrampersad.com/2011/09/29/scheme-factorial-infinite-recursion/#comments</comments>
		<pubDate>Thu, 29 Sep 2011 12:51:39 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Errors]]></category>
		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://blog.ryanrampersad.com/?p=4422</guid>
		<description><![CDATA[My textbook explains basic recursion with the age old factorial. The problem with that is that the code given might be a tiny bit buggy if the expectations aren&#8217;t kept on the straight and narrow. The given code from the book is: I left this code as is. Factorials are easy to understand, pick any [...]]]></description>
			<content:encoded><![CDATA[<p>My <a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-4.html#%_toc_%_sec_1.2.1">textbook explains basic recursion</a> with the age old <em>factorial</em>. The problem with that is that the code given might be a tiny bit buggy if the expectations aren&#8217;t kept on the straight and narrow.</p>
<p>The given code from the book is:</p>
<pre class="brush: plain; title: ; notranslate">
(define (factorial n)
  (if (= n 1)
      1
      (* n (factorial (- n 1)))))
</pre>
<p>I left this code as is. Factorials are easy to understand, pick any <em>n-value</em> and multiply each value below it until you get to 1.</p>
<p>Imagine you made a call to <code>(factorial 2)</code>. The result will be <em>2</em> because of <em>1 * 2</em>. Then imagine <code>(factorial 1)</code>, and the result being <em>1</em> because the truthy-branch of the if-statement is used instead of the calculation branch. So far, so good, right?</p>
<p>Now, imagine you had a call to <code>(factorial 0)</code>. What happens? You&#8217;re going to get a dreaded infinite recursion error, which will look similar to this:</p>
<blockquote><p>;Aborting!: maximum recursion depth exceeded</p></blockquote>
<p>What could cause this behavior? Perhaps the author of this method simply really trusted himself to not miscount in his recursion. Perhaps it was an oversight, but whatever the case, look at the condition that controls the procedure. Notice <code>if (= n 1)</code>, and that there is only <em>a single value</em> that will make this procedure <em>stop</em>, and that is 1.</p>
<p>To fix this to accept 0, while not affecting the result of the factorial numerically, you can simply exchange the <code>(= n 1)</code> with <code>(&lt; n 1)</code>. This will force a 1 whenever n is less than 1, so that covers 0 and all negative values.</p>
<p class="signoff"><em>Happy factorial!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryanrampersad.com/2011/09/29/scheme-factorial-infinite-recursion/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

