[racket-dev] Code micro-level organization

From: Neil Toronto (neil.toronto at gmail.com)
Date: Thu May 31 13:03:30 EDT 2012

On 05/30/2012 03:40 PM, Eli Barzilay wrote:
> Now, lets imagine that instead of a simple `<>' hole, there are two
> kinds of holes with an "up" or a "down" direction -- this leads to
> this kind of a syntax:
>
>    (○ "foo bar baz"
>       (substring ↑ 3 8)
>       (string-trim ↑)
>       (let ([str ↑]) ↓)
>       (and (regexp-match? #rx"^[a-z].*[a-z]$" str) ↓)
>       (string-append "*" str "*"))
>
> where you can read `↑' as "the above" and `↓' as "the below".

It seems like `↑' is another way to not name expressions (i.e. a 
"pointless style" :D), and `↓' is handled just fine by internal 
definitions. This is equivalent, currently defined, has less nesting, 
and avoids a rename:

    (define orig-str "foo bar baz")
    (define sub (substring orig-str 3 8))
    (define str (string-trim sub))
    (define m (regexp-match? #rx"^[a-z].*[a-z]$" str))
    (and m (string-append "*" str "*"))

A `define*' form like Jay (and I) want would make these kinds of things 
less error-prone and allow names to be reused:

    (define* ↑ "foo bar baz")
    (define* ↑ (substring ↑ 3 8))
    (define* str (string-trim ↑))
    (define* ↑ (regexp-match? #rx"^[a-z].*[a-z]$" str))
    (and ↑ (string-append "*" str "*"))

It's still pretty wordy, though. Even if I had `define*' I'd be tempted 
to go with my current favorite idiom, which trades wordiness for a 
nesting level:

(let* ([↑    "foo bar baz"]
        [↑    (substring ↑ 3 8)]
        [str  (string-trim ↑)]
        [↑    (regexp-match? #rx"^[a-z].*[a-z]$" str)])
   (and ↑ (string-append "*" str "*")))

I occasionally get annoyed by how deeply these can get nested. I feel 
your pain, man.

Neil ⊥

Posted on the dev mailing list.