How can i run this function/

Hello,i have just bought this book called "realm of racket" really good book by the way ,i have learned a lot but i came across a code related to local definitions,i understand the purpose of the code and how it works but i cannot seem to get it to run,i try but i keep getting errors,i understand it takes two arguments.,though i don't know how to apply posn to the function.Here is the code

(struct posn ( x y))
(struct rectangle (width height))
(define (inside-of-rectangle? r p)
(define x (posn-x p))
(define y (posn-y p))
(define width (rectangle-width r))
(define height (rectangle-height r))
(and (<= 0 x) (< x width ) (<= 0 y) (< y height)))

i have tried running it in different ways but the problem persists can someone help me understand what i am doing wrong many thanks

(inside-of-rectangle? (posn? x))

. . x: undefined;
cannot reference an identifier before its definition

(inside-of-rectangle? (make-posn-x 1))
. . make-posn-x: undefined;
cannot reference an identifier before its definition
(inside-of-rectangle? (make-posn 1))
. . make-posn: undefined;
cannot reference an identifier before its definition
(inside-of-rectangle? (posn-x 4))
. . posn-x: contract violation
expected: posn?
given: 4
(inside-of-rectangle? (posn ( 2 5)))
. . application: not a procedure;
expected a procedure that can be applied to arguments
given: 2
(inside-of-rectangle? (posn? ( 2 5)))
. . application: not a procedure;
expected a procedure that can be applied to arguments
given: 2
(inside-of-rectangle? (posn? 3))
. . inside-of-rectangle?: arity mismatch;
the expected number of arguments does not match the given number
expected: 2
given: 1
(inside-of-rectangle? (posn? 3 4))
. . posn?: arity mismatch;
the expected number of arguments does not match the given number
expected: 1
given: 2
(inside-of-rectangle? (posn 3 4))
. . inside-of-rectangle?: arity mismatch;
the expected number of arguments does not match the given number
expected: 2
given: 1
(inside-of-rectangle? (posn 3 4) (rectangle 50 100))
. . posn-x: contract violation
expected: posn?
given: #
(inside-of-rectangle? (posn 3 4) (posn-x 4 5(rectangle 50 100)))
. . posn-x: arity mismatch;
the expected number of arguments does not match the given number
expected: 1
given: 3
(inside-of-rectangle? (posn 3 4) (posn-x 4 5)(rectangle 50 100))
. . posn-x: arity mismatch;
the expected number of arguments does not match the given number
expected: 1
given: 2
(inside-of-rectangle? (posn-x 4 5)(rectangle 50 100))
. . posn-x: arity mismatch;
the expected number of arguments does not match the given number
expected: 1
given: 2
(inside-of-rectangle? (posn-x 4 ) (posn-y 5))
. . posn-x: contract violation
expected: posn?
given: 4
(inside-of-rectangle? (posn (posn-x 4 ) (posn-y 5)))
. . posn-x: contract violation
expected: posn?
given: 4
(inside-of-rectangle? (posn (posn 4 ) (posn 5)))
. . posn: arity mismatch;
the expected number of arguments does not match the given number
expected: 2
given: 1
(inside-of-rectangle? (posn (posn 4 5) (posn 5)))
. . posn: arity mismatch;
the expected number of arguments does not match the given number
expected: 2
given: 1
(inside-of-rectangle? (posn (posn 4 5) (posn 5 6)))
. . inside-of-rectangle?: arity mismatch;
the expected number of arguments does not match the given number
expected: 2
given: 1
(inside-of-rectangle? (posn (posn 4 5) (posn 5 6)) (rectangle 40 50))
. . posn-x: contract violation
expected: posn?
given: #
(inside-of-rectangle? (posn (posn 4 5) (posn 5 6)) ((rectangle 40 50)))
. . application: not a procedure;
expected a procedure that can be applied to arguments
given: #
(posn-x 4)
. . posn-x: contract violation
expected: posn?
given: 4
(posn? 4)
#f
(posn-x p)
. . p: undefined;
cannot reference an identifier before its definition
(posn-x x)
. . x: undefined;
cannot reference an identifier before its definition
(posn-x x)
. . x: undefined;
cannot reference an identifier before its definition
(make-posn 3 4)
. . make-posn: undefined;
cannot reference an identifier before its definition

(inside-of-rectangle? (rectangle 10 20) (posn 1 2))
  1. I notice is that you are calling inside-of-rectangle with 1 argument, but you have specified two r p. So you should make sure to call the function with the correct number of arguments.
  2. The racket reference on structs is a little intimidating but basically what it says is that when you define a struct using (struct my-struct (some-val)) you get a bunch of definitions that are automatically created for you. So using the posn example you get
(struct posn (x y))
(define new-posn (posn 10 10))
(posn-x new-posn)
(posn-y new-posn)
(posn? new-posn) 
  1. The reason you are getting x: undefined when you call (inside-of-rectangle? (posn? x)) is because before that line in your code you didn't create a variable called x using (define x my-value) Whenever you see the undefined error it means the variable doesn't exist.
  2. (inside-of-rectangle? (posn (posn 4 5) (posn 5 6))) is an arity mismatch error. What this means is you tried to call a function with the wrong number of arguments. That is what it means by arity. If I create a function that takes 2 arguments, but only provide 1 I will get a arity mismatch.
  3. posn-x: contract violation expected: posn? A contract violation means that you broke Racket's expectation for what value should be passed into that function. In that case Racket tells you. posn-x expected a posn? which means, it expected anything that would return true if posn? was called with that value. I.E (posn? my-posn)

You are very close with (inside-of-rectangle? (posn (posn 4 5) (posn 5 6)) (rectangle 40 50)) You are creating you rectangle struct correctly, but the posn struct is being created incorrectly. look at how you are constructing the rectangle struct to see how you should construct the posn one.

1 Like