ragidhallak
2018-12-15 13:26:47

@ragidhallak has joined the channel


ragidhallak
2018-12-15 13:29:00

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


soegaard2
2018-12-15 13:29:32

How are you making squares now?


ragidhallak
2018-12-15 13:29:54

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


ragidhallak
2018-12-15 13:30:20

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


soegaard2
2018-12-15 13:30:53

The most basic function to generate random numbers is random.


soegaard2
2018-12-15 13:31:23

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


ragidhallak
2018-12-15 13:31:41

oh, it was this easy


soegaard2
2018-12-15 13:31:58

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


soegaard2
2018-12-15 13:32:22

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


soegaard2
2018-12-15 13:32:35

If you store colors in a list:


soegaard2
2018-12-15 13:32:46

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


soegaard2
2018-12-15 13:32:49

then you can use


soegaard2
2018-12-15 13:33:00

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


soegaard2
2018-12-15 13:33:06

to pick a random color.


soegaard2
2018-12-15 13:33:20

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


soegaard2
2018-12-15 13:33:35

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


ragidhallak
2018-12-15 13:33:36

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


soegaard2
2018-12-15 13:33:44

and simply write (random-color).


soegaard2
2018-12-15 13:34:01

Which language are you using?


ragidhallak
2018-12-15 13:34:37

Beginning student with list abbservations


ragidhallak
2018-12-15 13:34:46

Abbreviations*


andreiformiga
2018-12-15 13:35:04

(+ 10 (random 10))


soegaard2
2018-12-15 13:35:05

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


ragidhallak
2018-12-15 13:37:24

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”.


ragidhallak
2018-12-15 13:37:42

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)))


shu--hung
2018-12-16 04:18:50

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))


sorawee
2018-12-16 04:22:09

I think the problem is with the context


sorawee
2018-12-16 04:22:20

If you use 'top-level, it works fine


shu--hung
2018-12-16 04:24:25

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


shu--hung
2018-12-16 04:25:19

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


lexi.lambda
2018-12-16 06:08:56

@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.


lexi.lambda
2018-12-16 06:10:37

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/


shu--hung
2018-12-16 06:15:23

ohh!! thanks a lot