[racket-dev] 2htdp/image Feature Suggestion

From: Jos Koot (jos.koot at gmail.com)
Date: Mon Jun 23 21:44:31 EDT 2014

Hi Robby
Replacing if by cond is very stylisch indeed. Nowadays I use cond more than
if, especially if it is nested or if I need internal definitions in one or
more of the alternatives. Replacing the outer named let by a reference to a
function defined outside of the procedure is not too difficult, I think. I
have tried to follow the recommendations strictly for this piece of code,
especially avoiding the nesting of all let-forms (named or unnamed) of which
this piece of code has four levels, two named and two unnamed. When I try
that for all levels, I introduce bugs.

Don't understand me wrong. I do appreciate the recommendations. In fact the
style interests me much. I see the advantages of cooperators using the same
style. Writing this, the idea comes up in my mind that it should be possible
to do some transformations to the recommended style by means of redex. Or
may be by means of syntax-case while limiting the transformers to the
desired level of expansion. Both instuments are interesting enough for me to
give it a try. If I am able to do something usefull in this sense, you'll
hear of me, but give me some time, please.

Thanks for your reaction, Jos

PS Within DrRacket transformation to the recommended indentation is almost
trivial. 'Almost', for in some cases additional newlines may be neceessary.
And there is pretty-print, of course.

> -----Original Message-----
> From: Robby Findler [mailto:robby at eecs.northwestern.edu] 
> Sent: martes, 24 de junio de 2014 2:12
> To: Jos Koot
> Cc: Asumu Takikawa; Kevin Forchione; dev at racket-lang.org; 
> jja.koot at gmail.com
> Subject: Re: [racket-dev] 2htdp/image Feature Suggestion
> 
> I think that the prefer-define-over-let applies only to the first let
> in this program.
> 
> The style guide also would recommend 'cond' over 'if' here, but it
> becomes very very important to do that only if there were nested
> 'let's or 'begin's or the like and you don't have that.
> 
> Robby
> 
> On Mon, Jun 23, 2014 at 12:32 PM, Jos Koot <jos.koot at gmail.com> wrote:
> > In the recommendations of 
> http://docs.racket-lang.org/style/index.html it is
> > recommended to use (internal or module top-level) define 
> rather than named
> > let.
> > I use named let a lot. How would you rewrite the following? 
> For me it is
> > rather difficult to make the change without loosing track 
> of the scope of
> > the variables. I also have a rather distinct way of indenting.
> >
> > #lang racket
> >
> > #|
> > A rearrangement of a list L is a list with the same elements as L,
> > but possibly in another order.
> > Proc make-natural->rearrangement takes a list L and returns a proc.
> > Let N be the number of distinct rearrangements of L.
> > The returned proc takes an index K less than N and returns the K-th
> > distinct rearrangement of L. Two rearrangements R0 and R1 
> are distinct if
> > (not (andmap EQ? R0 R1)).
> > EQ? must be an equivalence relation on the elements of L.
> >
> > E = element
> > H = head of L
> > T = tail of L (append H T) = L
> > K = index
> > N = nr of distinct rearrangements of L.
> > |#
> >
> > (define (make-natural->rearrangement L (EQ? equal?))
> >  (let ((N (nr-of-rearrangements L EQ?)))
> >   (lambda (K)
> >    (let rearrange ((L L) (K K) (N N) (result '()))
> >     ; Look for the K-th rearrangement of L and put it's 
> elements in result.
> >     (if (null? L) result
> >      (let pseudo-rotate ((H L) (T '()) (K K))
> >       ; Look in subsequent pseudorotations.
> >       (let ((E (car H)) (H (cdr H)))
> >        (if (member E T EQ?)
> >         ; Skip pseudorotation if it's car already was car 
> of a previous one.
> >         (pseudo-rotate H (cons E T) K)
> >         (let ((M (/ (* N (count-occurrences E L EQ?)) (length L))))
> >          ; M is the nr of rearrangements of (append H T)
> >          ; computed by means of a recurrent relation.
> >          (if (< K M)
> >           ; The rearrangement is in this pseudorotation.
> >           (rearrange (append H T) K M (cons E result))
> >           ; The rearrangement is not in this pseudorotation.
> >           ; Look in the following pseudorotation, but 
> decrease K by the
> >           ; nr of rearrangements of the skipped pseudorotation.
> >           (pseudo-rotate H (cons E T) (- K M))))))))))))
> >
> > (define (nr-of-rearrangements L EQ?) ...)
> > (define (count-occcurrences E L EQ?) ...)
> >
> >> -----Original Message-----
> >> From: dev [mailto:dev-bounces at racket-lang.org] On Behalf Of
> >> Asumu Takikawa
> >> Sent: lunes, 23 de junio de 2014 7:35
> >> To: Kevin Forchione
> >> Cc: dev at racket-lang.org
> >> Subject: Re: [racket-dev] 2htdp/image Feature Suggestion
> >>
> >> On 2014-06-22 20:27:21 -0700, Kevin Forchione wrote:
> >> >    Thanks! Is there any documentation or guide on which
> >> *styles* to prefer in
> >> >    writing Racket code? I find myself scratching my head at
> >> times in these
> >> >    matters!
> >>
> >> In recent Racket distributions and online docs there's now a style
> >> manual:
> >>
> >>   http://docs.racket-lang.org/style/index.html
> >>
> >> Cheers,
> >> Asumu
> >> _________________________
> >>   Racket Developers list:
> >>   http://lists.racket-lang.org/dev
> >
> > _________________________
> >   Racket Developers list:
> >   http://lists.racket-lang.org/dev


Posted on the dev mailing list.