<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Another tiny interpreter for a stack-based language&#8230; this time in OCaml</title>
	<atom:link href="http://4.flowsnake.org/archives/303/feed" rel="self" type="application/rss+xml" />
	<link>http://4.flowsnake.org/archives/303</link>
	<description>A Pythoneer's adventures with Scheme, Clojure and a whole lot more. ^_^</description>
	<lastBuildDate>Sat, 22 May 2010 22:09:30 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: lasts</title>
		<link>http://4.flowsnake.org/archives/303/comment-page-1#comment-421</link>
		<dc:creator>lasts</dc:creator>
		<pubDate>Thu, 25 Dec 2008 02:12:27 +0000</pubDate>
		<guid isPermaLink="false">http://4.flowsnake.org/?p=303#comment-421</guid>
		<description>Hi,

I&#039;m not sure why it&#039;s useful to explicitly type the primitives : why not use the stack directly ? (btw, adding state to lists is a nice exercise, but I wouldn&#039;t use it in general : lists are very handy for recursion, stacks aren&#039;t ^_^)

So, something like this might be simpler :

let dup (a::stack) = a::a::stack
let add (a::b::stack) = (a + b)::stack

let words = [ &quot;dup&quot;, dup; &quot;add&quot;, add ]

type input = Num of int &#124; Word of string

let eval stack = function
    &#124; Num a -&gt; a::stack
    &#124; Word w -&gt; List.assoc w words stack

let process = List.fold_left eval []

Typechecking the expression seems to be more funny :) .</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I&#8217;m not sure why it&#8217;s useful to explicitly type the primitives : why not use the stack directly ? (btw, adding state to lists is a nice exercise, but I wouldn&#8217;t use it in general : lists are very handy for recursion, stacks aren&#8217;t ^_^)</p>
<p>So, something like this might be simpler :</p>
<p>let dup (a::stack) = a::a::stack<br />
let add (a::b::stack) = (a + b)::stack</p>
<p>let words = [ "dup", dup; "add", add ]</p>
<p>type input = Num of int | Word of string</p>
<p>let eval stack = function<br />
    | Num a -&gt; a::stack<br />
    | Word w -&gt; List.assoc w words stack</p>
<p>let process = List.fold_left eval []</p>
<p>Typechecking the expression seems to be more funny :) .</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh</title>
		<link>http://4.flowsnake.org/archives/303/comment-page-1#comment-420</link>
		<dc:creator>Josh</dc:creator>
		<pubDate>Wed, 24 Dec 2008 15:38:24 +0000</pubDate>
		<guid isPermaLink="false">http://4.flowsnake.org/?p=303#comment-420</guid>
		<description>I thought it made more sense to have the top item be the first operand in case you want to subtract a lot of numbers from a single number, so my subtraction is reversed compared to yours.

import java.io.*;
import java.util.ArrayList;

