DrRacket:大爆炸;可以“画画”接受两个功能吗?

时间:2015-12-20 09:58:38

标签: racket

我正在制作闪卡程序。我希望我的大爆炸能够绘制两个功能:

;; Editor -> Image
;; consumes an Editor and produces an image of the pre, a cursor and the post.
    (define (editor-render ed)
       (overlay/align "left"
                      "bottom"
                      (beside (textify (implode (rev (editor-pre ed))))
                               CURSOR
                               (textify (implode (editor-post ed))))
                       SCENE))

;;List-of-words -> Image
;; consumes a list-of-words and produces an image 
    (define (list-render low)
           (above/align "left"
                 (textify2 (first (first low)))))

;; String, List-of-words -> Editor
;; launches the editor given some initial string
    (define (main s low)
         (big-bang (create-editor s "")
         (on-key editor-kh)
         (to-draw (and editor-render (list-render low)))))

单词列表的例子如下:

(cons (cons "hello" (cons "bonjour" empty)) empty)

第一个单词是需要由用户翻译的单词(使用文本编辑器功能{这里不是全部显示})

因此,我想绘制绘制需要翻译的单词的递归,同时允许用户输入。但我尝试的一切都没有奏效......

1 个答案:

答案 0 :(得分:1)

  1. editor-renderlist-render会产生不同的图像,这是没有意义的。实际上你只想要一个图像(可能是几个图像组合在一起),而不是同时显示很多图像。
  2. to-draw必须是一个函数,但你提供的(and ...)Boolean,所以这肯定不起作用!
  3. 我一直认为这样可以简化事情:

    World成为动画的状态,

    • to-draw中的功能消耗World并生成Image
    • on-key中的功能会消耗WorldKey并生成World
    • on-tick中的功能消耗World并生成World
    • stop-when中的功能消耗World并生成Boolean

    请注意,所有World必须属于同一类型。在您的情况下,您尝试拥有两个不同的世界(不同的类型),因此它不起作用,因为您只能拥有一个World。如何解决问题?你可以将两个世界结合在一起,以获得一个新的大世界。例如,您可以定义:

    (define-struct world (editor words))
    

    现在,您可以创建包装器来调用已编写的函数:

    ;; my-on-key : World, Key -> World
    (define (my-on-key a-world key)
      (make-world (editor-kh (world-editor a-world) key) (world-words a-world)))
    
    ;; do the same for on-tick, etc. if you have them
    
    (big-bang (on-key my-on-key) ...) ;; use the new wrappers instead
    

    回到绘图功能,您可以做的是,您将从Imagelist-render提供editor-render,然后editor-render会在改为输入图像的顶部。

    ;; editor-render : Editor, Image -> Image
    ;; Draw things on a-image instead of constructing everything from blank
    (define (editor-render ed a-image) ...)
    

    现在,您可以创建一个包装来绘制图像:

    ;; my-to-draw : World -> Image
    (define (my-to-draw a-world)
       (editor-render (world-editor a-world) (list-render (world-words a-world))))
    
    (big-bang ... (to-draw my-to-draw)) ;; use the new wrapper instead