# Wide-stripes function in Racket

The exercise is asking to make wider stripes similar to the previous exercise. I've included the exercise and code from it along with code that I've been messing with to get the desired result.

I know it can be done easily with loops, but the point of the exercise is to use what has been already introduced in the book. http://www.facom.ufu.br/~marcelo/PF/pictures_racket.pdf

I need a way to implement a series expression (5n+4) in order to get the result I desire.

I'm starting to think there isn't a way without looping or using if statements ect... (trying to use conditionals only is the goal here) I think what I have will suffice, but I know it's not right and the book will show a better way later on I assume.

I would like some feedback on the first exercise if there was a way to improve it and some critiquing on the wide-stripe logic as well.

The exercise or code isn't complete, but you can see where I'm going with it if you run the code. I'm missing a cleaner way to write it, but it hasn't struck me yet.

(require picturing-programs)
; builds an image black to red from left to right (see above lines 282-305)
(define (always-zero x y)
; x a number
; y a number
0
)
(define WIDTH 150)
(define HEIGHT 100)
; make-stripes : number number -> Image

; Takes in a width and height to create a shape
; with even and odd numbered stripes

(define (red-stripe x y)
(cond [(= (modulo (* y y) 2) 0) 255]
[else 0]))

(define (blue-stripe x y)
(cond[(= (modulo (* y y) 3) 1) 255]
[else 0]))

(define (make-stripes width height)
(build3-image width height red-stripe always-zero blue-stripe))

; another-make-stripe-function

(+ y 5))

(define (red-stripe2 x y)
[else 0]))

(define (blue-stripe2 x y)
(cond[(odd? (- 150 y)) 255]
[else 0]))

(define (make-wide-stripes width height)
(build3-image width height red-stripe2 always-zero blue-stripe2))

(build3-image 150 100 red-stripe2 always-zero blue-stripe2)

(define red (color 255 0 0 255))
(define blue (color 0 0 255 255))

(define total-rows
(/ HEIGHT 5)) ;20

(define (row y)
(+ y 5)) ;

(define (0-4? n)
(and (>= n 0)
(<= n 4)))

(define (5-9? n)
(and (>= n 5)
(<= n 9)))

(define (10-14? n)
(and (>= n 10)
(<= n 14)))

(define (15-19? n)
(and (>= n 15)
(<= n 19)))

(define (20-24? n)
(and (>= n 20)
(<= n 24)))

(define (25-29? n)
(and (>= n 25)
(<= n 29)))

(define (blue-or-red x y)
(cond [(0-4? y) blue]
[(5-9? y) red]
[(10-14? y) blue]
[(15-19? y) red]
[(20-24? y) blue]
[(25-29? y) red]
[else blue]))

(define (random-br-picture width height)
(build-image width height blue-or-red))

(build-image 150 100 blue-or-red)

• total-rows is invalid since HEIGHT isn't defined anywhere. Also always-zero is missing in this snippet, making it not runnable. If you wanna be nice to readers consider adding (require picturing-programs) at the top too, maybe #lang racket too, so it's a fully self-contained example. Jun 18, 2021 at 12:04
• @ferada okay fixed and added. I think I should brush up on recursive practices in order to grasp what needs to be done? And I don't know how I can implement total-rows in the program yet, but I know there need to be 20 rows for this height. I want to write it for any number though. Jun 18, 2021 at 12:53

To answer that first, you don't need anything complicated, just observe that the pattern is binary, on and off, and compare that with how odd and even numbers are following the same pattern, then apply that and scale by the strip width.

Once you see that, you can define it in multiple ways, one would be something like this:

(define (blue-or-red x y)
(cond [(odd? (quotient y 5)) red]
[else blue]))


Or you can of course use the modulo check again, basically defining your own odd? (or even?) predicate that way.

Note that cond is equivalent to an if here, you could write this as well:

(define (blue-or-red x y)
(if (odd? (quotient y 5)) red blue))


You wrote that you want things to make sense with any kind of image size: Simply ensure that everywhere you're referring to 150 or 100 you instead use the proper variables, WIDTH and HEIGHT and you should be all set. In general using uppercase like that is unusual for variable names though. Also you'd normally probably make these parameters to a custom function too, so that they're not globally defined (e.g. (define (make-my-bitmap width height) ...) and then pass them through).

While defining them as separate predicates is unnecessary, the number comparisons can also be simplified:

(<= 15 n 19)


This works because <= accepts one or more arguments and does what you'd expect it to.