using stop-when in racket

泪湿孤枕 提交于 2019-12-24 03:35:12

问题


I've been messing around with this program. It takes a number and adds 1 to it. I am wondering how exactly could you use stop-when here? For example, to make it stop at 5? I suppose a cond statement is necessary here. Thanks.

(require 2htdp/image)
(require 2htdp/universe)

(define (my-tick n)
(add1 n))

(define (my-render n)
  (text (number->string n) 36 "silver"))


(big-bang 1 (on-tick my-tick 2) (to-draw my-render))

回答1:


Give stop-when a predicate that consumes a world and returns true or false.

For more info, see the docs here.

Here's a version of your example that stops at 5:

(require 2htdp/image)
(require 2htdp/universe)

(define (my-tick n) (add1 n))

(define (my-render n)
  (text (number->string n) 36 "silver"))

(define (=5 n) (= n 5))

(big-bang 1 (on-tick my-tick 2) (to-draw my-render) (stop-when =5))


来源:https://stackoverflow.com/questions/18668773/using-stop-when-in-racket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!