[plt-dev] Racket web page

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Tue May 25 17:31:43 EDT 2010

On Tue, May 25, 2010 at 3:30 PM, Eli Barzilay <eli at barzilay.org> wrote:
> Anyway, you would obviously be too focused on the game to sum up your
> 7d20:
>  #lang racket
>  (for ([s (current-command-line-arguments)])
>    (match s
>      [(regexp #px"(\\d+)d(\\d+)" (list _ m n))
>       (let ([rolls (for/list ([i (in-range (string->number m))])
>                      (+ 1 (random (string->number n))))])
>         (printf "~s => ~a\n" rolls (apply + rolls)))]))

Here's another version that's 7 lines, does the summing up, and
includes a + or - modifier.  It also uses more readable variable
names.  It sacrifices multiple arguments, and printing individual
dice, but those are less compelling for a 7-line version.  Afterward
is a blurb that could be included for the "?" link.

#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))))))])

Playing a game but no dice on hand?
This program takes an argument of the form
"MdN" with optional suffix "+X" or "-X" and
adds up M dice with N sides with modifier X.

[galstaff at light] roll 3d4+3 > darkness

> It's a really nice idea, IMO -- tempting to extend to a full dice app
> thing.  How about adding a delta:

In college I had SML versions of this program that had endless bells
and whistles.  It was really ridiculous.  I'm happy to stick to 7-line
versions for now, but knock yourself out.  :)


Posted on the dev mailing list.