[racket] Racket style question

From: Rodolfo Carvalho (rhcarvalho at gmail.com)
Date: Mon Mar 19 02:08:11 EDT 2012

Hi Joe,

On Mon, Mar 19, 2012 at 01:44, Joe Gilray <jgilray at gmail.com> wrote:

> Hi, I created the following code from some scheme I found on
> ProjectEuler.net (problem #9):
>
> (define (right-triangle? a b c)
>   (= (+ (* a a) (* b b)) (* c c)))
>

> -snip-
>

Thoughts?
>
>
When I saw your code it made me think of a good chance to use for*.

This is what I got:


(define (pythagorean-triple/alt n)
  (for*/first ([a (in-range 1 n)]
               [b (in-range a n)]
               [c (in-value (- n a b))]
               #:when (right-triangle? a b c))
    (list a b c)))


The cool thing is that this is my first use of
in-value<http://docs.racket-lang.org/reference/sequences.html#(def._((lib._racket/private/base..rkt)._in-value))>,
and the docs say that "This form is mostly useful for
let<http://docs.racket-lang.org/reference/let.html#(form._((lib._racket/private/letstx-scheme..rkt)._let))>-like
bindings in forms such as
for*/list<http://docs.racket-lang.org/reference/for.html#(form._((lib._racket/private/base..rkt)._for*/list))>
."!

One more nice thing about using *for *is that the loop becomes bounded (I
got infinite running programs when I ran your code with bad input n).


[]'s

Rodolfo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120319/f8cb6496/attachment-0001.html>

Posted on the users mailing list.