[racket] exercise 6.6.3 htdp
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 30-11-11 13:11, frank weytjens wrote:
> (define H 300) (define L 300) (define-struct cirkel (posn number
> color)) (define cirkel1 (make-cirkel(make-posn (/ H 2) (/ L 2)) (/
> L 4) 'red)) ;; fun-for-circle: circle --> ? ;;(define
> (fun-for-circle circle) ...) ;;(define (fun-for-circle c)
> ;;(draw-solid-disk (cirkel-posn c)(cirkel-number c) (cirkel-color
> c))) (define (square x)(* x x)) (define (distance x1 x2) (- x1
> x2)) (define (x-cirkel cirkel) (posn-x (cirkel-posn cirkel)))
> (define (y-cirkel cirkel) (posn-y (cirkel-posn cirkel))) (define
> (y-pos pos) (posn-y pos)) (define (x-pos pos) (posn-x pos)) (define
> (distance-to-center C pos) (sqrt (+(square(distance (x-cirkel C)
> (x-pos pos)))(square (distance (y-cirkel C)(y-pos pos))))))
>
> (define (in-circle? C pos) (> (cirkel-number C) (distance-to-center
> C pos)))
>
> (in-circle? cirkel1 (make-posn 150 224)) --> true (in-circle?
> cirkel1 (make-posn 150 225)) --> false
>
> I'm not happy with my in-circle? function to many define's needed
> nested structures is the main cause why this simple program
> becomes complicated but how can one solve this problem more
> elegant?
Hi Frank,
some things to make your solution better in no particular order:
1) watch your spacing/layout
2) use standard functions; for example square is already available as sqr
3) try to make your functions general; for example your
distance-to-center function calculates the distance between two points
in R^2 but this is not exposed. Once you have such a function, you
also don't need to unpack your circle's coordinated from the position
anymore, so you can scrap x-cirkel and y-cirkel, and btw y-pos and
x-pos duplicate posn-x and posn-y, so you can also remove those.
Once you do that, you might end up with:
#lang racket
(define H 300)
(define L 300)
(define-struct posn (x y))
(define-struct cirkel (center radius color))
(define cirkel1 (make-cirkel (make-posn (/ H 2) (/ L 2)) (/ L 4) 'red))
(define (l2-distance pos1 pos2)
(sqrt (+ (sqr (- (posn-x pos1) (posn-x pos2)))
(sqr (- (posn-y pos1) (posn-y pos2))))))
(define (in-circle? circle pos)
(> (cirkel-radius circle)
(l2-distance (cirkel-center circle) pos)))
(in-circle? cirkel1 (make-posn 150 224))
(in-circle? cirkel1 (make-posn 150 225))
If that's still too cluttered, then you can separate the general logic
from the specific objects/variables that you define:
#lang racket
(define-struct posn (x y))
(define-struct cirkel (center radius color))
(define (l2-distance pos1 pos2)
(sqrt (+ (sqr (- (posn-x pos1) (posn-x pos2)))
(sqr (- (posn-y pos1) (posn-y pos2))))))
(define (in-circle? circle pos)
(> (cirkel-radius circle)
(l2-distance (cirkel-center circle) pos)))
(define H 300)
(define L 300)
(define cirkel1 (make-cirkel (make-posn (/ H 2) (/ L 2)) (/ L 4) 'red))
(in-circle? cirkel1 (make-posn 150 224))
(in-circle? cirkel1 (make-posn 150 225))
then you see that you basically have very few function definitions
that support your in-circle?.
Hope that helps,
Marijn
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.18 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk7XNT8ACgkQp/VmCx0OL2yGcwCgykWUnLNoiMDO9tpcGduHMbNL
heMAn2MF15VhF+N3cEuaI58n89p9IRH2
=QGjw
-----END PGP SIGNATURE-----