On Thu, Jul 6, 2017 at 8:53 AM, Sam Tobin-Hochstadt <[email protected]> wrote:
> Just for clarity, here's an example using `set!` and `define`, > although I wouldn't really suggest this style: > > #lang typed/racket > > (: f : Real -> Real) > (define (f x) > (define rand-value (random)) > (define new-value (+ x rand-value)) > (set! new-value (- new-value x)) > new-value) > > Sam > You wouldn't suggest it because of the set! or because of the defines? define seems like a useful construct -- if you're already inside the desired scope then (define x ...) is a less verbose syntax than (let ((x ...)) ...) and it doesn't introduce another layer of indentation. > > On Wed, Jul 5, 2017 at 10:52 PM, Alasdair McAndrew <[email protected]> > wrote: > > Well, after a bit of fiddling, I've discovered I can indeed do what I > need to, with a judicious use of "let" for creating a swag of random > values, and ensuring that their use is all within the scope of let. So > far, all good! > > > > On Thursday, 6 July 2017 10:54:53 UTC+10, Royall Spence wrote: > >> Sounds like two questions wrapped into one. When it comes to setting > >> names to values, Scheme programming encourages the use of a "let" > >> expression to bind values to names inside of a (usually narrow) scope > >> rather than assigning a value to a variable. See more here: > >> https://docs.racket-lang.org/guide/let.html > >> > >> As for the typed random value, the Flonum from (random) should be fine > >> since a Flonum is also a Real. Can you provide a short example of > >> runnable code that exposes the problem you're having? > >> > >> On Wed, Jul 5, 2017, at 08:39 PM, Alasdair McAndrew wrote: > >> > I'm doing a little programming which requires the use of some random > >> > numbers. Basically I add a random value at one stage, and subtract > it a > >> > bit later. Something like this pseudo-code (where "x" is an existing > >> > variable): > >> > > >> > set rand_value <- (random) > >> > set new_value <- x + rand_value > >> > > >> > ... do stuff ... > >> > > >> > set new_value <- new_value - rand_value > >> > > >> > All values may be considered Reals. I tried to do this in Typed > Racket, > >> > where x was of type "Real" and got errors about mismatched types: > >> > "(random)" produces a "flonum". I was also using "set!" for the > >> > assignment of the random value, which I understand to be poor > practice: > >> > how would I do something like the above in a more "rackety" manner? > >> > Thank you! > >> > > >> > -- > >> > You received this message because you are subscribed to the Google > Groups > >> > "Racket Users" group. > >> > To unsubscribe from this group and stop receiving emails from it, > send an > >> > email to [email protected]. > >> > For more options, visit https://groups.google.com/d/optout. > > > > -- > > You received this message because you are subscribed to the Google > Groups "Racket Users" group. > > To unsubscribe from this group and stop receiving emails from it, send > an email to [email protected]. > > For more options, visit https://groups.google.com/d/optout. > > -- > You received this message because you are subscribed to the Google Groups > "Racket Users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.

