Scheme struct在球拍中的应用

Scheme struct在球拍中的应用,scheme,racket,Scheme,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

我是个球拍新手。我在试着回答第一个问题。以下是我可以编写的代码:

#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))
)
#朗球拍
(需要2htdp/图像)
(需要机架单元)
(需要机架单元/文本用户界面)
(需要“额外的rkt”)
(定义当前目录“n”)
(定义curr-x 30)
(定义curr-y 40)
;; 我想到的结构
(定义结构机器人(x y方向))
(定义irobot(使机器人curr-x curr-y curr dir))
(定义最大高度200)
(定义最大宽度400)
(定义(渲染机器人)
(地点图像)
(裁剪14 0 10 20(圆圈10“实心”“蓝色”))
19 10
(圈10“实心”“红色”))
)
(定义(繁殖机器人x y方向)
(地点图像)
(续)
[(字符串=?“n”方向)(旋转90(渲染机器人))]
[(字符串=?“e”方向)(渲染机器人)]
[(字符串=?“w”方向)(旋转180(渲染机器人))]
[(字符串=?“s”方向)(旋转270(渲染机器人))]
)
xy
(空场景最大宽度最大高度)
)
)
(定义(初始机器人x y)

(如果(和(您正确地认为结构是更好的选择:

1) 在你的代码中,你不会被限制为一个机器人,而且

2) 你将以一种功能性的方式进行编程,这正是作业想要的

因此,使用机器人结构:

(define-struct robot (x y direction))
确保为结构提供了正确的数据定义

;; A Robot is a (make-robot x y direction), where:
;; - x is an integer
;; - y is an integer
;; - direction is a string
尽管如此,我还是建议对
方向使用符号而不是字符串


(机器人左)

(机器人北?


现在,要将这些函数合并到您的代码中,您需要确保将数据和输出图像的概念分开,而您目前没有这样做

(初始robot)
根本不应该渲染。它应该只返回robot的一个实例,正如我们在数据定义中定义的那样

注意这个作业中给出的规范不需要你渲染。这将是一个单独的任务。他们要求你定义的所有函数都严格地处理数据。然后,你可以考虑渲染来直观地测试你的函数,作为一个额外的单元测试,你也应该为每个函数创建。


我提供给您的代码应该是了解如何设计其余函数的良好起点。在结束之前不要担心渲染!不要忘记作业中提到
Robot
的每个签名都引用了我们为Robot str创建的数据定义uct.

正如您所知,这个问题更适合于
;; Turns a robot to the left.
;; Robot -> Robot
(define (robot-left robot-obj)
  (make-robot (robot-x robot-obj)
              (robot-y robot-obj)
              "w"))
;; Does robot-obj face north?
;; Robot -> Boolean
(define (robot-north? robot-obj)
  (string=? (robot-direction robot-obj) "n"))