[racket] Racket style question
Joe Gilray wrote:
> Thanks for sharing your code.
It beats staring blankly at a poorly written PE solution! And when the
counter tells you you'll have a result in a week, it's probably time to
rethink!
> Quick, newby question: why do you use "and" instead of "begin" in your
> progress function? I've seen others do this as well (of course, Eli's
> "(and...)" idiom above is a whole different animal... and very cool.
The code was factored out from a #:when clause in my loops, which would
read:
(for/XXX
([i 1000]
#:when (or (> (modulo i 5) 0) [the print bit])
[j 1000]
[k 1000]
[l 1000])
...)
[the print bit] started life (naively) as:
(printf "~a|" i) (flush-output)
Unfortunately, the (> ...) clause would fail on i=0,5,... causing the
printf to call, which returns void, a true value, so the "or" would succeed
without getting to the flush-output.
So, as you say, [the print bit] needed bracketing off. A "begin" would have
done. But the issue I had was with boolean logic operators so I looked to
them for a solution... "and" does the trick.
Re Style: looking back on it, my issue was with functions returning void,
which I find hard to make an opinion of one way or another whether it
should be true, false, or "don't use this in a boolean". (Yes I know
only #f is false and all that...). "Begin" would keep me out of that
philosophical quandary, and therefore be the style I would prefer, if I
had a mind about me!
"when" might have been even more appropriate, since it takes a boolean
condition and then arbitrary forms -- but I didn't really want to see:
#:when (when (> ...) print flush)
It's just odd.
Hope that hasn't exposed you too much to the darker corners of my thinking,
but you did ask!
Tim
--
Tim Brown <tim.brown at cityc.co.uk> | City Computing Limited |
T: +44 20 8770 2110 | City House, Sutton Park Road |
F: +44 20 8770 2130 | Sutton, Surrey, SM1 2AE, GB |
-----------------------------------------------------------------------|
BEAUTY: What's in your eye when you have a bee in your hand |
-----------------------------------------------------------------------'
City Computing Limited registered in London No. 1767817.
Registered Office: City House, Sutton Park Road, Sutton, Surrey, SM1 2AE
VAT number 372 8290 34.