【问题标题】:Racket doesn't print an image unless it returns to the REPL除非返回到 REPL,否则 Racket 不会打印图像
【发布时间】:2016-05-28 11:14:06
【问题描述】:

我正在使用 DrRacket 6.4,我想使用 2htdp/image 库显示图像。如果用户键入“开始”,将打印图像。但是,在尝试从循环内部显示图像时,我无法显示图像;似乎只有在程序“返回”时才会显示图像。

这行得通:

(define (showimg p) (place-image
   (triangle 32 "solid" "red")
   24 24
   p))

;; prints an image
(define (a p)
  (define command (read-line))
  (cond [(string=? command "start") (showimg p)]))

(a (empty-scene 160 90))

这不是:

;; should print an image indefinitely
(define (a p)
  (define command (read-line))
  (cond [(string=? command "start") (showimg p)])
  (a (empty-scene 160 90))
)

(a (empty-scene 160 90))

有什么方法可以使用递归(目前是这样),或者如果需要,可以通过其他方式在显示图像的同时连续获取用户输入?

谢谢。

【问题讨论】:

    标签: recursion scheme lisp racket read-eval-print-loop


    【解决方案1】:

    place-image 返回一个图像,它不显示一个。
    当函数返回图像时,显示由 REPL 完成。

    全部

    (cond [(string=? command "start") (showimg p)]) 
    

    正在做的是(可能)创建一个立即被丢弃的图像。

    您可以使用print 来显示图像,因为 DrRacket 在打印方面非常聪明。

    下面的效果我相信你在追求:

    (define (a p)
      (define command (read-line))
      (cond [(string=? command "start") (print (showimg p))])
      (a p))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-21
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 2020-07-03
      相关资源
      最近更新 更多