<?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>Drinkable Chicken &#187; arc</title>
	<atom:link href="http://4.flowsnake.org/archives/tag/arc/feed" rel="self" type="application/rss+xml" />
	<link>http://4.flowsnake.org</link>
	<description>A Pythoneer&#039;s adventures with Scheme, etc.</description>
	<lastBuildDate>Fri, 15 Jul 2011 08:53:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Cutting</title>
		<link>http://4.flowsnake.org/archives/125</link>
		<comments>http://4.flowsnake.org/archives/125#comments</comments>
		<pubDate>Wed, 09 Jul 2008 13:38:10 +0000</pubDate>
		<dc:creator>Hans Nowak</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arc]]></category>
		<category><![CDATA[chicken]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://4.flowsnake.org/?p=125</guid>
		<description><![CDATA[I did some long overdue Chicken hackery yesterday, and by accident I found out that Chicken&#8217;s (or rather, SRFI-26&#8216;s) cut/cute macros are not the same as Arc&#8217;s [ ] syntax after all. Scheme/Arc aficionados already knew this, of course, but to me it was news since I&#8217;ve never really used cut much. Here&#8217;s a short [...]]]></description>
			<content:encoded><![CDATA[<p>I did some long overdue Chicken hackery yesterday, and by accident I found out that Chicken&#8217;s (or rather, <a href="http://srfi.schemers.org/srfi-26/srfi-26.html">SRFI-26</a>&#8216;s) <em>cut</em>/<em>cute</em> macros are not the same as Arc&#8217;s <em>[ ]</em> syntax after all.</p>
<p>Scheme/Arc aficionados already knew this, of course, but to me it was news since I&#8217;ve never really used <em>cut</em> much. Here&#8217;s a short explanation.</p>
<p>Scheme&#8217;s <em>cut</em> produces an anonymous function that takes as many arguments as there are <em><></em> symbols at the &#8220;top level&#8221;. E.g.</p>
<pre>> (cut + 1 &lt;&gt;)
#&lt;procedure (? g3)&gt;
; has one argument

> (cut + 1 &lt;&gt; &lt;&gt;)
#&lt;procedure (? g4 g5)&gt;
; has two arguments

; etc...
</pre>
<p>However, any <em>&lt;&gt;</em> that is found in a nested expression, is not considered as a parameter. Therefore:</p>
<pre>;this works...
> (map (cut + 1 &lt;&gt;) '(1 2 3))
(2 3 4)

; but this does not:
> (map (cut + 1 (* 2 &lt;&gt;)) '(1 2 3))
Error: bad argument count - received 1 but expected 0: #&lt;procedure (?)&gt;
</pre>
<p>By contrast, Arc&#8217;s <em>[ ]</em> syntax produces an anonymous function that expects one and only one argument, which is represented by a single underscore. As a result, it&#8217;s simultaneously more and less limited than <em>cut</em>:</p>
<pre>arc> (map [+ 1 _] '(1 2 3))
(2 3 4)
arc> (map [+ 1 (* 2 _)] '(1 2 3))
(3 5 7)
; no problem

; but two parameters doesn't work:
arc> (map [cons _ _] '(1 2 3) '(4 5 6))
Error: "#&lt;procedure&gt;: expects 1 argument, given 2: 1 4"
</pre>
<p>Of course, for anonymous functions that are more complex than this, it&#8217;s probably better to just use <em>lambda</em>&#8230; :-/  Or a named function.</p>
]]></content:encoded>
			<wfw:commentRss>http://4.flowsnake.org/archives/125/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Modules</title>
		<link>http://4.flowsnake.org/archives/64</link>
		<comments>http://4.flowsnake.org/archives/64#comments</comments>
		<pubDate>Wed, 13 Feb 2008 03:14:22 +0000</pubDate>
		<dc:creator>Hans Nowak</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arc]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://4.flowsnake.org/archives/64</guid>
		<description><![CDATA[I noticed there&#8217;s a new version of Arc. According to the web page, &#8220;The most dramatic change is probably the ability to use x.y and x!y as abbreviations for (x y) and (x &#8216;y) respectively.&#8221; The x.y syntax reminds me of one of the things I miss in Scheme: a &#8220;Pythonic&#8221; module system. By &#8220;Pythonic&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>I noticed there&#8217;s a <a href="http://arclanguage.org/item?id=2166">new version of Arc</a>. According to the web page, &#8220;The most dramatic change is probably the ability to use <em>x.y</em> and <em>x!y</em> as abbreviations for <em>(x y)</em> and <em>(x &#8216;y)</em> respectively.&#8221;</p>
<p>The <em>x.y</em> syntax reminds me of one of the things I miss in Scheme: a &#8220;Pythonic&#8221; module system. By &#8220;Pythonic&#8221; I mean, that you can import modules, and get a module object back, that you can access using <em>x.y</em> syntax. I&#8217;m mostly interesting in the namespace issue here, as the convention in Scheme seems to be, to just stick everything in the global namespace.</p>
<p>In other words, I am somewhat uncomfortable that it&#8217;s not possible to do this:</p>
<pre>;; --- foo.scm ---

(define (bar x)
  (+ x 1))

;; --- REPL ---

&gt; (import foo)
&gt; foo
#&lt;module foo&gt;
&gt; (foo.bar 3)
4
&gt; foo.bar
#&lt;procedure (foo.bar x)&gt;</pre>
<p>&#8230;or something to that effect.</p>
<p>Maybe this is an irrational &#8220;need&#8221;, but I like namespaces, and have gotten used to them over the years, and loading the toplevel namespace with lots and lots of definitions just seems a bit &#8220;unsafe&#8221; to me.</p>
<p>I found a <a href="http://hkn.eecs.berkeley.edu/~dyoo/plt/modules.text">comparison of Python&#8217;s and PLT Scheme&#8217;s module systems</a>, which explains the issue better:</p>
<blockquote><p>Once we&#8217;ve done <em>&#8216;(require (lib &#8220;math.ss&#8221;))&#8217;</em>, we have access to the internals of the <em>math</em> library.  But there&#8217;s one surprise: unlike Python, &#8216;<em>math</em>&#8216; itself is not a first-class object. By default, the require form has the same semantics as Python&#8217;s &#8220;<em>from [module] import *</em>&#8220;!</p></blockquote>
<p>The article then mentions the following technique to add a prefix to the names imported from a module:</p>
<pre>&gt; (require (prefix math. (lib "math.ss" "mzlib")))
&gt; math.e
2.718281828459045</pre>
<p>I suppose this would not be too bad as an alternative, although you still cannot do things like inspecting a module, pass it around, etc. Chicken does not seem to support it though, and Schemers generally don&#8217;t seem to miss the feature. (Or maybe there&#8217;s a reason why it would be a bad idea in Scheme.) So maybe I should just learn to live without it. Thoughts welcome&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://4.flowsnake.org/archives/64/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Arc-macros, baby. Arc-macros. (part II)</title>
		<link>http://4.flowsnake.org/archives/54</link>
		<comments>http://4.flowsnake.org/archives/54#comments</comments>
		<pubDate>Thu, 31 Jan 2008 21:44:49 +0000</pubDate>
		<dc:creator>Hans Nowak</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arc]]></category>
		<category><![CDATA[chicken]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://4.flowsnake.org/archives/54</guid>
		<description><![CDATA[More useless reader macros ahead. This time, let&#8217;s implement Arc&#8217;s [ ... _ ... ] lambda shortcut. In Chicken Scheme, of course. ^_^ I will be using { } rather than [ ]. Incidentally, Chicken already supports { } as an alternative for ( ): &#62; '{1 2 3} 1 2 3) &#62; {+ 1 [...]]]></description>
			<content:encoded><![CDATA[<p>More useless reader macros ahead. This time, let&#8217;s implement Arc&#8217;s <em>[ ... _ ... ]</em> lambda shortcut. In Chicken Scheme, of course. <tt>^_^</tt></p>
<p>I will be using <em>{ }</em> rather than <em>[ ]</em>. Incidentally, Chicken already supports <em>{ }</em> as an alternative for <em>( )</em>:</p>
<pre>&gt; '{1 2 3}
1 2 3)
&gt; {+ 1 2}
3</pre>
<p>This reader macro will override that. Anyway, the code follows. It&#8217;s clumsy (although not as clumsy as my first version), and there&#8217;s probably a better/shorter way to do it (bit like installing the <a href="http://www.o2.co.uk/broadband/mobile/">latest O2 UK Mobile Broadband</a> after struggling through the 90s with 56k), that I&#8217;m not aware of yet. In any case, it&#8217;s naive; for example, this version does not allow nesting the <em>{ }</em> constructs. Also, the <em>replace-underscore</em> function won&#8217;t replace underscores in nested lists. <sup>1</sup>) And so on. Remember that this is just for demonstration purposes before pointing out the zillions of flaws. :-)</p>
<pre>(define (read-until-next-curly port)
  "clumsy way to read up until the first }."
  (read-token (lambda (c) (not (char=? c #\}))) port))

(define (replace-underscore exprlist)
  (define (replace-underscore-item y)
    (if (equal? y '_) 'x y))
  (map replace-underscore-item exprlist))

(define (make-lambda-form exprlist)
  `(lambda (x) (,@exprlist)))

(define (eval-string s)
  (with-input-from-string s read))

(define (transform-into-lambda s)
  (let* ((expr (eval-string s))
         (expr2 (replace-underscore expr)))
    (make-lambda-form expr2)))

(set-read-syntax! #\{
  (lambda (port)
    (let* ((s (read-until-next-curly port))
           (t (string-append "(" s ")")))
      (read-byte port) ; read trailing '}'
      (transform-into-lambda t))))</pre>
<p>Testing&#8230;</p>
<pre>(print { + _ 1 })     ; =&gt; #&lt;procedure (? x)
(print ({+ _ 1} 44))  ; =&gt; 45

(print (map { * _ 2 } '(1 2 3 4 5 6)))
; =&gt; (2 4 6 8 10 12)</pre>
<p>Cool. Of course, if you use a lot of small lambdas, then it&#8217;s possible to write a macro that shortens things for you, rather than having to resort to syntax changes. For example:</p>
<pre>&gt; (define-macro (fn . body)
&gt;   `(lambda (_) ,body))
&gt; (fn + _ 1)
#&lt;procedure (? _)&gt;
&gt; ((fn + _ 1) 44)
45

;; compare:
&gt; ((lambda (x) (+ x 1)) 44)
45</pre>
<p>Anyway, much like the <a href="/archives/53">previous post</a>, this was just an excuse to explore reader macros a bit more. So don&#8217;t shoot me&#8230;</p>
<p><sup>1</sup>) On a side note, why doesn&#8217;t SRFI-1 have a function to replace items in a list?</p>
]]></content:encoded>
			<wfw:commentRss>http://4.flowsnake.org/archives/54/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Arc-macros, baby. Arc-macros.</title>
		<link>http://4.flowsnake.org/archives/53</link>
		<comments>http://4.flowsnake.org/archives/53#comments</comments>
		<pubDate>Thu, 31 Jan 2008 19:19:30 +0000</pubDate>
		<dc:creator>Hans Nowak</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arc]]></category>
		<category><![CDATA[chicken]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://4.flowsnake.org/archives/53</guid>
		<description><![CDATA[OK, this is not really about macros, it&#8217;s really a reader hack, but bear with me&#8230; :-) Let&#8217;s implement Arc&#8217;s &#8220;negation&#8221; operator (~) in Chicken. It&#8217;s only a few lines of code. (define (negate pred) (lambda (x) (not (pred x)))) (set-read-syntax! #\~ (lambda (port) (let ((expr (read port))) (list 'negate expr)))) That&#8217;s all. Test test&#8230; [...]]]></description>
			<content:encoded><![CDATA[<p>OK, this is not really about macros, it&#8217;s really a reader hack, but bear with me&#8230; :-)</p>
<p>Let&#8217;s implement Arc&#8217;s &#8220;negation&#8221; operator (<em>~</em>) in Chicken. It&#8217;s only a few lines of code.</p>
<pre>(define (negate pred)
  (lambda (x)
    (not (pred x))))

(set-read-syntax! #\~
  (lambda (port)
    (let ((expr (read port)))
      (list 'negate expr))))</pre>
<p>That&#8217;s all. Test test&#8230; (Code below uses SRFI-1 for <em>filter</em>.)</p>
<pre>&gt; (use srfi-1)
; loading library srfi-1 ...
&gt; (filter ~odd? '(1 2 3 4 5 6 7 8))
(2 4 6 8)
&gt; (filter ~(lambda (x) (&gt; x 2)) '(0 1 2 3 4 5 6))
(0 1 2)</pre>
<p>This probably falls in the category &#8220;don&#8217;t try this at home&#8221;.  But if you&#8217;re coming from languages where this just isn&#8217;t possible, this kind of stuff is really cool.</p>
<p>Note #1: you can just use <em>(negate odd?)</em> and such, which is slightly longer but probably cleaner. And that kind of thing would work in Python as well. (Although function composition isn&#8217;t all that popular in Python-land.)</p>
<p>Note #2: This implementation just reads the expression that follows <em>~</em>, then feeds it to <em>negate</em>. So using a <em>lambda</em> works as well (but kind of defeats the purpose, which is conciseness).</p>
<p>By the way, the reader extension I showed a few days ago (to add <a href="/archives/49">Awk-like $N behavior</a>) works, but I found out that it&#8217;s not really how you&#8217;re supposed to write it. At the point something like <em>$3</em> is read, we&#8217;re *reading* rather than *evaluating*, so it should really expand to something that returns the right result *when evaluated later*. So better code would be:</p>
<pre>(set-read-syntax! #\$
  (lambda (port)
    (let* ((s (read-number port))
           (i (string-&gt;number s)))
      (list 'field i))))</pre>
<p>In other words:</p>
<ul>
<li>when the reader encounters <em>$3</em>, it expands it to the form <em>(field 3)</em> (but *does not* evaluate it at that point)</li>
<li>later on, we evaluate <em>(field 3)</em> and get the correct result, using whatever is in <em>*fields*</em> at the time of evaluation</li>
</ul>
<p>So yeah, maybe this is like macros after all, but I don&#8217;t know the correct term. :-)</p>
<p>There&#8217;s another post like this coming up, using Arc as an excuse to tinker with the Scheme reader.</p>
<p><strong>Update (2008-02-04)</strong>: Here is <a href="http://konryd.blogspot.com/2008/02/implementing-arcs-function-negation.html">similar code in Python</a>, sort of. The additional &#8220;syntax&#8221; (which is actually, creating a class that defines &#8220;~&#8221;, and then wrapping functions in it) seems to be more trouble than it&#8217;s worth. If you do this kind of thing a lot in Python (probably not, but you never know), you might be better off using a simple <em>negate</em> function, e.g.</p>
<pre>def negate(f):
    return lambda *args, **kwargs: not f(*args, **kwargs)</pre>
<p>(I normally would not have used <em>lambda</em>, but after more than a month of Scheming, it&#8217;s hard not to. :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://4.flowsnake.org/archives/53/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Arc, first impressions</title>
		<link>http://4.flowsnake.org/archives/52</link>
		<comments>http://4.flowsnake.org/archives/52#comments</comments>
		<pubDate>Thu, 31 Jan 2008 04:33:25 +0000</pubDate>
		<dc:creator>Hans Nowak</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arc]]></category>

		<guid isPermaLink="false">http://4.flowsnake.org/archives/52</guid>
		<description><![CDATA[I took a quick look at Arc. If nothing else, I want to see how it compares to Scheme. Here are a few remarks (in no particular order). = is used for assignment rather than for equality testing. This may be confusing to people using other variants of Lisp. Possibly less so to users of [...]]]></description>
			<content:encoded><![CDATA[<p>I took a quick look at <a href="/archives/51">Arc</a>. If nothing else, I want to see how it compares to Scheme. Here are a few remarks (in no particular order).</p>
<ul>
<li><em>=</em> is used for assignment rather than for equality testing. This may be confusing to people using other variants of Lisp. Possibly less so to users of languages that use the same operator. Still, inside parentheses it looks a bit odd: <em>(= x 4)</em></li>
</ul>
<ul>
<li>Many abbrevations! Quite a few of them seem gratuitous; I&#8217;m used to <em>def</em>, but what about <em>mac</em>, <em>rem</em>, <em>pr</em>, <em>prn</em>, <em>fn</em>, <em>o</em>, and so on, some of which only shave off a character or two. It makes the resulting code a bit shorter, but whether it&#8217;s clearer is a different issue.</li>
</ul>
<ul>
<li>The <em>[ ... _ ... ]</em> syntax seems useful. This is basically a shortcut for simple lambdas, e.g. <em>[+ _ 1]</em> is equivalent to <em>(fn (x) (+ x 1))</em>.</li>
</ul>
<ul>
<li>Odd assignment rules for lists and hash tables. E.g. if <em>airports</em> is a hash table, then <em>(airports &#8220;Boston&#8221;)</em> looks up the key &#8220;Boston&#8221; in there. This also works in assignments: <em>(= (airports &#8220;Orlando&#8221;) &#8216;mco)</em>. Lists work the same way, if I understand correctly. Personally, I think it&#8217;s a bit confusing, and I&#8217;m not sure it&#8217;s a good idea to overload this kind of syntax. <em>(foo bar)</em> can now mean: function application, hash table lookup, list lookup&#8230;</li>
</ul>
<ul>
<li>On the other hand, you get to do table lookups with <em>map</em>:<br />
<em>(map airports &#8216;(&#8220;San Francisco&#8221; &#8220;Orlando&#8221; &#8220;Paris&#8221;))</em><br />
=&gt; <em>(sfo mco cdg)</em></li>
</ul>
<ul>
<li>On a side note, the use of hash tables for dictionaries, and the <em>keys</em> and <em>vals</em> functions to get keys and values, look suspiciously Pythonic. :-)</li>
</ul>
<ul>
<li>Compose functions with &#8220;:&#8221;:<br />
<em>odd:car</em> is equivalent to <em>(fn (x) (odd (car (x)))</em></li>
</ul>
<ul>
<li> &#8220;Negation&#8221; operator <em>~</em>:<br />
<em>~odd? </em>is equivalent to <em>(fn (x) (not (odd? x)))</em><br />
N.B. It&#8217;s not hard to do the same thing in Chicken&#8230; I already talked about <a href="/archives/49">defining custom literals</a> in a separate post, and an implementation of <em>~</em> would use the same mechanism. More about this <a href="/archives/53">later</a>.</li>
</ul>
<ul>
<li>if with more than 3 arguments is a nested if&#8230; e.g.<br />
<em>(if a b c d e)</em> =&gt; <em>(if a b (if c d e))</em></li>
</ul>
<ul>
<li>Some forms were reinvented without parentheses, which is much clearer, IMHO. E.g.<br />
<em>(let x 1 &#8230;body&#8230;)</em><br />
<em>(with (a 1 b 2) &#8230;body&#8230;)</em><br />
Compare this to the parentheses-fest that is <a href="http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-7.html#%_idx_124"><em>let</em></a> in Scheme and Common Lisp. :-}</li>
</ul>
<p>No &#8220;conclusion&#8221; at this point; I have only skimmed the tutorial and tinkered a bit with the REPL. Plus, it&#8217;s likely to change anyway.</p>
<p>On forums, the biggest gripe people seem to have is the lack of Unicode support, and PG&#8217;s apparent unwillingless to add it.</p>
]]></content:encoded>
			<wfw:commentRss>http://4.flowsnake.org/archives/52/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Arc!</title>
		<link>http://4.flowsnake.org/archives/51</link>
		<comments>http://4.flowsnake.org/archives/51#comments</comments>
		<pubDate>Wed, 30 Jan 2008 00:27:39 +0000</pubDate>
		<dc:creator>Hans Nowak</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arc]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://4.flowsnake.org/archives/51</guid>
		<description><![CDATA[Who cares about the Florida primaries when there is some *real* news? Arc is out. (Took them long enough. :-) The official site is here. I will probably download it and give it a spin tonight, after wrestling through the tutorial, forum discussions, and comment threads on Reddit and Y Combinator. More about this later, [...]]]></description>
			<content:encoded><![CDATA[<p>Who cares about the Florida primaries when there is some *real* news? <a href="http://paulgraham.com/arc0.html">Arc is out.</a> (Took them long enough. :-) The official site is <a href="http://arclanguage.org/">here</a>. I will probably download it and give it a spin tonight, after wrestling through the <a href="http://ycombinator.com/arc/tut.txt">tutorial</a>, <a href="http://arclanguage.org/forum">forum discussions</a>, and comment threads on <a href="http://reddit.com/r/programming/info/6710p/comments/">Reddit</a> and <a href="http://news.ycombinator.com/item?id=106398">Y Combinator</a>.</p>
<p>More about this later, without a doubt.</p>
<p>(First thought: It&#8217;s built on top of MzScheme. Can we do s/MzScheme/Chicken? :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://4.flowsnake.org/archives/51/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

