<?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"
	>

<channel>
	<title>Overflow: Auto</title>
	<atom:link href="http://www.tylerprete.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tylerprete.com</link>
	<description>Straight from the mind of Tyler Prete</description>
	<pubDate>Mon, 07 Apr 2008 17:35:33 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Fast Iterative List Subsequences in Java</title>
		<link>http://www.tylerprete.com/2008/04/06/fast-iterative-list-subsequences-in-java/</link>
		<comments>http://www.tylerprete.com/2008/04/06/fast-iterative-list-subsequences-in-java/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 05:45:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[List]]></category>

		<guid isPermaLink="false">http://www.tylerprete.com/2008/04/06/fast-iterative-list-subsequences-in-java/</guid>
		<description><![CDATA[Today on Ray Myers Blog, (cadr life), he posted about returning lists of n-sized subsequences of a larger list.  He used Java and tried to implement the recursive solution that he would normally write in Lisp or Haskell. He also issued a challenge to write this function without recursion.  Well, my implementation is [...]]]></description>
			<content:encoded><![CDATA[<p>Today on <a href="http://cadrlife.blogspot.com/2008/03/returning-considered-difficult.html" title="(cadr life)">Ray Myers Blog, (cadr life)</a>, he posted about returning lists of n-sized subsequences of a larger list.  He used Java and tried to implement the recursive solution that he would normally write in Lisp or Haskell. He also issued a challenge to write this function without recursion.  Well, my implementation is certainly not clean, but it is iterative, and is significantly faster for large lists (up to 10x).   You can download my Java source file (with a main setup to time my version vs. his recursive version) <a href="http://www.tylerprete.com/files/SubSequence.java" title="SubSequence.java">here.</a></p>
<p><span id="more-7"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> List<span style="color: #339933;">&lt;</span>List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;&gt;</span> mysubn<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> n, List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> li<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		List<span style="color: #339933;">&lt;</span>List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;&gt;</span> ret <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> size <span style="color: #339933;">=</span> li.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			ret.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> ret<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>li.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> ret<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>size <span style="color: #339933;">&lt;</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> ret<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>size <span style="color: #339933;">==</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			ret.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>li<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> ret<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">/* I use counters to actually keep track of where I am in the list,
		 * but iterators to actually get the elements.  This is because we can't
		 * assume what type of list we are accessing.  list.get(n) is O(1) for
		 * ArrayList, but O(n) for LinkedList, so we'd like to minimize these
		 * as much as possible.
		 * The reason we need to keep the counters and ending array, 
		 * instead of going until the iterators run out of elements, 
		 * is we only want them to the point where they could still make a
		 * subsequence.
		 */</span>
		ArrayList<span style="color: #339933;">&lt;</span>ListIterator<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;&gt;</span> iters <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>ListIterator<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ArrayList<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> currElems <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> counters <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> endings <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Set up our initial values</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			iters.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>li.<span style="color: #006633;">listIterator</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			currElems.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>iters.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			counters<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
			endings<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> size <span style="color: #339933;">-</span> n <span style="color: #339933;">+</span> i<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">// Go until the we don't have enough elements left to make a subsequence</span>
		<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>counters<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> endings<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> sub <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				sub.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>currElems.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			ret.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>sub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">int</span> c <span style="color: #339933;">=</span> n <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// Here we figure out how many of the counters (indexes) need updating.</span>
			<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>c <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>counters<span style="color: #009900;">&#91;</span>c<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> endings<span style="color: #009900;">&#91;</span>c<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">else</span>
					c<span style="color: #339933;">--;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #666666; font-style: italic;">// Update the left-most counter (index)</span>
			counters<span style="color: #009900;">&#91;</span>c<span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>iters.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				currElems.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span>c, iters.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			c<span style="color: #339933;">++;</span>
			<span style="color: #666666; font-style: italic;">// Starting from the next left-most counter (if there is one),</span>
			<span style="color: #666666; font-style: italic;">// set counter to be 1 more than previous counter, and reset</span>
			<span style="color: #666666; font-style: italic;">// the iterator to start there as well.</span>
			<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>c <span style="color: #339933;">&lt;</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// Reset the counter, and reset the iterator</span>
				<span style="color: #666666; font-style: italic;">// to the new starting position.</span>
				counters<span style="color: #009900;">&#91;</span>c<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> counters<span style="color: #009900;">&#91;</span>c<span style="color: #cc66cc;">-1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
				iters.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span>c, li.<span style="color: #006633;">listIterator</span><span style="color: #009900;">&#40;</span>counters<span style="color: #009900;">&#91;</span>c<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// We make sure the iterator has another element.</span>
				<span style="color: #666666; font-style: italic;">// The only time this will return false is when we are completely done</span>
				<span style="color: #666666; font-style: italic;">// and the outer while is over.</span>
				<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>iters.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
					currElems.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span>c, iters.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				c<span style="color: #339933;">++;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> ret<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.tylerprete.com/2008/04/06/fast-iterative-list-subsequences-in-java/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Problem: Spam-Egg List</title>
		<link>http://www.tylerprete.com/2007/07/12/problem-spam-egg-list/</link>
		<comments>http://www.tylerprete.com/2007/07/12/problem-spam-egg-list/#comments</comments>
		<pubDate>Thu, 12 Jul 2007 16:08:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://www.tylerprete.com/2007/07/12/problem-spam-egg-list/</guid>
		<description><![CDATA[So on Stoto&#8217;s Weblog Tuesday there was a little programming challenge called the Spam-Egg List Problem.
Here is the problem:
Write a function called dumpList that takes as its parameters a string and a reference to an arbitrarily complex nested list and prints the value of each list element on a separate line. The value on each [...]]]></description>
			<content:encoded><![CDATA[<p>So on <a href="http://jroller.com/page/sto" title="Stoto's Weblog">Stoto&#8217;s Weblog</a> Tuesday there was a little programming challenge called the <a href="http://jroller.com/page/sto?entry=nested_list_question" title="Spam-Egg List Problem">Spam-Egg List Problem.</a></p>
<p>Here is the problem:<br />
Write a function called dumpList that takes as its parameters a string and a reference to an arbitrarily complex nested list and prints the value of each list element on a separate line. The value on each line should be preceded by the string and numbers indicating the depth and index of the element in the list. Assume that the list contains only strings and other nested lists.</p>
<p>For example, given the following nested list:<br />
list = [ 'a string', [ 'a', 'b', 'c' ], &#8217;spam&#8217;, [ 'eggs' ] ] and the string &#8216;Foo&#8217;, the output of dumpList(&#8217;Foo&#8217;, list) would look like:</p>
<p>Foo.0: a string<br />
Foo.1.0: a<br />
Foo.1.1: b<br />
Foo.1.2: c<br />
Foo.2: spam<br />
Foo.3.0: eggs<br />
<span id="more-6"></span><br />
My solution (in scheme) is as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="scheme"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>dump<span style="color: #66cc66;">-</span><span style="color: #b1b100;">list</span> word <span style="color: #b1b100;">sequence</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>dump<span style="color: #66cc66;">-</span>list<span style="color: #66cc66;">-</span>iter word <span style="color: #b1b100;">sequence</span> count<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> <span style="color: #b1b100;">sequence</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>? <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #b1b100;">sequence</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
                <span style="color: #66cc66;">&#40;</span>dump<span style="color: #66cc66;">-</span>list<span style="color: #66cc66;">-</span>iter
&nbsp;
                 <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">-</span><span style="color: #b1b100;">append</span>
&nbsp;
                  word <span style="color: #ff0000;">&quot;.&quot;</span> <span style="color: #66cc66;">&#40;</span>number<span style="color: #66cc66;">-&gt;</span>string count<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #b1b100;">sequence</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
                <span style="color: #66cc66;">&#40;</span>dump<span style="color: #66cc66;">-</span>list<span style="color: #66cc66;">-</span>iter word <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> <span style="color: #b1b100;">sequence</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> count <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
               <span style="color: #66cc66;">&#40;</span>#t
&nbsp;
                <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">display</span>
&nbsp;
                 <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">-</span><span style="color: #b1b100;">append</span> word <span style="color: #ff0000;">&quot;.&quot;</span> <span style="color: #66cc66;">&#40;</span>number<span style="color: #66cc66;">-&gt;</span>string count<span style="color: #66cc66;">&#41;</span>
&nbsp;
                                <span style="color: #ff0000;">&quot;: &quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #b1b100;">sequence</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
                <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">newline</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
                <span style="color: #66cc66;">&#40;</span>dump<span style="color: #66cc66;">-</span>list<span style="color: #66cc66;">-</span>iter word <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> <span style="color: #b1b100;">sequence</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> count <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span>dump<span style="color: #66cc66;">-</span>list<span style="color: #66cc66;">-</span>iter word <span style="color: #b1b100;">sequence</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>It&#8217;s a simple recursive solution with side-effects (printing).  It would be relatively easy to rework it to maintain a list of strings and print them at the very end, but the problem didn&#8217;t call for it, so I&#8217;m satisfied with this solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tylerprete.com/2007/07/12/problem-spam-egg-list/feed/</wfw:commentRss>
		</item>
		<item>
		<title>XKCD #287 Comic Solution</title>
		<link>http://www.tylerprete.com/2007/07/09/xkcd-comic-solution/</link>
		<comments>http://www.tylerprete.com/2007/07/09/xkcd-comic-solution/#comments</comments>
		<pubDate>Mon, 09 Jul 2007 22:18:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://www.tylerprete.com/2007/07/09/xkcd-comic-solution/</guid>
		<description><![CDATA[In today&#8217;s xkcd comic the joke was about using the knapsack problem at a restaurant.  Just for fun, I solved it in scheme.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
&#40;define &#40;make-appetizer name cost&#41;
  &#40;cons name cost&#41;&#41;
&#40;define &#40;name app&#41;
  &#40;car app&#41;&#41;
&#40;define &#40;cost app&#41;
  &#40;cdr app&#41;&#41;
&#160;
&#40;define delta 0.000001&#41;
&#40;define &#40;float-equal? x y&#41;
  &#40;&#60; &#40;abs &#40;- x y&#41;&#41; delta&#41;&#41;
&#160;
&#40;define appetizers
  [...]]]></description>
			<content:encoded><![CDATA[<p>In today&#8217;s <a href="http://xkcd.com/c287.html">xkcd comic</a> the joke was about using the knapsack problem at a restaurant.  Just for fun, I solved it in scheme.</p>
<p><span id="more-5"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="scheme"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>appetizer name cost<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> name cost<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>name app<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> app<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>cost app<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> app<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> delta <span style="color: #cc66cc;">0.000001</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>float<span style="color: #66cc66;">-</span><span style="color: #b1b100;">equal?</span> x y<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">abs</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-</span> x y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> delta<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> appetizers
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>
   <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>appetizer <span style="color: #ff0000;">&quot;mixed fruit&quot;</span> <span style="color: #cc66cc;">2.15</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>appetizer <span style="color: #ff0000;">&quot;french fries&quot;</span> <span style="color: #cc66cc;">2.75</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>appetizer <span style="color: #ff0000;">&quot;side salad&quot;</span> <span style="color: #cc66cc;">3.35</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>appetizer <span style="color: #ff0000;">&quot;hot wings&quot;</span> <span style="color: #cc66cc;">3.55</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>appetizer <span style="color: #ff0000;">&quot;mozzarella sticks&quot;</span> <span style="color: #cc66cc;">4.20</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>appetizer <span style="color: #ff0000;">&quot;sampler plate&quot;</span> <span style="color: #cc66cc;">5.80</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>append<span style="color: #66cc66;">-</span>unless<span style="color: #66cc66;">-</span>null set elem<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> set<span style="color: #66cc66;">&#41;</span>
      '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> elem set<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>appetizers<span style="color: #66cc66;">-</span>for<span style="color: #66cc66;">-</span>cost<span style="color: #66cc66;">-</span>n apps n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> apps<span style="color: #66cc66;">&#41;</span>
      '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>float<span style="color: #66cc66;">-</span><span style="color: #b1b100;">equal?</span> <span style="color: #66cc66;">&#40;</span>cost <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> apps<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> apps<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>cost <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> apps<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>appetizers<span style="color: #66cc66;">-</span>for<span style="color: #66cc66;">-</span>cost<span style="color: #66cc66;">-</span>n <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> apps<span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">else</span>
             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">append</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> apps<span style="color: #66cc66;">&#41;</span> x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                   <span style="color: #66cc66;">&#40;</span>appetizers<span style="color: #66cc66;">-</span>for<span style="color: #66cc66;">-</span>cost<span style="color: #66cc66;">-</span>n apps <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-</span> n <span style="color: #66cc66;">&#40;</span>cost <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> apps<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span>appetizers<span style="color: #66cc66;">-</span>for<span style="color: #66cc66;">-</span>cost<span style="color: #66cc66;">-</span>n <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> apps<span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Calling it with: (appetizers-for-cost-n appetizers 15.05)<br />
Results in:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mixed fruit&quot;</span> . <span style="color: #cc66cc;">2.15</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mixed fruit&quot;</span> . <span style="color: #cc66cc;">2.15</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mixed fruit&quot;</span> . <span style="color: #cc66cc;">2.15</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mixed fruit&quot;</span> . <span style="color: #cc66cc;">2.15</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mixed fruit&quot;</span> . <span style="color: #cc66cc;">2.15</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mixed fruit&quot;</span> . <span style="color: #cc66cc;">2.15</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;mixed fruit&quot;</span>
  .
  <span style="color: #cc66cc;">2.15</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mixed fruit&quot;</span> . <span style="color: #cc66cc;">2.15</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;hot wings&quot;</span> . <span style="color: #cc66cc;">3.55</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;hot wings&quot;</span> . <span style="color: #cc66cc;">3.55</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;sampler plate&quot;</span>
  .
  <span style="color: #cc66cc;">5.8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.tylerprete.com/2007/07/09/xkcd-comic-solution/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Postfix to expression-tree in scheme</title>
		<link>http://www.tylerprete.com/2007/07/02/postfix-to-expression-tree-in-scheme/</link>
		<comments>http://www.tylerprete.com/2007/07/02/postfix-to-expression-tree-in-scheme/#comments</comments>
		<pubDate>Mon, 02 Jul 2007 16:33:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://www.tylerprete.com/2007/07/02/postfix-to-expression-treee-in-scheme/</guid>
		<description><![CDATA[So today someone on a mailing list asked for help with a postfix to expression-tree converter.  While he hasn&#8217;t responded to me yet, I thought I&#8217;d share my solution on here.  It was surprisingly easy to write in Scheme.  Please feel free to comment if you have suggestions.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
; define a tree structure [...]]]></description>
			<content:encoded><![CDATA[<p>So today someone on a mailing list asked for help with a postfix to expression-tree converter.  While he hasn&#8217;t responded to me yet, I thought I&#8217;d share my solution on here.  It was surprisingly easy to write in Scheme.  Please feel free to comment if you have suggestions.<br />
<span id="more-4"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="scheme"><span style="color: #808080; font-style: italic;">; define a tree structure to work with</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>tree val left right<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> val left right<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>value tree<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> tree<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>left<span style="color: #66cc66;">-</span>branch tree<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cadr</span> tree<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>right<span style="color: #66cc66;">-</span>branch tree<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">caddr</span> tree<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">; converts postfix list to an expression-tree</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>postfix<span style="color: #66cc66;">-</span>to<span style="color: #66cc66;">-</span>tree ops<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>postfix<span style="color: #66cc66;">-</span>iter symbols values<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;progress through symbols keeping a list of values</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> symbols<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> values<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; returning end result</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>x <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> symbols<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">number?</span> x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>postfix<span style="color: #66cc66;">-</span>iter <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> symbols<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> x values<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#40;</span>postfix<span style="color: #66cc66;">-</span>iter
                       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> symbols<span style="color: #66cc66;">&#41;</span>
                       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>tree x <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cadr</span> values<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> values<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                             <span style="color: #66cc66;">&#40;</span>cddr values<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>postfix<span style="color: #66cc66;">-</span>iter ops '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;(postfix-to-tree '(2 1 + 3 4 * +)) == (+ (* 4 3) (+ 1 2))</span></pre></td></tr></table></div>

<p>Also, suppose rather than making an expression tree, we wanted to evaluate the postfix instead?<br />
We could just eval the output of postfix-to-tree, i.e. (eval (postfix-to-tree &#8216;(2 1 + 3 4 * +))), but in general I think it&#8217;s preferred to avoid eval.<br />
So alternatively, we&#8217;ll write a function to evaluate the symbols, and make a minor change to our main function and we&#8217;re there.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="scheme"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>eval<span style="color: #66cc66;">-</span>op op a b<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eq?</span> '<span style="color: #66cc66;">+</span> op<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> a b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eq?</span> '<span style="color: #66cc66;">-</span> op<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-</span> a b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eq?</span> '<span style="color: #66cc66;">*</span> op<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span> a b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eq?</span> '<span style="color: #66cc66;">/</span> op<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">/</span> a b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">; evaluates postfix-expression</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>eval<span style="color: #66cc66;">-</span>postfix ops<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>postfix<span style="color: #66cc66;">-</span>iter symbols values<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;progress through symbols keeping a list of values</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> symbols<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> values<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; returning end result</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>x <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> symbols<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">number?</span> x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>postfix<span style="color: #66cc66;">-</span>iter <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> symbols<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> x values<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#40;</span>postfix<span style="color: #66cc66;">-</span>iter
                       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> symbols<span style="color: #66cc66;">&#41;</span>
                       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span>eval<span style="color: #66cc66;">-</span>op x <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cadr</span> values<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> values<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                             <span style="color: #66cc66;">&#40;</span>cddr values<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>postfix<span style="color: #66cc66;">-</span>iter ops '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">; (eval-postfix '(2 1 + 3 4 * +)) == 15</span></pre></td></tr></table></div>

<p>These solutions could probably use a little error-checking, but they both operate correctly on valid input and get the job done.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tylerprete.com/2007/07/02/postfix-to-expression-tree-in-scheme/feed/</wfw:commentRss>
		</item>
		<item>
		<title>I&#8217;d like to welcome you with a little logic puzzle</title>
		<link>http://www.tylerprete.com/2007/07/01/id-like-to-welcome-you-with-a-little-logic-puzzle/</link>
		<comments>http://www.tylerprete.com/2007/07/01/id-like-to-welcome-you-with-a-little-logic-puzzle/#comments</comments>
		<pubDate>Sun, 01 Jul 2007 21:03:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Logic]]></category>

		<guid isPermaLink="false">http://www.tylerprete.com/2007/07/01/id-like-to-welcome-you-with-a-little-logic-puzzle/</guid>
		<description><![CDATA[So I'm trying to finally get around to starting and maintaining a blog.  We'll see how it works out this time.  As a little welcome for ALL my many valued readers, I'll present a logic puzzle that has supposedly been used by Microsoft for programming interviews.

 You have a 3-quart bucket, a 5-quart bucket, and an infinite
"supply of water. How can you measure out exactly 4 quarts?]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m trying to finally get around to starting and maintaining a blog.  We&#8217;ll see how it works out this time.  As a little welcome for ALL my many valued readers, I&#8217;ll present a logic puzzle that has supposedly been used by Microsoft for programming interviews.</p>
<p><strong><strong>You have a 3-quart bucket, a 5-quart bucket, and an infinite<br />
&#8220;supply of water. How can you measure out exactly 4 quarts?</strong></strong><br />
<span id="more-3"></span></p>
<p>My solution is as follows:</p>
<p>First, place the 3-quart bucket upside-down inside the 5-quart bucket.  Fill up the 5-quart bucket, then remove the 3-quart bucket.  This will leave 2 quarts inside the 5-quart bucket.  Pour this into the 3-quart bucket.  So there are now 2 quarts in the 3 quart bucket, and none in the 5-quart bucket.  Place the 5-quart bucket over the 3-quart bucket so the bottom of the 5-quart bucket is covering the rim of the 3-quart bucket, then turn them over, so the 5-quart bucket is facing upwards.  Fill the outside of the 5-quart bucket,  then remove the 3-quart bucket.  The 2-quarts that were contained in the 3-quart bucket will mix with the 2-quarts surrounding the outside of the 3-quart bucket, and you&#8217;ll be left with 4 quarts in the 5-quart bucket.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tylerprete.com/2007/07/01/id-like-to-welcome-you-with-a-little-logic-puzzle/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
