[plt-dev] Racket web page
At Tue, 25 May 2010 13:29:15 -0400, Danny Yoo wrote:
> ;; gets the unique lines, although not guaranteeing order:
> (let ([a-ht (for/fold ([a-ht #hash()])
> ([line (in-lines (current-input-port))])
> (hash-set a-ht line #t))])
> (for ([line (in-hash-keys a-ht)])
> (printf "~a~n" line)))
I'd write that one as
#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)))
to preserve the order. Ok?
Another possibility is
#lang racket
;; Report each unique line from stdin:
(for/fold ([saw (hash)]) ([line (in-lines)])
(unless (hash-ref saw line #f)
(displayln line))
(hash-set saw line #t))
but the result from `for/fold' isn't wanted, so an extra `void' wrapper
(or similar) would be needed.
This program is currently the example of processing lines/strings with
`for' loop. It seems like a good representative of the set, because it
requires a bit more than just `awk'-like matching.
> #lang racket
> ;; Compute md5 checksum of the file given at the command line
> (require scheme/cmdline
> file/md5)
> (printf "~a~n" (md5 (command-line #:args (filename) filename)))
I'm not sure about a program that mostly calls a function that has a
Unix command-line counterpart. The use of `command-line' is a good
idea, and it fits nicely with Carl's dice program...
At Tue, 25 May 2010 17:31:43 -0400, Carl Eastlund wrote:
> #lang racket
> (define rx #px"(\\d*)d(\\d+)([+-]\\d+|)")
> (match (current-command-line-arguments)
> [(vector (regexp rx (list _ dice sides mod)))
> (apply + (or (string->number mod) 0)
> (build-list (or (string->number dice) 1)
> (λ (i) (+ 1 (random (string->number sides))))))])
Impressively compact! But there's really a lot going on there.
Dice make a good example for a simple command-line utility. Here's a
simpler program that is inspired by your example:
#lang racket
;; A dice-rolling command-line utility
(require racket/cmdline)
(command-line
#:args (sides dice)
(for ([i (in-range (string->number dice))])
(displayln (random (string->number sides)))))
At Tue, 25 May 2010 17:15:11 -0400, Neil Van Dyke wrote:
> Regarding single-character identifiers... Given that Scheme standard
> identifiers tend to be verbose, and that many of these examples squeezed
> into 40x7 are one-liners in Perl anyway, maybe focusing on readability
> is a good idea. Maybe bump it up to as wide as 70x7.
I'm willing to go a little wider --- wide enough for the Google
example, which is now added (based on Guillaume's version).
At Tue, 25 May 2010 14:51:15 -0600, Jon Rafkind wrote:
> Since all the code is extremely public can I make a strong request that
> variable names not be single letter characters? Some short word would be
> preferable.
I fixed some unnecessarily short names. Others would either make the
program wider (e.g., in the GUI program) or don't seem to merit long
names (e.g., `i' as an index variable).