子字符串:开始索引超出范围,并伴有大爆炸



"图片节目";http://www.facom.ufu.br/~marcelo/PF/pictures_racket.pdf是我正在写的书,第10章。我试图以线性的方式使用书中介绍的内容。当鼠标移动或点击时,该程序会将字符串的第一个字符剪掉,但我找不到方法阻止它抛出超出范围的错误。

不幸的是,我尝试了几种不同的方法,花了几个小时。这必须在没有条件、循环、尚未提及的内置函数的情况下完成;等等。

任何提示都将不胜感激。

以下是练习题。

(require picturing-programs)
(define (add-str s)
(string-append  s "b"))
(define (a-with-b s)
(text s 18 "green"))
(define (chop-first-on-mouse s mouse-x mouse-y me)
(substring s 1 (string-length s)))
(define (string-world s)
(big-bang s
(check-with string?)
(on-tick add-str 1/2)
(on-draw a-with-b 200 200)
(on-mouse chop-first-on-mouse)))
(string-world  "a")

因此,为了改进程序,在函数定义中添加了一段代码(鼠标上先砍…(,这就是内置函数(min(。这是获得所需结果所需的唯一更改。

这似乎给出了期望的输出,因为对";先在老鼠身上砍";现在一次只删除一个字符,而且不会抛出错误(始终保持在子字符串参数的范围内(。

而在原始调用"0"之前;先在老鼠身上砍";会超出范围,也会删除字符过快而无法分析s/z'e。

(require picturing-programs)
(define (add-str s)
(string-append  s "b"))
(define (a-with-b s)
(text s 18 "green"))
# Only change to program is the built-in function (min) in the substring call below 
(define (chop-first-on-mouse s mouse-x mouse-y me)
(substring s (min 1 (string-length s))))
(define (string-world s)
(big-bang s
(check-with string?)
(on-tick add-str 1/2)
(on-draw a-with-b 200 200)
(on-mouse chop-first-on-mouse)))
(string-world  "a" )

最新更新