Я новичок в Racket. Я задавал вопрос 1 от here. Ниже приведен код, который я мог бы сделать:Использование структуры в Racket
#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))
)
В интерпретатором я попытался это:
Я думал, что код, вероятно, будет хорошо, но все же некоторые сомнения возникли в мой ум:
в коде по мне, используя структуры (сделать-структура) должна было хорошо, но Accordin g для объяснения вопроса, я думаю экземпляр робота является результатом функции
initial-robot
. Используется ли структура?В функциях
robot-left
иrobot-north?
как я должен использоватьrobot1
в качестве аргумента? Установка глобальной переменной, которая сохраняет текущее направление объекта, может использоваться для упомянутых функций . Что мне здесь делать?
Любые предложения приветствуются. Спасибо!
Просто, чтобы вы знали, этот вопрос лучше подходит для http://codereview.stackexchange.com/ –