[racket] Rosetta code submission / string operations

From: Daniel Prager (daniel.a.prager at gmail.com)
Date: Tue Jun 4 10:10:53 EDT 2013

Hi Sean

If you look again on the page I hit on a similar for/fold solution
independently, which is now up on Rosetta

Amusingly, the Common Lisp version was submitted by a colleague of mine who
I mentioned this exercise to, so no doubt there'll be some friendly banter
tomorrow!  So any further help pn the code golf aspects is much appreciated!

-- Dan


On Wed, Jun 5, 2013 at 12:05 AM, Sean Kanaley <skanaley at gmail.com> wrote:

>
> Hello fellow Rosetta submitter!
>
> The regular expressions are actually pretty useful to make a functional
> version shorter than the imperative one!
>
> 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.
> Second, the regexp portion can take a converted symbol instead of a
> hard-coded string to save duplicate names.
>
> I present the following compactified implementation:
>
> (define-values (cei cie ie ei)
>   (for/fold ([cei 0] [cie 0] [ie 0] [ei 0])
>     ([line (file->lines "unixdict.txt")])
>     (define-syntax-rule (tally x ...)
>       (values (if (regexp-match? (symbol->string 'x) line) (add1 x) x)
> ...))
>     (tally cei cie ie ei)))
>
> (print-rule "I before E when not preceded by C" (- ie cie) (- ei cei))
> (print-rule "E before I when preceded by C" cei cie)
>
>  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.
>
> 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 ...
>
> On Tue, Jun 4, 2013 at 9:34 AM, Tobias Hammer <tobias.hammer at dlr.de>wrote:
>
>> Sorry for the misinformation but Eli is of course right that regexp-quote
>> must be used.
>>
>> Example:
>>
>> (regexp-match? "a" "abc")
>>
>>> #t
>>>
>> (regexp-match? "." "abc")
>>
>>> #t  ; WRONG! '.' matches every character
>>>
>>
>> (regexp-match? (regexp-quote "a") "abc")
>>
>>> #t
>>>
>> (regexp-match? (regexp-quote ".") "abc")
>>
>>> #f
>>>
>>
>>
>> On Tue, 04 Jun 2013 13:57:41 +0200, Daniel Prager <
>> daniel.a.prager at gmail.com> wrote:
>>
>>  Thanks Tobias & Eli
>>>
>>> I've updated my submission to use (regexp-match? sub-str str).
>>>
>>> [Also added the stretch goal solution, but could do with some refactoring
>>> to reduce the joint line-count.]
>>>
>>> -- Dan
>>>
>> ____________________
>>  Racket Users list:
>>  http://lists.racket-lang.org/**users<http://lists.racket-lang.org/users>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130605/ac5f7321/attachment.html>

Posted on the users mailing list.