<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!)'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->lines "unixdict.txt")])<br> (define-syntax-rule (tally x ...)<br> (values (if (regexp-match? (symbol->string 'x) line) (add1 x) x) ...))<br>
(tally cei cie ie ei)))<br><br>(print-rule "I before E when not preceded by C" (- ie cie) (- ei cei))<br>(print-rule "E before I when preceded by C" 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 "cei" as "ei", it'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'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'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 "it" 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"><<a href="mailto:tobias.hammer@dlr.de" target="_blank">tobias.hammer@dlr.de</a>></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? "a" "abc")<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
#t<br>
</blockquote>
(regexp-match? "." "abc")<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
#t ; WRONG! '.' matches every character<br>
</blockquote>
<br>
(regexp-match? (regexp-quote "a") "abc")<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 ".") "abc")<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 <<a href="mailto:daniel.a.prager@gmail.com" target="_blank">daniel.a.prager@gmail.com</a>> wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Thanks Tobias & Eli<br>
<br>
I'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>