How to make a picture using Racket - racket

I am new at using Racket and have a question that seems pretty simple.
Basically what I need to do is make a scene/picture using Racket. My first step is I want to have a moon, grass, and a background in the same picture.
Should/can I use E-SCENE for that or should I just use overlay?
I've been trying to figure this out for so long. Everytime I do it the moon and background come out as one and the background and grass show up as a different one. Here is what I have so far (not using empty-scene):
(require 2htdp/image)
(require 2htdp/universe)
(define rectangle1 (rectangle 450 400 "solid" "midnight blue"))
(define circle1 (circle 50 "solid" "WhiteSmoke"))
(place-image circle1 (/ 450 6) (* 400 .15) rectangle1)
(define rectangle2 (rectangle 450 30 "solid" "forest green"))
(place-image rectangle2 (/ 450 2) 400 rectangle1)
(define background (overlay rectangle2 circle1 rectangle1))

Don't think of place-image as an effect; think of it as a function that takes data and returns new data, just like + taking two numbers and returning a number. If you pretend for a moment that + only takes 2 numbers, then to add 3 numbers together with + you have to nest them:
(+ 1 (+ 2 3))
The place-image function only combines 2 images, so in the same way we nested the + calls above to combine 3 numbers, to combine 3 images together with place-image you have to nest the place-image calls:
(place-image image1 x1 y1
(place-image image2 x2 y2
image3))
For your code this means:
(place-image circle1 (/ 450 6) (* 400 .15)
(place-image rectangle2 (/ 450 2) 400
rectangle1))
Which combines the three images together to produce:
Similarly to combine even more things together, for numbers:
(+ 1 (+ 2 (+ 3 (+ 4 5))))
And for images:
(place-image image1 x1 y1
(place-image image2 x2 y2
(place-image image3 x3 y3
(place-image image4 x4 y4
image5))))
NOTE: For numbers you normally don't have to do this. (+ 1 2 3 4 5) works just as well as (+ 1 (+ 2 ...)).

Related

Understanding the question in a Racket programming assignment

The question is :
A univariate polynomial of order n is given by the following equation.
Pn (x) = anxn + . . . + a2x2 + a1x + a0
Here, ai are the coefficients of the polynomial, and x is its unique variable. You might implement a procedure poly-3 for computing the polynomial of order 3 of x as follows.
(define (poly-3 x a0 a1 a2 a3)
(+ a0 (* a1 x) (* a2 x x) (* a3 x x x)))
In poly-3, the coefficients and the variable are bundled together as arguments; and you would have to specify the coefficients each time you want to compute the same polynomial with different values of x.
Instead, implement the procedure make-poly-3 that generates a procedure that computes the polynomial for an arbitrary x.
(define (make-poly-3 a0 a1 a2 a3)
...)
(define my-poly-3
(make-poly-3 1 2 3 4))
(my-poly-3 2)
Next, write a function sum-poly-3-range which will sum up the results for calling my-poly-3 for the values in a range:
(define (sum-poly-3-range from to)
...)
(sum-poly-3-range 1 50)
I am not understanding what I need to do (I am not asking for the programming solution, just steps).
My confusions:
Can't understand the workflow or say the steps I need to follow.
How to pass coefficients for the polynomial? Should I generate randomly or should I use the constant values of a0, a1,a2,a3?
When looping through the range should I use that value as x?
make-poly-3 is a procedure which takes four arguments, and which will return another procedure. The values of the four arguments it takes will be the values of the coefficients to the polynomial.
The procedure it returns will take a single argument, which will be the value of x at which the polynomial is to be evaluated.
So, for instance
(define linear (make-poly-3 0 1 0 0))
> (linear 2)
2
> (define squared (make-poly-3 0 0 1 0))
> (squared 2)
4
The sum-poly-3-range function uses whatever value my-poly-3 has (it 'uses my-poly-3 free' to use a bit of jargon), and evaluates it for every integer in a range which you give it, and works out the sum of the results.
So, as a simple example:
> (define my-poly-3 (make-poly-3 1 0 0 0))
> (sum-poly-3-range 1 50)
50
This is because (make-poly-3 1 0 0 0) returns a polynomial function which evaluates to 1 for all arguments (the constant term is the only non-zero term).
And
> (define my-poly-3 (make-poly-3 0 1 0 0))
> (sum-poly-3-range 1 50)
1275
because this polynomial just squares its argument.

how to create a barplot in NetLogo where bars are side by side