class Stack {
   private ArrayList stack = new ArrayList ();
   public void push(Integer i) {   stack.add(i);   }
   public Integer pop() {   return stack.remove(stack.size() - 1);   }
   public void swap() {   push(stack.remove(stack.size() - 2));   }
   public Integer peek() {   return stack.get(stack.size() - 1);   }
   public String toString() {   return stack.toString();   }
   public void execute(String cmd) {
           if (cmd.equals(&quot;swap&quot;)) {   swap();        }
      else if (cmd.equals(&quot;dup&quot;))  {   push(peek());  }
      else if (cmd.equals(&quot;drop&quot;)) {   pop();         }
      else if (cmd.equals(&quot;+&quot;)) {   push(pop() + pop());   }
      else if (cmd.equals(&quot;-&quot;)) {   push(pop() - pop());   }
      else if (cmd.equals(&quot;*&quot;)) {   push(pop() * pop());   }
      else if (cmd.equals(&quot;/&quot;)) {   push(pop() / pop());   }
      else {                        push(Integer.parseInt(cmd));   }
      System.out.println(stack);
   }
   public static void main(String[] args) {
      try {
         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
         String line = &quot;&quot;;
         Stack stack = new Stack();
         while ((line = in.readLine()) != null) {
            stack.execute(line);
         }
      } catch (Exception e) {
         System.out.println(e);
      }
   }
}</description>
		<content:encoded><![CDATA[<p>I thought it made more sense to have the top item be the first operand in case you want to subtract a lot of numbers from a single number, so my subtraction is reversed compared to yours.</p>
<p>import java.io.*;<br />
import java.util.ArrayList;</p>
<p>class Stack {<br />
   private ArrayList stack = new ArrayList ();<br />
   public void push(Integer i) {   stack.add(i);   }<br />
   public Integer pop() {   return stack.remove(stack.size() &#8211; 1);   }<br />
   public void swap() {   push(stack.remove(stack.size() &#8211; 2));   }<br />
   public Integer peek() {   return stack.get(stack.size() &#8211; 1);   }<br />
   public String toString() {   return stack.toString();   }<br />
   public void execute(String cmd) {<br />
           if (cmd.equals(&#8220;swap&#8221;)) {   swap();        }<br />
      else if (cmd.equals(&#8220;dup&#8221;))  {   push(peek());  }<br />
      else if (cmd.equals(&#8220;drop&#8221;)) {   pop();         }<br />
      else if (cmd.equals(&#8220;+&#8221;)) {   push(pop() + pop());   }<br />
      else if (cmd.equals(&#8220;-&#8221;)) {   push(pop() &#8211; pop());   }<br />
      else if (cmd.equals(&#8220;*&#8221;)) {   push(pop() * pop());   }<br />
      else if (cmd.equals(&#8220;/&#8221;)) {   push(pop() / pop());   }<br />
      else {                        push(Integer.parseInt(cmd));   }<br />
      System.out.println(stack);<br />
   }<br />
   public static void main(String[] args) {<br />
      try {<br />
         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));<br />
         String line = &#8220;&#8221;;<br />
         Stack stack = new Stack();<br />
         while ((line = in.readLine()) != null) {<br />
            stack.execute(line);<br />
         }<br />
      } catch (Exception e) {<br />
         System.out.println(e);<br />
      }<br />
   }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hans Nowak</title>
		<link>http://4.flowsnake.org/archives/303/comment-page-1#comment-419</link>
		<dc:creator>Hans Nowak</dc:creator>
		<pubDate>Wed, 24 Dec 2008 15:11:13 +0000</pubDate>
		<guid isPermaLink="false">http://4.flowsnake.org/?p=303#comment-419</guid>
		<description>@JJ: That might work, but they would still take a different number of arguments...</description>
		<content:encoded><![CDATA[<p>@JJ: That might work, but they would still take a different number of arguments&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JJ</title>
		<link>http://4.flowsnake.org/archives/303/comment-page-1#comment-418</link>
		<dc:creator>JJ</dc:creator>
		<pubDate>Wed, 24 Dec 2008 15:04:37 +0000</pubDate>
		<guid isPermaLink="false">http://4.flowsnake.org/?p=303#comment-418</guid>
		<description>Why not have the functions return lists? Then they would all have the same signature.</description>
		<content:encoded><![CDATA[<p>Why not have the functions return lists? Then they would all have the same signature.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hans Nowak</title>
		<link>http://4.flowsnake.org/archives/303/comment-page-1#comment-417</link>
		<dc:creator>Hans Nowak</dc:creator>
		<pubDate>Wed, 24 Dec 2008 12:02:30 +0000</pubDate>
		<guid isPermaLink="false">http://4.flowsnake.org/?p=303#comment-417</guid>
		<description>But I kinda like them... :-}</description>
		<content:encoded><![CDATA[<p>But I kinda like them&#8230; :-}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Lee Smith</title>
		<link>http://4.flowsnake.org/archives/303/comment-page-1#comment-416</link>
		<dc:creator>Mark Lee Smith</dc:creator>
		<pubDate>Wed, 24 Dec 2008 11:11:59 +0000</pubDate>
		<guid isPermaLink="false">http://4.flowsnake.org/?p=303#comment-416</guid>
		<description>In Java:

5 - 1

;).

Seriously, nice work.</description>
		<content:encoded><![CDATA[<p>In Java:</p>
<p>5 &#8211; 1</p>
<p>;).</p>
<p>Seriously, nice work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rich</title>
		<link>http://4.flowsnake.org/archives/303/comment-page-1#comment-415</link>
		<dc:creator>Rich</dc:creator>
		<pubDate>Wed, 24 Dec 2008 09:40:28 +0000</pubDate>
		<guid isPermaLink="false">http://4.flowsnake.org/?p=303#comment-415</guid>
		<description>Quick tip: You don&#039;t need all those double-semicolons.

When ;; appears before let, type, open [and a few others] you can drop it.</description>
		<content:encoded><![CDATA[<p>Quick tip: You don&#8217;t need all those double-semicolons.</p>
<p>When ;; appears before let, type, open [and a few others] you can drop it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
