<div dir="ltr"><br>Hello fellow Rosetta submitter!<br><div><br></div><div>The regular expressions are actually pretty useful to make a functional version shorter than the imperative one!<br><br>First, forms with (for ...) and a bunch of (set!)&#39;s is made functional by (for/fold ...) where the final (values ...) functionally sets the new values.<br>
</div><div>Second, the regexp portion can take a converted symbol instead of a hard-coded string to save duplicate names.<br><br>I present the following compactified implementation:<br></div><div><br>(define-values (cei cie ie ei)<br>
  (for/fold ([cei 0] [cie 0] [ie 0] [ei 0])<br>    ([line (file-&gt;lines &quot;unixdict.txt&quot;)])<br>    (define-syntax-rule (tally x ...)<br>      (values (if (regexp-match? (symbol-&gt;string &#39;x) line) (add1 x) x) ...))<br>
    (tally cei cie ie ei)))<br><br>(print-rule &quot;I before E when not preceded by C&quot; (- ie cie) (- ei cei))<br>(print-rule &quot;E before I when preceded by C&quot; cei cie)<br></div><div class="gmail_extra"><br></div>
<div class="gmail_extra">Since the macro would be needlessly complicated with extra conditional logic to avoid counting &quot;cei&quot; as &quot;ei&quot;, it&#39;s better to just subtract at the end.<br><br></div><div class="gmail_extra">
I was wondering if there is some way to combine things like define-values and for/fold...in general I find that whenever I use the functional forms of (for...), since I will by definition care about the return values, it&#39;s inconvenient, especially in the multiple values case, to rebind the results to the accumulators in the for form (as seen in the above example).  In other words, since the results of the functional versions of (for...) HAVE to be in some way bound eventually (unless it&#39;s the entire program), it may as well perform the binding outright and save the duplication of (let ([result (for ...)]) ...).  Perhaps anaphoric for (afor...) that binds &quot;it&quot; or something like that, and for multiple values, it0, it1 ...<br>
</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 4, 2013 at 9:34 AM, Tobias Hammer <span dir="ltr">&lt;<a href="mailto:tobias.hammer@dlr.de" target="_blank">tobias.hammer@dlr.de</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Sorry for the misinformation but Eli is of course right that regexp-quote must be used.<br>
<br>
Example:<br>
<br>
(regexp-match? &quot;a&quot; &quot;abc&quot;)<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
#t<br>
</blockquote>
(regexp-match? &quot;.&quot; &quot;abc&quot;)<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
#t  ; WRONG! &#39;.&#39; matches every character<br>
</blockquote>
<br>
(regexp-match? (regexp-quote &quot;a&quot;) &quot;abc&quot;)<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
#t<br>
</blockquote>
(regexp-match? (regexp-quote &quot;.&quot;) &quot;abc&quot;)<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
#f<br>
</blockquote><div class="HOEnZb"><div class="h5">
<br>
<br>
On Tue, 04 Jun 2013 13:57:41 +0200, Daniel Prager &lt;<a href="mailto:daniel.a.prager@gmail.com" target="_blank">daniel.a.prager@gmail.com</a>&gt; wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Thanks Tobias &amp; Eli<br>
<br>
I&#39;ve updated my submission to use (regexp-match? sub-str str).<br>
<br>
[Also added the stretch goal solution, but could do with some refactoring<br>
to reduce the joint line-count.]<br>
<br>
-- Dan<br>
</blockquote></div></div><div class="HOEnZb"><div class="h5">
____________________<br>
 Racket Users list:<br>
 <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/<u></u>users</a><br>
</div></div></blockquote></div><br></div></div>