I have two lists that I want to plot as sided bars:
[44.44 28.57 50 22.72 37.52]
[64.10 75 76.19 55.55 72.22]
I'd like to have a result as in the figure below, but I don't understand how to achieve this in netlogo, given the available primitives for plotting: "histogram" is definitely not useful here, while "plot" requires data to be a single value not a list.
This is really not a trivial task. First of all, you won`t get the x labels on the x-axis right, because NetLogo plots cannot handle categorical axes scales (the gap between groups cannot skipped). However, it is possible to create filled bar charts with NetLogo. I wrote a procedure some time ago and adapted it to your needs:
to go
ca
;; Define plot variables:
let y.a [44.44 28.57 50 22.72 37.52]
let y.b [64.10 75 76.19 55.55 72.22]
let plotname "plot 1"
let ydata (list y.a y.b)
let pencols (list 16 56)
let barwidth 2
let step 0.01
;; Call the plotting procedure
groupedbarplot plotname ydata pencols barwidth step
end
to groupedbarplot [plotname ydata pencols barwidth step]
;; Get n from ydata -> number of groups (colors)
let n length ydata
let i 0
;; Loop over ydata groups (colors)
while [i < n]
[
;; Select the current ydata list and compute x-values depending on number of groups (n), current group (i) and bardwith
let y item i ydata
let x n-values (length y) [? -> (i * barwidth) + (? * (((n + 1) * barwidth)))]
print y
print x
;; Initialize the plot (create a pen, set the color, set to bar mode and set plot-pen interval)
set-current-plot plotname
create-temporary-plot-pen (word i)
set-plot-pen-color item i pencols
set-plot-pen-mode 1
set-plot-pen-interval step
;; Loop over xy values from the two lists:
let j 0
while [j < length x]
[
;; Select current item from x and y and set x.max for current bar, depending on barwidth
let x.temp item j x
let x.max x.temp + (barwidth * 0.97)
let y.temp item j y
;; Loop over x-> xmax and plot repeatedly with increasing x.temp to create a filled barplot
while [x.temp < x.max]
[
plotxy x.temp y.temp
set x.temp (x.temp + step)
] ;; End of x->xmax loop
set j (j + 1)
] ;; End of dataloop for current group (i)
set i (i + 1)
] ;; End of loop over all groups
end
You may have to adjust the plotname within the goprocedure and then execute the go procedure to perform the plotting. One advantage of this method is, that it is quite flexible regarding the number of groups you want to plot next to each other. You just have to submit a list of y coordinates (one list for each group nested in one big list) and a corresponding list of pencolors for each group. You can just add more y lists and colors to plot more groups. The groupedbarplot procedure loops trough these groups and calculates matching x-coordinates (based on the number of groups and the barwidth). The plotting itself is nested inside another loop to plot the y coordinates repeatedly with increasing x coordinates (increment step). This creates many small bars next to each other in order to create the illusion of a filled barplot.

Matlab "diff" in F# (subtract element by element)

There is a diff function in MATLAB, that computes difference between elements in vector (or matrix, but it is not the case now).
X = [1 1 2 3 5 8 13 21];
Y = diff(X)
Results in: 0 1 1 2 3 5 8
I came up with F# solution:
let x =[1;1;2;3;5;8;13;21]
let diff x = List.map2 (-) (x|> List.tail) (x|> List.take ((x|>List.length) - 1))
diff x
which results in same List, but I feel there should be a better way how to do difference in F#? Is there?
There's List.pairwise : 'T list -> ('T * 'T) list which gives you a list of consecutive pairs of items.
let x =[1;1;2;3;5;8;13;21]
let diff x =
x |> List.pairwise |> List.map (fun (x, y) -> y - x)
Working with sequences instead of lists there is a compact solution:
let diff x = Seq.map2 (-) (Seq.skip 1 x) x
This would not work with lists because List.map2 requires its arguments to have the same length. Seq.map2 does not have that requirement.
For your specific case you could do:
[1;1;2;3;5;8;13;21] |> diff |> List.ofSeq
if you want the result to be a list.

The binary relation R is given. Construct the transitive and reflexive closure R * in LISP

The binary relation R is given. Construct the transitive and reflexive closure
R *. in LISP //// how come?
You didn't give much information, but typically, given a relation R, the transitive and reflexive closure of R is a relation R* defined as follows:
for all X,Y such that R(X,Y), the relation R*(X,Y) also holds;
for all X,Y,Z such that R(X,Y) and R*(Y,Z), the relation R*(X,Z) holds;
Let's say that you are given a function R that behaves as follows:
(funcall R :x X :y Y) returns a non-nil value iff R(X,Y)
(funcall R :x X) returns all Y such that R(X,Y)
(funcall R :y Y) returns all X such that R(X,Y)
(funcall R) returns a list of (X . Y) pairs for which R(X,Y).
Then you can build a function that computes the transitive and reflexive closure; if you want to know if R*(X,Z) holds, you start from X and try all possible Y that satisfy R(X,Y) until either Y is equal to Z, or you can recursively determine that R*(Y,Z).
After you implement and test that, try to detect cycles too.

Modular arithmetic AND Eucledian Algorithm

I was studying how to find the modular inverse. Suppose the example is:
27*x is congruent to 1 (mod 392) .
Now we have to find x. In the process we write this Equation as:
x is congruent to 27^(-1) (mod 392).
Here is my confusion does in modular arithmetic we can simply take 27 from left hand side and move it to right hand side and write it as 1/(27) (mod 392) without considering the 1 (mod 392) present their already and inserting 1/27 in between of 1 and (mod 392).
Because 27*x was congruent to 1(mod 392) but now we take x is congruent to 1/27 (mod 392).
This seems confused. If 27x = 1 (mod 392) then by definition x is 27^-1 (mod 392). You don't solve this equation by "moving" things from the left hand side to the right hand side. You solve it by using the Extended Euclidean Algorithm to write 27x + 392y = 1 in which case x is the inverse you seek since you can rearrange the equation as 392y = 1 - 27x which shows that 27x differs from 1 by a multiple of 392 hence 27x = 1 (mod 392)