[plt-dev] Racket web page

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Tue May 25 22:39:34 EDT 2010

On Tue, May 25, 2010 at 8:48 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> 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)))))

#lang racket
;; A dice-rolling command-line utility
(require racket/cmdline)
(command-line
 #:args (dice sides)
 (for ([i (in-range (string->number dice))])
   (displayln (+ 1 (random (string->number sides))))))

This version puts "dice" and "sides" in the more canonical order, for
those of us used to writing both down consistently.  I also fixed your
off-by-one error in calling "random".

I'm kind of sad to give up the regexp matching, but I see another
example has that covered.

No one appears to have big-bang so far, so here's the best I can come
up with in 7 lines:

#lang htdp/bsl
(require 2htdp/image) (require 2htdp/universe)
;; Blow up a big red balloon before it deflates.
(define (balloon b) (circle b "solid" "red"))
(define (blow-up b k) (+ b 5))
(define (deflate b) (max (- b 1) 1))
(big-bang 50 (on-key blow-up) (on-tick deflate) (to-draw balloon 200 200))

--Carl


Posted on the dev mailing list.