[racket] what's the best way to make auxilery functions here.

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Jul 5 16:34:26 EDT 2012

A good piece of code is like a love song you write for your girl friend. 
If it isn't good enough for her, don't serenade to her. It won't work. 

If your code isn't good enough to read for Racketeers (as in visual appearances)
don't present it to the list. 

So first, here is how you would write the function if you followed the (implicit) style 
rules of the book. Please try in the future; it is really difficult for my soul and 
heart to look at your code. 

; Vzoo -> Vzoo
; function which alters the happiness and x-coordinate based on the ticks of the computer.

(check-expect (tock (make-VZoo (make-VCat 500 5 "rechts") (make-VCham 450 5 "rechts")))
              (make-VZoo (make-VCat 503 4.9 "rechts")(make-VCham 453 4.9 "rechts")))
(check-expect (tock (make-VZoo (make-VCat 500 5 "rechts") (make-VCham 1032 5 "rechts")))
              (make-VZoo (make-VCat 503 4.9 "rechts")(make-VCham 1032 4.9 "links")))
(check-expect (tock (make-VZoo (make-VCat 500 5 "rechts") (make-VCham 1032 5 "links")))
              (make-VZoo (make-VCat 503 4.9 "rechts") (make-VCham 1029 4.9 "links")))
(check-expect (tock (make-VZoo (make-VCat 500 5 "rechts") (make-VCham 500 5 "links")))
              (make-VZoo (make-VCat 503 4.9 "rechts")(make-VCham 497 4.9 "links")))

(define (tock s)
    [(and (< (VCham-X (VZoo-VCham s)) bovengrens-cham)
          (< (VCat-X (VZoo-VCat s)) bovengrens-cat)
          (string=? (VCham-R (VZoo-VCham s)) "rechts") 
          (string=? (VCat-R (VZoo-VCat s)) "rechts"))
      (make-VCat (+ (VCat-X (VZoo-VCat s)) 3) (- (VCat-H (VZoo-VCat s)) 0.1) "rechts")
      (make-VCham (+(VCham-X (VZoo-VCham s)) 3) (-(VCham-H (VZoo-VCham s)) 0.1) "rechts"))]
    [(and (> (VCham-X (VZoo-VCham s)) bovengrens-cham )
          (< (VCat-X (VZoo-VCat s)) bovengrens-cat)
          (string=? (VCham-R (VZoo-VCham s)) "rechts")
          (string=? (VCat-R (VZoo-VCat s)) "rechts"))
     (make-VZoo (make-VCat (+ (VCat-X (VZoo-VCat s) 3)  (- (VCat-H (VZoo-VCat s)) 0.1) "rechts"))
                (make-VCham (VCham-X (VZoo-VCham s)) (-(VCham-H (VZoo-VCham s)) 0.1) "links"))]
    [(and (> (VCham-X (VZoo-VCham s)) ondergrens-cham )
          (< (VCat-X (VZoo-VCat s)) bovengrens-cat)
          (string=? (VCham-R (VZoo-VCham s)) "links")
          (string=? (VCat-R (VZoo-VCat s)) "rechts"))
     (make-VZoo (make-VCat (+ (VCat-X (VZoo-VCat s)) 3) (- (VCat-H (VZoo-VCat s)) 0.1) "rechts" )
                (make-VCham (-(VCham-X (VZoo-VCham s)) 3) (-(VCham-H (VZoo-VCham s)) 0.1) "links"))]))

Some of the visual mistakes make me think you copied expressions from one line to the next. 
If you ever catch yourself doing so, write a helper function. 

So: I am glad you asked about helper functions. Because if a function like this is sooo long and 
contains that many patterns, it is really difficult to read. So here is a re-write of the first
cond line so you get an idea of where to start: 

(define (tock s)
    [(cat-and-cham-in-bounds-and-moving-right (VZoo-VCham s) (VZoo-VCat s))
     (make-VZoo (move-cat (VZoo-VCat s)) (move-cham (VZoo-VCham s)))]
    ..  ))

;; where you have these helper functions defined: 

(define (cat-and-cham-in-bounds-and-moving-right cham cat)
  (and (< (VCham-X cham) bovengrens-cham)
       (< (VCat-X cat) bovengrens-cat)
       (string=? (VCham-R cham) "rechts") 
       (string=? (VCat-R cat) "rechts")))

(define (move-cat cat)
  (make-VCat (+ (VCat-X cat) 3) (- (VCat-H cat) 0.1) "rechts"))

(define (move-cham cham)
  (make-VCham (+ (VCham-X cham) 3) (-(VCham-H cham) 0.1) "rechts"))

As you create these helper functions, you may catch yourself copying a function 
and changing it a bit. You will later learn how to avoid that too. 

Good luck -- Matthias

Posted on the users mailing list.