- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个问题。它很长。
首先,这里是这个的数据定义
(define-struct ball (x y color))
;; Ball = (make-ball Number Number Color)
;; Color is one of 'red, 'yellow, 'blue, etc.
(require 2htdp/image)
(require 2htdp/universe)
;;An LoB is one of
;;--empty
;;--(cons ball LoB)
;;(define (ball-template lob)
;; (cond
;; [(empty? lob) ...]
;; [else
;; (first lob)...
;; (ball-template (rest lob))]))
;;lob-length : LoB -> number
;;Counts the balls in the list
(define (lob-length lob)
(cond
[(empty? lob) 0]
[else
(add1 (lob-length (rest lob)))]))
;;Examples
(check-expect (lob-length empty) 0)
(check-expect (lob-length(cons (make-ball 1 2 "red")
(cons(make-ball 3 3 "blue")
(cons(make-ball 5 86 "white")empty))))3)
;;lob-draw : LoB -> Scene
;;Adds the balls to an empty scene in the form of circles
(define (lob-draw lob)
(cond
[(empty? lob) (empty-scene 300 300)]
[else
(place-image (circle 3 "solid" (ball-color (first lob)))
(ball-x (first lob))
(ball-y (first lob))
(lob-draw (rest lob)))]))
;;Examples
(lob-draw empty)
(lob-draw (cons (make-ball 50 60 "red")
(cons(make-ball 20 15 "blue")
(cons(make-ball 5 200 "black")empty))))
;;lob-member? LoB, ball -> boolean
;;Checks to see if the ball is in the list
(define (lob-member? lob b)
(cond
[(empty? lob) false]
[(same-ball? b (first lob)) true]
[else (lob-member? (rest lob) b)]))
;;Examples
(check-expect (lob-member? empty (make-ball 300 70 "blue"))
false)
(check-expect (lob-member? (cons (make-ball 30 70 "blue")
(cons (make-ball 310 500 "black")
(cons (make-ball 30 340 "yellow") empty)))
(make-ball 310 500 "black")) true)
;;same-ball? ball ball -> boolean
;;Compares two balls
(define (same-ball? b1 b2)
(and (= (ball-x b1) (ball-x b2))
(= (ball-y b1) (ball-y b2))
(string=? (ball-color b1) (ball-color b2))))
;;Example
(check-expect (same-ball? (make-ball 30 30 "white")(make-ball 30 30 "white"))
true)
(check-expect (same-ball? (make-ball 30 30 "white")(make-ball 23 40 "black"))
false)
lob-yellow
,它改变了
Balls
列表中所有球的颜色到黄色。我猜我需要 cond,但我不知道该怎么做。有什么想法吗?
最佳答案
假设结构是不可变的,这里有一些提示可以帮助您开始,填写空白:
(define (lob-yellow lob)
(cond [<???> ; if the list is empty
<???>] ; return the empty list
[else ; otherwise,
(cons (make-ball ; cons a new ball, build it with:
(<???> (first lob)) ; the x coordinate of the first ball
(ball-y <???>) ; the y coordinate of the first ball
<???>) ; and always the yellow color
(lob-yellow <???>))])) ; recur over the rest of the list
(define-struct ball (x y color) #:mutable) ; now the struct is mutable
(define (lob-yellow lob)
(cond [<???> ; if the list is empty
<???>] ; return the empty list
[else ; otherwise,
(set-ball-color! <???> 'yellow) ; set the color of the first ball
(lob-yellow <???>)])) ; recur over the rest of the list
关于scheme - 数据定义 DrRacket?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14843770/
我正在将 Racket 和 DrRacket 用于一个小项目(我正在尝试学习两者并使用它们来教我的 friend 一些函数式编程)。 我刚刚发现了自动完成功能 (C-/),它与库函数一起工作得很好,但
我正在使用 DrRacket 调试使用 R5RS 语言的方案代码。我正在尝试调试使用加载函数导入的多个文件。我按照 Dr Racket 网站上的说明打开了这两个文件,但是在调试时,其他文件中的函数被跳
我有这个问题。它很长。 首先,这里是这个的数据定义 (define-struct ball (x y color)) ;; Ball = (make-ball Number Number Col
因此,我正在尝试在scheme中编写以下函数,并使其能够在DrRacket上运行。问题如下, make5 -接受两个整数,并返回一个5位数的整数,该整数由第一个输入的最右3位和第二个输入的最左2位构成
如何通过组合较长列表(例如 4 个元素)中的任意两个元素来生成序列列表? 例如,我想获取 '(1 2), '(1 3), '(1 4), '(2 3)、'(2 4)和基于'(1 2 3 4)的'(3
我目前在 Mac OS X 上使用 DrRacket 并选择语言“R5RS”,但是当我输入时 (random 100) 我收到错误消息: reference to undefined identifi
我希望有人能指导我正确的方向:我正在寻找两个在两个列表中生成所有可能的项目组合: 示例: 给定列表 '(symbol1 symbol2) 和 '(1 2),我希望生成: (list (list 'sy
在链接 http://www.ccs.neu.edu/home/ryanc/macro-stepper/tutorial.html 上有使用宏步进器的说明。 但是,当我要尝试时,我无法在非零定义中获得
所以我正在为我的编程语言课做一些练习题,其中一项作业是创建一个脚本“MyEval”,它允许您进行简单的嵌套加法和乘法。因此,例如程序将能够执行此 (MyEval '(1 +(3 *4))) 或更深入的
我想让 DrRacket 的默认启动画面成为他们用于情人节的特殊启动画面。将它显示在屏幕上的一种方法是将计算机上的时间更改为 2 月 14 日。如何在不更改时间的情况下将其设置为默认启动画面? 在这里
我想显示文本然后覆盖它(对于简单的进度条等)。在 Linux 和 Windows 的终端中运行 Racket 时,显示回车 ( "\r" ) 的常用技巧工作正常,但在 DrRacket 和 GRack
我用初级学生语言编写了一个小模块。当我单击“运行”时,“定义”窗口中的某些表达式以反白显示(黑色背景上的红色文本)。我无法弄清楚选择了哪些表达方式或反向视频的含义。 最佳答案 根据颜色偏好,它表示测试
我是一名大学生,想通过“The Scheme编程语言”作为自学。 这是一个简单的程序,我将其命名为“reciprocal.ss” (define reciprocal (lambda (n) (if(
如何为 DrRacket 插件编写软件测试?考虑到插件本质上是 Racket 中的一个单元,它不能像函数一样简单地调用。 最佳答案 您可以在此处使用几个选项。 创建一个虚拟单元来链接——基本上是一个“
方括号在按钮“P”旁边和“Enter”之前。所以我想更改它们,以便可以将它们用作括号而不是方括号。 这样打字更容易。 我也找到了答案,但提前让人们知道。 偏好 -> 编辑 -> 方案 或 Racket
我最近一直在使用一些函数,我有一个适用于某些情况的函数,我需要一个通用函数。 我需要一个正则表达式,以便它准确地找到我正在寻找的东西,而不仅仅是它的痕迹。 我有这个,例如 >(regexp-repla
Racket 编程语言的核心是什么?核心是基于任何带有所有附加功能的 RnRS 规范,例如基于该微小核心的众多序列方法,还是语言核心的引用部分中的所有内容? 最佳答案 Racket 建立在由 #%ke
我的兄弟想向我介绍 lisp 语言,所以他在他的电脑上向我展示了 DrRacket。一切看起来都很棒,包括 arrows指向导入函数的位置,以及 documentation bubble告诉你一个函数
考虑这个片段: #lang racket (define (f i) (if (> i 5) 0 (+ 1 (f (+ 1 i))))) 在这里,对 f 的调用不在尾部位置
赞 Racket - how to get the “previous executed command” in bash script?但是有了 DrRacket。如何在 DrRacket 中获取先
我是一名优秀的程序员,十分优秀!