[racket] any remarks

From: Roelof Wobben (r.wobben at home.nl)
Date: Thu Apr 19 10:04:10 EDT 2012


As a exercise of the Htpd2e I made this script :

(define workspace (empty-scene 200 20))
(define cursor (rectangle 2 20 "solid" "red"))
(define textcolor "black")
(define textfont 11)

(define-struct editor (pre post))
; Editor = (make-editor String String)
; interp. (make-editor s t) means the text in the editor is
; (string-append s t) with the cursor displayed between s and t
; make-editor String String -> Editor
; editor-pre Editor -> String
; editor-post Editor -> String
; editor? Editor Any -> Boolean

; make-struct
(define verwerker (make-editor "abc" "def"))

; Editor -> Image
; Function which displays the struct on the screen into a image.
(define (render e)
;; verwerker  editor
;; (editor-pre verwerker) string
;; (editor-post verwerker) string
;; cursor  fixed image
;; workspace fixed image
(place-image (beside (text(editor-pre e) textfont textcolor) cursor 
(text(editor-post e) textfont textcolor ))25 10 workspace ))

;;NonEmptyString -> NonEmptyString
;; Function which takes the first character of a String.
;(check-expect (string-first "aaa") "a")
;(check-expect (string-first "a") "a")
(define (string-first s)
   (substring s 0 1))

;; NonEmptyString -> String
;; Function which takes the string expect the first character.
;(check-expect (string-rest "aaa") "aa")
;(check-expect (string-rest "a") "")
(define (string-rest s)
   (substring s 1 (string-length s)))

;; NonEmptyString -> NonEmptyString
;; Function who takes the last character of a string
;(check-expect (string-last "aaa") "a")
;(check-expect (string-last "a") "a")
(define (string-last s)
   (substring s (-(string-length s)1)))

;;NonEmptyString -> String
;; Function who takes the string except the last character.
;(check-expect (string-remove-last "aaa") "aa")
;(check-expect (string-remove-last "a") "")
(define (string-remove-last s)
   (substring s 0 (-(string-length s)1)))

;; Editor Key -> Editor
;; Function which change the world according to the pressed key.
(check-expect (edit verwerker "right") (make-editor "abcd" "ef"))
(check-expect (edit (make-editor "abc" "") "right") (make-editor "abc" ""))
(check-expect (edit (make-editor "" "def") "right") (make-editor "d" "ef"))
(check-expect (edit verwerker "left") (make-editor "ab" "cdef"))
(check-expect (edit (make-editor "" "def") "left") (make-editor "" "def"))
(check-expect (edit (make-editor "abc" "") "left") (make-editor "ab" "c"))
(check-expect (edit (make-editor "abc" "") "up") (make-editor "abc" ""))
(check-expect (edit  verwerker "\b") (make-editor "ab" "def"))
(check-expect (edit (make-editor "" "def") "\b") (make-editor "" "def"))
(check-expect (edit (make-editor "abc" "") "\b") (make-editor "ab" ""))
(check-expect (edit verwerker "\t") (make-editor "abc" "def"))
(check-expect (edit verwerker "\u007F") (make-editor "abc" "def"))
(check-expect (edit verwerker "z") (make-editor "abcz" "def"))
(check-expect (edit (make-editor "abc" "")"z") (make-editor "abcz" ""))
(check-expect (edit (make-editor "" "def")"z") (make-editor "z" "def"))
(define (edit e k)
      [  (and(key=? "right" k) (> (string-length (editor-post e)) 0)) 
(make-editor(string-append (editor-pre e) (string-first (editor-post e) 
)) (string-rest (editor-post e) ))]
      [  (and(key=? "left" k) (> (string-length (editor-pre e)) 0)) 
(make-editor ( string-remove-last (editor-pre e) ) (string-append 
(string-last(editor-pre e))  (editor-post e)))]
      [  (and (key=? "\b" k) (> (string-length (editor-pre e)) 0)) 
(make-editor (string-remove-last (editor-pre e)) (editor-post e))]
      [  (and  (key=? "\b" k) (equal? (string-length  (editor-pre e)) 
0)) e]
      [  ( or (key=? "\t" k) (key=? "\u007F" k)) e]
      [  ( > (string-length k) 1) e ]
      [else (make-editor (string-append (editor-pre e) k) (editor-post e))]

Are there any remarks so I can learn from it or it this script well 
according to the racket rules ?


Posted on the users mailing list.