
@ragidhallak has joined the channel

Hello, how do i make a square for example with random size and color? i read a lot about randomizing in racket but didn’t find a way

How are you making squares now?

I am still in the basics, (square 10 “solid” “red”)

i am working on designing a simulation using DrRacket big-bang environment.

The most basic function to generate random numbers is random
.

(random 10)
will produce a random number between 0 (inclusive) and 10 (exclusive).

oh, it was this easy

(random 10 20)
will produce a number between 10 (inclusive) and 20 (exclusive).

So (square (random 10 20) “solid” “red”) will produce a square with a random size.

If you store colors in a list:

(define colors (list “red” “blue” “green”))

then you can use

(list-ref colors (random (length colors)))

to pick a random color.

If you are using it multiple times, define a function:

(define (random-color) (list-ref colors (random (length colors))))

I am getting the following error when i am trying to create (square (random 10 20) solid red:
random: expects only 1 argument, but found 2

and simply write (random-color).

Which language are you using?

Beginning student with list abbservations

Abbreviations*

(+ 10 (random 10))

Ok, then use (+ 10 (random 10)) to get a random number between 10 and 20.

The project that i am working is about a structure that got (size color type position)
i must write functions like tick-f (i did it) but i must create a function called key-f which takes list-of-shapes and keyEvent and produces a list-of-shapes. The function checks the key event. IfkeyEvent is \s" then create a new shape as \square" and produce random colored shape with random size (between 0–30) and random position where x is between [0,WIDTH] and y is between [0,HEIGHT]. If keyEvent \c" then create a new shape as “circle” or if keyEvent is “t” then create a new shape as “triangle”.

Constructors of the structure: (define b1 (make-shape 10 “red” “c” (make-posn 450 350))) (define b2 (make-shape 15 “green” “t” (make-posn 150 150))) (define b3 (make-shape 10 “purple” “c” (make-posn 250 200)))

Why can’t I use local-expand
with begin
to expand definitions? #lang racket/base
(require (for-syntax racket/base syntax/parse))
(define-syntax mutual
(syntax-parser
[(_ form ...)
(define expanded-forms
(local-expand #'(begin form ...) 'module '()))
#'(void)]))
(mutual
(define x 5))

I think the problem is with the context

If you use 'top-level
, it works fine

I thought 'module
means module-level context, which also allows definitions

okay, by replacing 'module
with 'top-level
I can have definitions. Mutually recursive definitions are still defeated :disappointed:

@shu—hung The gist of it is that define
outside of the top level is not actually a form that means anything in isolation… rather, enclosing forms (like #%module-begin
or let
) perform partial expansion to find definitions, then the enclosing forms themselves handle the definitions in some appropriate way.

If you want to do that same kind of partial expansion yourself, you can—indeed, forms like class
and unit
do do that in order to implement their custom definition contexts. This blog post of mine covers the gnarly details of doing such a thing “properly”: https://lexi-lambda.github.io/blog/2018/09/13/custom-core-forms-in-racket-part-ii-generalizing-to-arbitrary-expressions-and-internal-definitions/

ohh!! thanks a lot