Animesh Pandey
Animesh Pandey

Reputation: 6018

Using struct in Racket

I am a newbie in Racket. I was trying question 1 from here. Following is the code that I could make :

#lang racket

(require 2htdp/image)

(require rackunit)
(require rackunit/text-ui)
(require "extras.rkt")

(define curr-dir "n")
(define curr-x 30)
(define curr-y 40)

;; Structure that I thought about
(define-struct robot (x y direction))
(define irobot (make-robot curr-x curr-y curr-dir))

(define MAX_HEIGHT 200)
(define MAX_WIDTH  400)

(define (render-robot)
(place-image
(crop 14 0 10 20 (circle 10 "solid" "blue"))
19 10
(circle 10 "solid" "red"))
)

(define (spawn-robot x y direction)
(place-image
(cond 
    [(string=? "n" direction) (rotate 90 (render-robot))]
    [(string=? "e" direction) (render-robot)]
    [(string=? "w" direction) (rotate 180 (render-robot))]
    [(string=? "s" direction) (rotate 270 (render-robot))]
    )
x y
(empty-scene MAX_WIDTH MAX_HEIGHT)
)
)

(define (initial-robot x y)
(if (and (<= x (- MAX_HEIGHT 10)) (<= y (- MAX_WIDTH 10)))
    (spawn-robot x y curr-dir)
    (error "The placement of the robot is wrong!")
    )
)

(define robot1 (initial-robot curr-x curr-y))

;;-----------------------------------------------------;;

;; Doubt Here (Make the robot turn left)
(define (robot-left robot-obj)
(set! curr-dir "w") ;; Hard coded for north direction
(initial-robot curr-x curr-y)
)

;; Doubt Here (Function checks whether robot is facing north or not.)
(define (robot-north? robot-obj)
(if (string=? "n" curr-dir) (true) (false))
)

In the interpreter I tries this out:

Interpreter Snap

I was thinking that the code is probably going fine but still some doubts cropped up in my mind:

  1. In the code according to me using a Structure (make-struct) should have been good but according to explanation of the question I think the instance of robot is the result of the function initial-robot. Is using a Structure feasible?

  2. In the functions robot-left and robot-north? how should I use robot1 as the argument? Setting a global variable that stores the current direction of the object can to used for the functions mentioned. What should I do here?

Any suggestion is welcome. Thanks!

Upvotes: 0

Views: 961

Answers (1)

ಠ_ಠ
ಠ_ಠ

Reputation: 3078

You are correct that a struct would be a better choice:

1) You wouldn't be limited to a single robot in your code, and

2) You would be programming in a functional manner, which is what the assignment wants.

So, with your robot struct:

(define-struct robot (x y direction))

Make sure you give a proper data definition to the struct.

;; A Robot is a (make-robot x y direction), where:
;; - x is an integer
;; - y is an integer
;; - direction is a string

Although, I'd recommend using symbols instead of strings for direction.


(robot-left):

;; Turns a robot to the left.
;; Robot -> Robot
(define (robot-left robot-obj)
  (make-robot (robot-x robot-obj)
              (robot-y robot-obj)
              "w"))

(robot-north?):

;; Does robot-obj face north?
;; Robot -> Boolean
(define (robot-north? robot-obj)
  (string=? (robot-direction robot-obj) "n"))

Now, to incorporate these functions into your code, you need to make sure that you separate the idea of data and output images, which you currently don't.

(initial-robot) should not render at all. It should just return an instance of a Robot, as defined in our data definition.

Notice that the spec given in this homework assignment does not require you to render at all. This would be a separate undertaking. All functions that they ask you to define strictly deal with data. Afterwards, you can consider rendering to test your functions visually, as an extra to the unit tests you should also create for each function.

The code that I've provided you should be a good starting point for figuring out how you would design the rest of your functions. Don't worry about rendering until the end! Don't forget that every signature given in the homework assignment that mentions Robot is referencing the data definition that we have created for our robot struct.

Upvotes: 2

Related Questions