[plt-dev] Racket web page
On Wed, May 26, 2010 at 10:32 AM, Carl Eastlund <cce at ccs.neu.edu> wrote:
> On Wed, May 26, 2010 at 10:27 AM, Matthias Felleisen
> <matthias at ccs.neu.edu> wrote:
>>
>> On May 25, 2010, at 8:48 PM, Matthew Flatt wrote:
>>
>>> #lang racket
>>> ;; Report each unique line from stdin:
>>> (let ([saw (make-hash)])
>>> (for ([line (in-lines)])
>>> (unless (hash-ref saw line #f)
>>> (displayln line))
>>> (hash-set! saw line #t)))
>>
>> I would prefer to sacrifice one line to avoid the repetition of the test (leakage). I doubt Perl and friends do that:
>>
>> #lang racket
>> ;; Report each unique line from stdin:
>> (define saw (make-hash))
>> (for ([line (in-lines)])
>> (hash-ref saw line
>> (lambda ()
>> (displayln line)
>> (hash-set! saw line #t))))
>
> To anyone who isn't intimately familiar with hash-ref, both versions
> are kind of obscure; the second one more so. If you insist on the
> second one, though, use a lambda character and put the displayln on
> the same line and you'll be back to 7. Personally, I'd recommend a
> variant of the first that uses hash-has-key? instead of hash-ref.
> It'll be much more readable.
And I would recommend using hash-ref! to save a line.
Jay
--
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://teammccarthy.org/jay
"The glory of God is Intelligence" - D&C 93