Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Scheme Programming Language

Exercise 21.4.1 from "How to Design Programs"


Exercise 21.4.1

1 - Abstract the functions draw-a-circle and clear-a-circle into a
single function process-circle.

2 - Define translate-circle using process-circle.

Hint: If a primitive function doesn't quite fit an abstraction,
we have to define auxiliary functions. For now, use define to do so.

1-
;; process-circle : (posn N symbol -> boolean) -> boolean
(define (process-circle f a-circle)
        (f (circle-center a-circle)
                         (circle-radius a-circle)
                                 (circle-color a-circle)))

so i can call the function this way:
(process-circle draw-circle a-circle)
(process-circle clear-circle a-circle)

2-
now .. here is the function translate-circle

;; translate-circle : circle N -> circle
;; The function consumes a circle structure and a number delta
;; The result is a circle whose center is delta pixels to the right of
the input.
;; The function has no effect on the canvas.
(define (translate-circle a-circle delta)
 (make-circle (make-posn (+ (posn-x (circle-center a-circle)) delta)
                           (posn-y (circle-center a-circle)))
         (circle-radius a-circle) (circle-color a-circle)))

the exercise asks:

Define translate-circle using process-circle.

how can you do it .. process-circle gets a circle structure and
applies
a given function to the properties of the structure .. i can't figure
out where exactly
the number delta is going to be ..

any ideas?

(I know the subject and the answer may appear to be extremely
obvious .. and I could skip this part of the exercise ..anyway ..)

thanks

I must say this book is nice :) I'm reading it in the office in my
spare time (and doing all the exercises ..)

On Apr 5, 10:17 am, lukas@gmail.com wrote:

A hint:

(1)

(define (translate-circle a-circle delta)
   (define (translate circle-center circle-radius circle-color)
     (make-circle <<you fill in this part>>))
   (process-circle translate a-circle))

-- or --

(2)

(define (translate-circle a-circle)
  (process-circle
     (lambda (circle-center circle-radius circle-color)
        (make-circle <<you fill in this part>>))
     a-circle))

I think (1)'s what they want:

"Hint: If a primitive function doesn't quite fit an abstraction,
we have to define auxiliary functions. For now, use define to do so."

The auxiliary function is translate (not translate-circle!). You don't
have to use a named auxiliary function because you can use lambda as
in (2).

Remember that delta is accessible in the <<you fill in this part>>,
because of lexical scoping.

I find process-circle more readable if you format it like this:

 (define (process-circle f a-circle)
    (f (circle-center a-circle)
       (circle-radius a-circle)
       (circle-color a-circle)))

Jules

On 5 Apr, 14:07, "Jules" <julesjac@gmail.com> wrote:

Thanks Jules,

(define (translate-circle a-circle delta)
 (local ((define (translate circle-center circle-radius circle-color)
                     (make-circle (make-posn (+ (posn-x circle-center)
delta) (posn-y circle-center))
                                        circle-radius circle-color)))
  (process-circle translate a-circle)))

The lamba version works fine too

(define (translate-circle a-circle delta)
  (process-circle
   (lambda (circle-center circle-radius circle-color)
     (make-circle (make-posn (+ (posn-x circle-center) delta) (posn-y
circle-center))
                         circle-radius circle-color)) a-circle))

You did not only help to understand what they wanted but gave me a
clue as to what lamba does :)

Regards

Luca

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc