PasteRack.org
Paste # 72412
2017-05-22 03:05:24

Fork as a new paste.

Paste viewed 133 times.


Embed:

  1. #lang racket
  2.  
  3. (require 2htdp/image)
  4. (require 2htdp/universe)
  5.  
  6.  
  7. ;; 5.9 Stucture in the World
  8.  
  9. ;; for world programs use stuctures to hold more than one piece of changing
  10. ;; data
  11.  
  12.  
  13. ;; data type to hold tank and ufo info.
  14.  
  15. (define-struct space-game [ufo tank])
  16. ; space-game is a (make-space-game Number Number)
  17. ; interp. a game state with ufo at y pos and a tank at x pos
  18.  
  19. (define GAME-STATE-1 (make-space-game 100 0))
  20.  
  21. ; Space-game -> ???
  22. #;; template
  23. (define (fn-for-space-game g)
  24.   (... (... (space-game-ufo g))
  25.        (... (space-game-tank g))))
  26.  
  27. ; A SpaceGame is a structure:
  28. ;   (make-space-game Posn Number).
  29. ; interpretation (make-space-game (make-posn ux uy) tx)
  30. ; describes a configuration where the UFO is
  31. ; at (ux,uy) and the tank's x-coordinate is tx
  32.  
  33.  
  34. ;; 5.10 A Graphical Editor
  35.  
  36.  
  37.  
  38. (define-struct editor [pre post])
  39. ; An Editor is a structure:
  40. ;   (make-editor String String)
  41. ; interpretation (make-editor s t) describes an editor
  42. ; whose visible text is (string-append s t) with
  43. ; the cursor displayed between s and t
  44.  
  45. (define ED1 (make-editor "" "")) ; Empty editor
  46. (define ED2 (make-editor "" "Hello world")) ; Cursor at beginning or buffuer
  47. (define ED3 (make-editor "He" "llo world")) ; Cursor after "e" in buffer
  48.  
  49.  
  50. ; Editor -> ???
  51. #;; template
  52. (define (fn-for-editor e)
  53.   (... (... (editor-pre e))
  54.        (... (editor-post e))))
  55.  
  56.  
  57. ;; Ex. 83:
  58. ;; Design the function render, which consumes an Editor and produces an image.
  59.  
  60. ;; graphical constants
  61. (define TEXT-SIZE 16)
  62. (define TEXT-COLOR "black")
  63.  
  64. (define BUFFER-LENGTH 200) ; length of buffer display
  65. (define BUFFER-HEIGHT 20)  ; height of buffer display
  66.  
  67. (define MT (empty-scene BUFFER-LENGTH BUFFER-HEIGHT))
  68.  
  69. (define CURSOR (rectangle 1 BUFFER-HEIGHT "solid" "red"))
  70.  
  71. ; try a mock-up
  72. (define MOCK-UP1 (overlay/align "left" "center"
  73.                                 (beside (text "hel" TEXT-SIZE TEXT-COLOR)
  74.                                         CURSOR
  75.                                         (text "lo world" TEXT-SIZE TEXT-COLOR))
  76.                                 MT))
  77.  
  78. ; Editor -> Image
  79. ; produce an Image given and Editor
  80.  
  81. (check-expect (render (make-editor "" ""))
  82.               (overlay/align "left" "center"
  83.                              (beside (text "" TEXT-SIZE TEXT-COLOR)
  84.                                      CURSOR
  85.                                      (text "" TEXT-SIZE TEXT-COLOR))
  86.                              MT))
  87.  
  88. (check-expect (render (make-editor "hel" "lo world"))
  89.               (overlay/align "left" "center"
  90.                              (beside (text "hel" TEXT-SIZE TEXT-COLOR)
  91.                                      CURSOR
  92.                                      (text "lo world" TEXT-SIZE TEXT-COLOR))
  93.                              MT))
  94.  
  95. ;(define (render ed) MT) ;stub
  96.  
  97. (define (render ed)
  98.   (overlay/align "left" "center"
  99.                  (beside (text (editor-pre ed) TEXT-SIZE TEXT-COLOR)
  100.                          CURSOR
  101.                          (text (editor-post ed) TEXT-SIZE TEXT-COLOR))
  102.                  MT))
  103.  
  104. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  105. ;; Ex. 84:
  106. ;; Design edit. The function consumes two inputs, an editor ed and
  107. ;; a KeyEvent ke, and it produces another editor. Its task is to add
  108. ;; a single-character KeyEvent ke to the end of the pre field of ed, unless ke
  109. ;; denotes the backspace ("\b") key. In that case, it deletes the character
  110. ;; immediately to the left of the cursor (if there are any). The function
  111. ;; ignores the tab key ("\t") and the return key ("\r").
  112.  
  113. ;; The function pays attention to only two KeyEvents longer than one letter:
  114. ;; "left" and "right". The left arrow moves the cursor one character to the
  115. ;; left (if any), and the right arrow moves it one character to the right (if
  116. ;; any). All other such KeyEvents are ignored.
  117.  
  118. ;; Develop a good number of examples for edit, paying attention to special
  119. ;; cases. When we solved this exercise, we created 20 examples and turned all
  120. ;; of them into tests.
  121.  
  122. ;; Hint Think of this function as consuming KeyEvents, a collection that is
  123. ;; specified as an enumeration. It uses auxiliary functions to deal with the
  124. ;; Editor structure. Keep a wish list handy; you will need to design
  125. ;; additional functions for most of these auxiliary functions, such as
  126. ;; string-first, string-rest, string-last, and string-remove-last. If you
  127. ;; haven’t done so, solve the exercises in Functions.
  128.  
  129. ; Editor KeyEvent -> Editor
  130. ; add or delete chars to the buffor or move the cursor around
  131. (check-expect (edit (make-editor "hel" "lo") "left")
  132.               (make-editor "he" "llo"))
  133. (check-expect (edit (make-editor "" "hello") "left")
  134.               (make-editor "" "hello"))
  135.  
  136. (check-expect (edit (make-editor "hel" "lo") "right")
  137.               (make-editor "hell" "o"))
  138. (check-expect (edit (make-editor "hello" "") "right")
  139.               (make-editor "hello" ""))
  140.  
  141. (check-expect (edit (make-editor "hel" "lo") "a")
  142.               (make-editor "hela" "lo"))
  143.  
  144. (check-expect (edit (make-editor "hel" "lo") "\b")
  145.               (make-editor "he" "lo"))
  146. (check-expect (edit (make-editor "" "hello") "\b")
  147.               (make-editor "" "hello"))
  148.  
  149. (check-expect (edit (make-editor "hel" "lo") "\t")
  150.               (make-editor "hel" "lo"))
  151. (check-expect (edit (make-editor "hel" "lo") "\r")
  152.               (make-editor "hel" "lo"))
  153.  
  154. ;(define (edit ed ke) (make-editor "" "")) ;stub
  155.  
  156. (define (edit ed ke)
  157.   (cond
  158.         [(key=? ke "left")  (cursor-left ed)]
  159.         [(key=? ke "right") (cursor-right ed)]
  160.         [(key=? ke "\b")    (delete-left ed)]
  161.         [(key=? ke "\t") ed]
  162.         [(key=? ke "\r") ed]
  163.         [(equal? (string-length ke) 1)
  164.          (add-right ed ke)]
  165.         [else ed])) ; ignore other key presses
  166.  
  167.  
  168. ;; edit string helper functions
  169.  
  170. ; Editor-> Editor
  171. ; move cursor left
  172. (check-expect (cursor-left (make-editor "hel" "lo")) (make-editor "he" "llo"))
  173. (check-expect (cursor-left (make-editor "" "hello")) (make-editor "" "hello"))
  174.  
  175. (define (cursor-left ed)
  176.   (cond [(string=? (editor-pre ed) "") ed]
  177.         [else
  178.          (make-editor (string-remove-last (editor-pre ed))
  179.                       (string-append (string-last (editor-pre ed))
  180.                                      (editor-post ed)))]))
  181.  
  182. ; Editor-> Editor
  183. ; move cursor right
  184. (check-expect (cursor-right (make-editor "hel" "lo")) (make-editor "hell" "o"))
  185. (check-expect (cursor-right (make-editor "hello" "")) (make-editor "hello" ""))
  186.  
  187. (define (cursor-right ed)
  188.   (cond [(string=? (editor-post ed) "") ed]
  189.         [else
  190.          (make-editor (string-append (editor-pre ed)
  191.                                      (string-first (editor-post ed)))
  192.                       (string-rest (editor-post ed)))]))
  193.  
  194. ; Editor -> Editor
  195. ; delete 1String to the left of the Cursor
  196. (check-expect (delete-left (make-editor "hel" "lo")) (make-editor "he" "lo"))
  197. (check-expect (delete-left (make-editor "" "hello")) (make-editor "" "hello"))
  198.  
  199. (define (delete-left ed)
  200.   (cond [(string=? (editor-pre ed) "")  ed]
  201.         [else
  202.          (make-editor (string-remove-last (editor-pre ed))
  203.                       (editor-post ed))]))
  204.  
  205. ; Editor 1String -> Editor
  206. ; add 1String to the right of the Cursor
  207. (check-expect (add-right (make-editor "hel" "lo") "d")
  208.               (make-editor "held" "lo"))
  209.  
  210. (define (add-right ed c)
  211.   (make-editor (string-append (editor-pre ed) c) (editor-post ed)))
  212.  
  213. ; string handling helpers
  214.  
  215. ; String -> 1String
  216. ; extracts the first 1String from a non-empty string.
  217. ; Don’t worry about empty strings. (ex. 13)
  218. (check-expect (string-first "s") "s")
  219. (check-expect (string-first "string") "s")
  220.  
  221. (define (string-first str)
  222.   (substring str 0 1))
  223.  
  224. ; String -> String
  225. ; extracts all but the first 1String from a String
  226. (check-expect (string-rest "b") "")
  227. (check-expect (string-rest "string") "tring")
  228.  
  229. (define (string-rest s)
  230.   (substring s 1 (string-length s)))
  231.  
  232. ; String -> 1String
  233. ; extracts the last 1String from a non-empty string.
  234. ; Don’t worry about empty strings. (ex. 14)
  235. (check-expect (string-last "d") "d")
  236. (check-expect (string-last "hello world") "d")
  237.  
  238. (define (string-last str)
  239.   (substring str (- (string-length str) 1)))
  240.  
  241. ; String -> String
  242. ; remove the last 1String in a non-empty String
  243. (check-expect (string-remove-last "b") "")
  244. (check-expect (string-remove-last "string") "strin")
  245.  
  246. (define (string-remove-last str)
  247.   (substring str 0 (sub1 (string-length str))))
  248.  
  249.  
  250. ;; Ex. 85:
  251. ;; Define the function run. It consumes a string, the pre field of an editor,
  252. ;; and launches an interactive editor, using render and edit from the preceding
  253. ;; two exercises for the to-draw and on-key clauses.
  254.  
  255. ; Editor -> Editor
  256. ; start with: (run (make-editor "hello world" "")
  257. (define (run ed)
  258.   (big-bang ed                   ; Editor
  259.             (to-draw   render)   ; Editor -> Image
  260.             (on-key    edit)))   ; Editor KeyEvent -> Editor
  261.  
  262.  
  263. ;; Ex. 86:
  264. ;; Notice that if you type a lot, your editor program does not display all of
  265. ;; the text. Instead the text is cut off at the right margin. Modify your
  266. ;; function edit from exercise 84 so that it ignores a keystroke if adding it
  267. ;; to the end of the pre field would mean the rendered text is too wide for
  268. ;; your canvas.
  269. ;; See editor2.rkt
  270.  
  271.  
  272. ;; Ex. 87:
  273. ;; Develop a data representation for an editor based on our first idea, using
  274. ;; a string and an index. Then solve the preceding exercises again. Re-trace
  275. ;; the design recipe. Hint if you haven’t done so, solve the exercises in
  276. ;; Functions.
  277. ;; See editor3.rkt

=>