f



HTDP - Ex 6.3.3. Assistance required

Hello all

Trust that you all survived the festivities without mishap and generally 
had a good time.

I have run aground on trying to work out the solution for Ex 6.3.3. in 
HTDP. I have included the assignment here for reference:

"Provide a structure definition that represents an airforce's jet 
fighters. Assume that a fighter has four essential properties: 
designation ('f22, 'tornado, or 'mig22), acceleration, top-speed, and 
range. Then develop the function within-range. The function consumes a 
fighter record and the distance of a target from the (fighter's) base. 
It determines whether the fighter can reach the intended target. Also 
develop the function reduce-range. The function consumes a fighter 
record and produces one in which the range field is reduced to 80% of 
the original value."

Source: http://www.htdp.org/2002-09-22/Book/curriculum-Z-H-9.html

I have been stuck here all day with this problem, not really even very 
sure where to start.
So far, I have built up the structure definition

(define-struct jet-fighter
     (designation acceleration top-speed range))

This obviously allows for operations such as:

(jet-fighter-designation
     (make-jet-fighter 'F22 100 200 300))

etc ...

But then I get stuck in understanding how the problem proceeds. For 
example, I am thinking about how I can go about including these data 
structure definition concepts in relational expressions. I am inclined 
to think that, on the balance of probability, that is what in the UK 
we'd call OTT (over the top) :)
However, I am unable to conceive of the solution. I'd appreciate it if 
someone were willing to drop some hints or suggest a way of approaching 
this. I'd like to try to do the calculations so I'd prefer process 
rather than answers.

Can anyone lend a brain cell or two?

Thanks
- Andy

0
Andy_not (48)
12/28/2003 1:36:08 AM
comp.lang.scheme 4781 articles. 0 followers. Post Follow

13 Replies
273 Views

Similar Articles

[PageSpeed] 32

"Andy" wrote:
> "Provide a structure definition that represents an airforce's jet 
> fighters. Assume that a fighter has four essential properties: 
> designation ('f22, 'tornado, or 'mig22), acceleration, top-speed, and 
> range. 
....
 > So far, I have built up the structure definition
 >
 > (define-struct jet-fighter
 >     (designation acceleration top-speed range))

Looks good so far...

 > Then develop the function within-range. The function consumes a
> fighter record and the distance of a target from the (fighter's) base. 

This gives: (define (within-range fighter target-distance)...

> It determines whether the fighter can reach the intended target. 

Translating a little, the function should return true if the 
target-distance is within the fighter's range, otherwise it should 
return false.  Given the above definitions, you need to compare the 
fighter's range, i.e. (fighter-range fighter) to target-distance, and 
arrange to return true or false based on the result of the comparison.

 > For example, I am thinking about how I can go about including these
 > data structure definition concepts in relational expressions. I am
 > inclined to think that, on the balance of probability, that is what
 > in the UK we'd call OTT (over the top) :)

A relational expression is exactly what you want for the above: you need 
to determine whether one value is less than another.  Conveniently, the 
result of such a comparison should be true or false, which is exactly 
what the function needs to return.

> Also develop the function reduce-range. The function consumes
> a fighter record and produces one in which the range field is
> reduced to 80% of the original value."

This gives: (define (reduce-range fighter)...

Inside this function, all you have to do is create a new fighter record 
based on the original, using something like:

   (make-jet-fighter
     (fighter-designation fighter)
     (fighter-acceleration fighter)
     ...

Of course, the last value provided to make-jet-fighter should be the 
newly calculated range, equal to 80% of (fighter-range fighter).

Anton

0
anton58 (1240)
12/28/2003 4:31:26 AM
Anton van Straaten wrote:
> "Andy" wrote:
> 
>> "Provide a structure definition that represents an airforce's jet 
>> fighters. Assume that a fighter has four essential properties: 
>> designation ('f22, 'tornado, or 'mig22), acceleration, top-speed, and 
>> range. 
> 
> ....
>  > So far, I have built up the structure definition
>  >
>  > (define-struct jet-fighter
>  >     (designation acceleration top-speed range))
> 
> Looks good so far...
> 
>  > Then develop the function within-range. The function consumes a
> 
>> fighter record and the distance of a target from the (fighter's) base. 
> 
> 
> This gives: (define (within-range fighter target-distance)...
> 
>> It determines whether the fighter can reach the intended target. 
> 
> 
> Translating a little, the function should return true if the 
> target-distance is within the fighter's range, otherwise it should 
> return false.  Given the above definitions, you need to compare the 
> fighter's range, i.e. (fighter-range fighter) to target-distance, and 
> arrange to return true or false based on the result of the comparison.
> 
>  > For example, I am thinking about how I can go about including these
>  > data structure definition concepts in relational expressions. I am
>  > inclined to think that, on the balance of probability, that is what
>  > in the UK we'd call OTT (over the top) :)
> 
> A relational expression is exactly what you want for the above: you need 
> to determine whether one value is less than another.  Conveniently, the 
> result of such a comparison should be true or false, which is exactly 
> what the function needs to return.
> 
>> Also develop the function reduce-range. The function consumes
>> a fighter record and produces one in which the range field is
>> reduced to 80% of the original value."
> 
> 
> This gives: (define (reduce-range fighter)...
> 
> Inside this function, all you have to do is create a new fighter record 
> based on the original, using something like:
> 
>   (make-jet-fighter
>     (fighter-designation fighter)
>     (fighter-acceleration fighter)
>     ...
> 
> Of course, the last value provided to make-jet-fighter should be the 
> newly calculated range, equal to 80% of (fighter-range fighter).
> 
> Anton
> 
Anton
Thank you so much. I'll give what you suggested a spin today and get 
back to you. I just couldn't figure out the next step and appreciate 
your insight.
Will keep you posted :)

Best wishes
- Andy

-- 
"Today a young man on acid realised that all matter was really energy
condensed to a slow vibration, that we are all one consciousness
experiencing itself subjectively, there's no such thing as death,
life is only a dream, and we're the imaginations of ourselves.
Here's Tom with the weather ..." - Bill Hicks.

0
Neptune
12/28/2003 11:38:29 AM
Anton

Thanks once again for your help. It would appear that I have been able 
to successfully resolve part 1 of the problem I described earlier.

This is the solution, and I'd welcome your comments:

+++++++++++++code begin+++++++++++++++++

;; HTDP : Ex 6.3.3.
;; fighter : symbol integer integer integer -> fighter-structure
;; consume 1 symbol and 3 integers to produce a fighter structure
;;     definition

(define-struct fighter
                (designation acceleration top-speed range))


;; construct three fighters according to structure definition

(define MiG
    (make-fighter 'MiG 100 250 300))

(define Tornado
    (make-fighter 'Tornado 150 300 420))

(define F22
    (make-fighter 'F22 130 245 301))

;; within-range : structure integer -> boolean
;; test the range of each plane against a given distance
;; examples: (within-range (fighter-range Tornado) 250) -> 'cando
;;           (within-range (fighter-range F22) 400) -> 'toofar

(define (within-range fighter target-distance)
     (cond
        [(> target-distance fighter) 'toofar]
        [(<= target-distance fighter) 'cando]))


;; reduce-range : structure -> structure
;; consume fighter record and produce a range reduced by 80% of the
;;    original value

(define (reduce-range fighter)
     (- fighter (* fighter .8)))

+++++++++++++code end+++++++++++++++++++

I have calculated all of the above and each produces the outcomes one 
would expect, so I guess that means the code is okay algorithmically. 
Can you make any comments about the overall code/style things that might 
be redundant - for e.g. I'm not sure that each of the fighters needed to 
be defined like that; I could have done that at the interpreter. It 
looks like the originally defined values are preserved this way however 
(e.g. the range of the Tornado is still 420 despite the reduce-range 
calculation) and saves me having to re-enter the data.

Anyway, once again. Big thanks for your help on this Anton. Your steer 
was very useful.

Best wishes

- Andy

0
Andy_not (48)
12/28/2003 4:46:26 PM
"Andy" <Andy_not@home.com> wrote:
> ;; within-range : structure integer -> boolean
> ;; test the range of each plane against a given distance
> ;; examples: (within-range (fighter-range Tornado) 250) -> 'cando
> ;;           (within-range (fighter-range F22) 400) -> 'toofar
>
> (define (within-range fighter target-distance)
>      (cond
>         [(> target-distance fighter) 'toofar]
>         [(<= target-distance fighter) 'cando]))

I believe the HTDP specification wanted this function to accept a fighter
record, not the fighter's range.  So the cond should contain expressions
like (> target-distance (fighter-range fighter)), and the calling examples
should look like (within-range Tornado 250).

Also, the comment above defines the function as returning a boolean (which
was what I had suggested), but it in fact returns a symbol - 'toofar, or
'cando.  You should either change the comment, or change the return result.
In the Student language levels, true and false are defined as keywords
representing booleans, so you can use those (without quotes of any kind).
Traditionally, a function like this would return a boolean, although I'm not
sure if HTDP implies otherwise anywhere prior to this exercise.

> ;; reduce-range : structure -> structure
> ;; consume fighter record and produce a range reduced by 80% of the
> ;;    original value
>
> (define (reduce-range fighter)
>      (- fighter (* fighter .8)))

Again, the comment doesn't match the definition.  Reduce-range as defined
above will only work if given an actual range integer, and it will return an
integer.  The comment and the problem statement says it should consume a
fighter record, and return a fighter record.  Take another look at my
previous post to see a hint of how to write this.  Note that accessing the
range of the fighter will need to be done inside the function, not in the
caller.

Finally (sorry for the laundry list), the original specification said "the
range field is reduced *to* 80% of the original value."  That's not quite
the same as the comment above, or the definition.  On this last point, don't
worry, apparently space agency programmers make these sorts of mistakes all
the time, which is why probes keep missing Mars...  ;)

Anton



0
anton58 (1240)
12/29/2003 7:48:52 AM
Andy <Andy_not@home.com> writes:

>
> ;; within-range : structure integer -> boolean
> ;; test the range of each plane against a given distance
> ;; examples: (within-range (fighter-range Tornado) 250) -> 'cando
> ;;           (within-range (fighter-range F22) 400) -> 'toofar
>
> (define (within-range fighter target-distance)
>      (cond
>         [(> target-distance fighter) 'toofar]
>         [(<= target-distance fighter) 'cando]))

May I suggest that you return #t or #f rather than 'toofar or 'cando?

That way you can use this conditional as the predicate to other
conditionals without having to first compare the result to 'toofar or
'cando.  Additionally, your type signature,  
   structure integer -> boolean,
would be more technically correct.  Right now it is technically
   structure integer -> symbol

> +++++++++++++code end+++++++++++++++++++
>
> I have calculated all of the above and each produces the outcomes one
> would expect, so I guess that means the code is okay
> algorithmically. Can you make any comments about the overall
> code/style things that might be redundant - for e.g. I'm not sure that
> each of the fighters needed to be defined like that; I could have done
> that at the interpreter. It looks like the originally defined values
> are preserved this way however (e.g. the range of the Tornado is still
> 420 despite the reduce-range calculation) and saves me having to
> re-enter the data.

Yes, this is an advantage.  You don't want to reduce the range of the
old Tornados (imagine the pilot's surprise!) you want to create a new
Tornado with a smaller gas tank.

-- 
~jrm
0
12/29/2003 9:53:40 AM
Thanks for your comments and feedback Anton and Joe. Looking back on it 
I see your comments are right on target and so I'll take your advice and 
re-work these definitions and structures. Once I've done that, I'll repost.
Thanks for your patience and (once again) for your comments. At least 
someone is looking out for the welfare of these poor pilots!!! :)

Best wishes
- Andy

0
Andy_not (48)
12/29/2003 12:34:12 PM
In article <3fef08aa@212.67.96.135>, Andy  <Andy_not@home.com> wrote:
>

I haven't looked at the original problem specification, but two
pure style things --


>(define (within-range fighter target-distance)
>     (cond
>        [(> target-distance fighter) 'toofar]
>        [(<= target-distance fighter) 'cando]))

How about:

  (define (within-range fighter target-distance)
    (if (<= target-distance fighter)
      'cando
      'toofar))

Or if you want to return #t / #f instead, consider:

  (define (within-range fighter target-distance)
    (if (<= target-distance fighter)
      #t
      #f))

Which everyone would shorten to:

  (define (within-range fighter target-distance)
    (<= target-distance fighter)

And some people would shorten to:

  (define within-range <=)  # wrong

Except that in this case your arguments are in the wrong order.
But let it be a reminder to avoid needlessly wrapping function
calls inside other functions.  Another place you see this
commonly is:

  (map (lambda (x) (f x)) l)

Instead of

  (map f l)



>(define (reduce-range fighter)
>     (- fighter (* fighter .8)))

If this is really what you want, consider instead:

  (define (reduce-range fighter)
    (* fighter .2))

Even if your compiler can do that optimization, it's probably
easier on the reader (and less prone to error) if you simply
yourself.


-- 
Jeffrey M. Vinocur   *   jmv16@cornell.edu
http://www.people.cornell.edu/pages/jmv16/
0
jmv16 (23)
12/29/2003 6:19:00 PM
Hi Anton & Joe

I think that I have fixed the error(s). Your comments were helpful last 
time, so if there is more feedback I'd welcome that.
New code:

++++++++++++++code begin++++++++++++++

;; HtDP : Ex.6.3.3.

;; fighter : symbol integer integer integer -> fighter-structure
;; consume 1 symbol and 3 integers to produce a fighter structure
;;     definition

(define-struct fighter
                (designation acceleration top-speed range))


;; construct three fighters according to structure definition

(define MiG
   (make-fighter 'MiG 100 250 300))

(define Tornado
   (make-fighter 'Tornado 150 300 420))

(define F22
   (make-fighter 'F22 130 245 301))

;; within-range : structure integer -> boolean
;; test the range of each plane against a given distance
;; examples: (within-range (fighter-range (make-fighter 'Tornado 150 300 
   420) 250)) -> #t
;;           (within-range (fighter-range (make-fighter 'F22 130 245 
301) 400)) -> #f

(define (within-range fighter target-distance)
   (cond
     [(> target-distance (fighter-range fighter)) #f]
     [(<= target-distance (fighter-range fighter)) #t]))

;; reduce-range : structure -> structure
;; consume fighter record and produce a new record with a range
;;    field is reduced by 80%
;; examples: (reduce-range (fighter-range Tornado)) -> 84

(define (reduce-range a-fighter)
   (make-fighter (fighter-designation a-fighter)
                 (fighter-acceleration a-fighter)
                 (fighter-top-speed a-fighter)
                 (- (* 80/100 fighter-range a-fighter))))


++++++++++++++code end++++++++++++++++

Seems to work just fine ... so any other comments thoughts, etc would be 
welcomed.
Thanks a bunch guys!!!

Best wishes

- Andy

0
Andy_not (48)
12/30/2003 6:01:27 PM
"Andy" <Andy_not@home.com> wrote:
> ;; within-range : structure integer -> boolean
> ;; test the range of each plane against a given distance
> ;; examples: (within-range (fighter-range (make-fighter 'Tornado 150 300
>    420) 250)) -> #t
> ;;           (within-range (fighter-range (make-fighter 'F22 130 245
> 301) 400)) -> #f
>
> (define (within-range fighter target-distance)
>    (cond
>      [(> target-distance (fighter-range fighter)) #f]
>      [(<= target-distance (fighter-range fighter)) #t]))

The code above looks correct, but the comment is out of date.  The type of
within-range is now "structure -> boolean", and the example calls no longer
need to use 'fighter-range', since that's being done within the function
itself.

Jeffrey M. Vinocur mentioned an optimization which you can do with the above
code.  I'm not sure if HTDP would encourage you to do something like this at
this point, but I'll mention it anyway.  The code should work fine as it is,
but you should be aware that the type of the result returned by the
relational operators "<" and ">=" is a boolean.  Since you want your
function to return a boolean, if you arrange things properly, you can
eliminate the 'cond' above and end up just returning the result of a
relational expression, e.g.

   (relational-operator target-distance (fighter-range fighter)))

....where 'relational-operator' is the operator you need to get the right
answer in all cases.  If this seems confusing, ignore it for now.

> ;; reduce-range : structure -> structure
> ;; consume fighter record and produce a new record with a range
> ;;    field is reduced by 80%
> ;; examples: (reduce-range (fighter-range Tornado)) -> 84
>
> (define (reduce-range a-fighter)
>    (make-fighter (fighter-designation a-fighter)
>                  (fighter-acceleration a-fighter)
>                  (fighter-top-speed a-fighter)
>                  (- (* 80/100 fighter-range a-fighter))))

Again, the code looks pretty good, but in the example call in the comment,
the use of 'fighter-range' isn't needed any more: it should be simply
(reduce-range Tornado).

As for the code, the subtraction needs to be removed.  As it stands, the
range of the new fighter records will be negative (assuming the original was
positive), and it won't work correctly when you use within-range (since no
positive number will be within range).

Anton



0
anton58 (1240)
12/31/2003 12:50:24 AM
Anton van Straaten wrote:
> "Andy" <Andy_not@home.com> wrote:
> 
>>;; within-range : structure integer -> boolean
>>;; test the range of each plane against a given distance
>>;; examples: (within-range (fighter-range (make-fighter 'Tornado 150 300
>>   420) 250)) -> #t
>>;;           (within-range (fighter-range (make-fighter 'F22 130 245
>>301) 400)) -> #f
>>
>>(define (within-range fighter target-distance)
>>   (cond
>>     [(> target-distance (fighter-range fighter)) #f]
>>     [(<= target-distance (fighter-range fighter)) #t]))
> 
> 
> The code above looks correct, but the comment is out of date.  The type of
> within-range is now "structure -> boolean", and the example calls no longer
> need to use 'fighter-range', since that's being done within the function
> itself.
> 
> Jeffrey M. Vinocur mentioned an optimization which you can do with the above
> code.  I'm not sure if HTDP would encourage you to do something like this at
> this point, but I'll mention it anyway.  The code should work fine as it is,
> but you should be aware that the type of the result returned by the
> relational operators "<" and ">=" is a boolean.  Since you want your
> function to return a boolean, if you arrange things properly, you can
> eliminate the 'cond' above and end up just returning the result of a
> relational expression, e.g.
> 
>    (relational-operator target-distance (fighter-range fighter)))
> 
> ....where 'relational-operator' is the operator you need to get the right
> answer in all cases.  If this seems confusing, ignore it for now.
> 
> 
>>;; reduce-range : structure -> structure
>>;; consume fighter record and produce a new record with a range
>>;;    field is reduced by 80%
>>;; examples: (reduce-range (fighter-range Tornado)) -> 84
>>
>>(define (reduce-range a-fighter)
>>   (make-fighter (fighter-designation a-fighter)
>>                 (fighter-acceleration a-fighter)
>>                 (fighter-top-speed a-fighter)
>>                 (- (* 80/100 fighter-range a-fighter))))
> 
> 
> Again, the code looks pretty good, but in the example call in the comment,
> the use of 'fighter-range' isn't needed any more: it should be simply
> (reduce-range Tornado).
> 
> As for the code, the subtraction needs to be removed.  As it stands, the
> range of the new fighter records will be negative (assuming the original was
> positive), and it won't work correctly when you use within-range (since no
> positive number will be within range).
> 
> Anton
> 
> 
> 

Thanks again for your comments Anton. A good lesson for me to learn here 
is to be watchful for my careless mistakes!! You are quite correct in 
pointing out the errors in the comments. Also, I thought that I had 
removed the subtraction on the code I posted because I kept on coming up 
with negatives, but I guess that I changed it but didn't save that 
version when I posted it. <blush>
Oh well ... I have posted the (hopefully) final version of the code 
snippets below.

With respect to Jeffrey's comments regarding optimisation: I did read 
his comments but (a) wasn't sure how I could apply it to these examples 
without confusing myself further (b) considered doing that optimisation 
of code as something that while it might offer a more compact code, in 3 
days from now I really don't think that I'll be able to figure out what 
was what! No, I haven't come across anything in HTDP that recommends 
this approach (yet, anyway). So I'm going to stick with unoptimised code 
for now - at least until I can begin to really get a handle on how 
Scheme works. It took me ages to figure out, for example, that the 
'reduce-range' code required an extra set of parentheses in the last 
line to make it work. My watch-word for now is "baby steps", I'm afraid. 
I am finding difficulty in keeping the flow of values through the 
programs, so anything that reminds me about how a program works (e.g. 
comments and/or actual design/structure) is valuable to me right now.

So, here then is the final (???) version of HTDP Exercise 6.3.3., and 
once again Anton ... thanks a bunch for your kindness in walking me 
through this.

++++++++++++++code begin+++++++++++++++++++++++

;; within-range : structure -> boolean
;; test the range of each plane against a given distance
;; examples: (within-range (make-fighter 'Tornado 150 300 420) 250) -> #t
;;           (within-range (make-fighter 'F22 130 245 301) 400) -> #f

(define (within-range fighter target-distance)
   (cond
     [(> target-distance (fighter-range fighter)) #f]
     [(<= target-distance (fighter-range fighter)) #t]))

;; reduce-range : structure -> structure
;; consume fighter record and produce a new record with a range
;;    field is reduced by 80%
;; examples: (reduce-range Tornado) -> 84

(define (reduce-range a-fighter)
   (make-fighter (fighter-designation a-fighter)
                 (fighter-acceleration a-fighter)
                 (fighter-top-speed a-fighter)
                 (* 20/100 (fighter-range a-fighter))))

+++++++++++++++code end++++++++++++++++++++++++++++++++++

0
Andy_not (48)
12/31/2003 1:53:06 PM
Andy <Andy_not@home.com> writes:

> Hi Anton & Joe
>
> I think that I have fixed the error(s).  Your comments were helpful
> last time, so if there is more feedback I'd welcome that.


> ;; construct three fighters according to structure definition
>
> (define MiG
>    (make-fighter 'MiG 100 250 300))
>
> (define Tornado
>    (make-fighter 'Tornado 150 300 420))
>
> (define F22
>    (make-fighter 'F22 130 245 301))

Looks good.  Note that you can make several of each kind:

(define jrms-f22
  (make-fighter 'f22 130 245 201))

;; Give anton a bigger gas tank, but worse acceleration
;; and slower top speed.

(define antons-f22
  (make-fighter 'f22 120 230 300))

> ;; within-range : structure integer -> boolean
> ;; test the range of each plane against a given distance
> ;; examples: (within-range (fighter-range (make-fighter 'Tornado 150
> ;; 300  420) 250)) -> #t
> ;;           (within-range (fighter-range (make-fighter 'F22 130 245
> ;;           301) 400)) -> #f
>
> (define (within-range fighter target-distance)
>    (cond
>      [(> target-distance (fighter-range fighter)) #f]
>      [(<= target-distance (fighter-range fighter)) #t]))

Much better.

The first test, (> target-distance (fighter-range fighter)) *ought* to
be the exact opposite of (<= target-distance (fighter-range fighter)),
but suppose that a few months from now, after you've left the company,
some manager decides that you *really* ought to take into account that
you might want to spend some time at the target rather than
immediately turning around.  He assigns a junior programmer to change
your code, and the junior programmer decides that he'll do this by
adding some amount to the target distance to make the target appear a
little further away.  He changes the code as follows:

 (define (within-range fighter target-distance)
    (cond
      [(> target-distance (fighter-range fighter)) #f]
      [(<= (+ target-distance 10) (fighter-range fighter)) #t]))

Of course, this junior programmer makes a mistake and only changes
*one* of the arms of the conditional.  Now there is a problem.  If the
target is just in range, the first test will fail, but if it is
*almost* out of range the second test fails too.  Then we get an
undefined result!

Jeffrey Vinocur suggested (something like) this:

 (define (within-range fighter target-distance)
    (cond
      [(<= target-distance (fighter-range fighter)) #t]
      [else #f]))

Now when the lazy junior programmer comes in to change the code, he
can't make the mistake of only fixing half of it.

Jeffrey Vinocur also noted that you could even simplify this to the
following:

 (define (within-range fighter target-distance)
    (<= target-distance (fighter-range fighter)))

Because <= returns #t or #f, so there is no reason to test the result
just to return the same thing.

-- 
~jrm
0
12/31/2003 10:53:37 PM
Joe Marshall wrote:
> Andy <Andy_not@home.com> writes:
> 
> 
>>Hi Anton & Joe
>>
>>I think that I have fixed the error(s).  Your comments were helpful
>>last time, so if there is more feedback I'd welcome that.
> 
> 
> 
>>;; construct three fighters according to structure definition
>>
>>(define MiG
>>   (make-fighter 'MiG 100 250 300))
>>
>>(define Tornado
>>   (make-fighter 'Tornado 150 300 420))
>>
>>(define F22
>>   (make-fighter 'F22 130 245 301))
> 
> 
> Looks good.  Note that you can make several of each kind:
> 
> (define jrms-f22
>   (make-fighter 'f22 130 245 201))
> 
> ;; Give anton a bigger gas tank, but worse acceleration
> ;; and slower top speed.
> 
> (define antons-f22
>   (make-fighter 'f22 120 230 300))
> 
> 
>>;; within-range : structure integer -> boolean
>>;; test the range of each plane against a given distance
>>;; examples: (within-range (fighter-range (make-fighter 'Tornado 150
>>;; 300  420) 250)) -> #t
>>;;           (within-range (fighter-range (make-fighter 'F22 130 245
>>;;           301) 400)) -> #f
>>
>>(define (within-range fighter target-distance)
>>   (cond
>>     [(> target-distance (fighter-range fighter)) #f]
>>     [(<= target-distance (fighter-range fighter)) #t]))
> 
> 
> Much better.
> 
> The first test, (> target-distance (fighter-range fighter)) *ought* to
> be the exact opposite of (<= target-distance (fighter-range fighter)),
> but suppose that a few months from now, after you've left the company,
> some manager decides that you *really* ought to take into account that
> you might want to spend some time at the target rather than
> immediately turning around.  He assigns a junior programmer to change
> your code, and the junior programmer decides that he'll do this by
> adding some amount to the target distance to make the target appear a
> little further away.  He changes the code as follows:
> 
>  (define (within-range fighter target-distance)
>     (cond
>       [(> target-distance (fighter-range fighter)) #f]
>       [(<= (+ target-distance 10) (fighter-range fighter)) #t]))
> 
> Of course, this junior programmer makes a mistake and only changes
> *one* of the arms of the conditional.  Now there is a problem.  If the
> target is just in range, the first test will fail, but if it is
> *almost* out of range the second test fails too.  Then we get an
> undefined result!
> 
> Jeffrey Vinocur suggested (something like) this:
> 
>  (define (within-range fighter target-distance)
>     (cond
>       [(<= target-distance (fighter-range fighter)) #t]
>       [else #f]))
> 
> Now when the lazy junior programmer comes in to change the code, he
> can't make the mistake of only fixing half of it.
> 
> Jeffrey Vinocur also noted that you could even simplify this to the
> following:
> 
>  (define (within-range fighter target-distance)
>     (<= target-distance (fighter-range fighter)))
> 
> Because <= returns #t or #f, so there is no reason to test the result
> just to return the same thing.
> 
Hello Joe
Thanks for your comments and thoughts about this exercise. I get your 
point about the potential for errors to creep into the code if one were 
to modify the code later on. I didn't realise that one of the values of 
Jefferey Vinocur's code suggestion also helped ameliorate opportunities 
for errors ... so thanks for pointing that out. I have corrected the 
code in my notes to reflect this and will try to watch for it in future. 
Basically then the other 'arm' of the boolean is implied providing that 
one 'arm' is explicit ... so it is not necessary to include (> x y) if 
one has written (<= x y), because if x is neither less than or equal to 
y then, by default, it must be greater than.
I won't post the code corrections here, but I think that the idea is 
beginning to dawn.
Thanks :)

- Andy


-- 
"Today a young man on acid realised that all matter was really energy
condensed to a slow vibration, that we are all one consciousness
experiencing itself subjectively, there's no such thing as death,
life is only a dream, and we're the imaginations of ourselves.
Here's Tom with the weather ..." - Bill Hicks.

0
Neptune
1/1/2004 1:16:03 PM
Joe Marshall wrote:
> Andy <Andy_not@home.com> writes:
> 
> 
>>Hi Anton & Joe
>>
>>I think that I have fixed the error(s).  Your comments were helpful
>>last time, so if there is more feedback I'd welcome that.
> 
> 
> 
>>;; construct three fighters according to structure definition
>>
>>(define MiG
>>   (make-fighter 'MiG 100 250 300))
>>
>>(define Tornado
>>   (make-fighter 'Tornado 150 300 420))
>>
>>(define F22
>>   (make-fighter 'F22 130 245 301))
> 
> 
> Looks good.  Note that you can make several of each kind:
> 
> (define jrms-f22
>   (make-fighter 'f22 130 245 201))
> 
> ;; Give anton a bigger gas tank, but worse acceleration
> ;; and slower top speed.
> 
> (define antons-f22
>   (make-fighter 'f22 120 230 300))
> 
> 
>>;; within-range : structure integer -> boolean
>>;; test the range of each plane against a given distance
>>;; examples: (within-range (fighter-range (make-fighter 'Tornado 150
>>;; 300  420) 250)) -> #t
>>;;           (within-range (fighter-range (make-fighter 'F22 130 245
>>;;           301) 400)) -> #f
>>
>>(define (within-range fighter target-distance)
>>   (cond
>>     [(> target-distance (fighter-range fighter)) #f]
>>     [(<= target-distance (fighter-range fighter)) #t]))
> 
> 
> Much better.
> 
> The first test, (> target-distance (fighter-range fighter)) *ought* to
> be the exact opposite of (<= target-distance (fighter-range fighter)),
> but suppose that a few months from now, after you've left the company,
> some manager decides that you *really* ought to take into account that
> you might want to spend some time at the target rather than
> immediately turning around.  He assigns a junior programmer to change
> your code, and the junior programmer decides that he'll do this by
> adding some amount to the target distance to make the target appear a
> little further away.  He changes the code as follows:
> 
>  (define (within-range fighter target-distance)
>     (cond
>       [(> target-distance (fighter-range fighter)) #f]
>       [(<= (+ target-distance 10) (fighter-range fighter)) #t]))
> 
> Of course, this junior programmer makes a mistake and only changes
> *one* of the arms of the conditional.  Now there is a problem.  If the
> target is just in range, the first test will fail, but if it is
> *almost* out of range the second test fails too.  Then we get an
> undefined result!
> 
> Jeffrey Vinocur suggested (something like) this:
> 
>  (define (within-range fighter target-distance)
>     (cond
>       [(<= target-distance (fighter-range fighter)) #t]
>       [else #f]))
> 
> Now when the lazy junior programmer comes in to change the code, he
> can't make the mistake of only fixing half of it.
> 
> Jeffrey Vinocur also noted that you could even simplify this to the
> following:
> 
>  (define (within-range fighter target-distance)
>     (<= target-distance (fighter-range fighter)))
> 
> Because <= returns #t or #f, so there is no reason to test the result
> just to return the same thing.
> 

Hello Joe
Thanks for your comments and thoughts about this exercise. I get your 
point about the potential for errors to creep into the code if one were 
to modify the code later on. I didn't realise that one of the values of 
Jefferey Vinocur's code suggestion also helped ameliorate opportunities 
for errors ... so thanks for pointing that out. I have corrected the 
code in my notes to reflect this and will try to watch for it in future. 
Basically then the other 'arm' of the boolean is implied providing that 
one 'arm' is explicit ... so it is not necessary to include (> x y) if 
one has written (<= x y), because if x is neither less than or equal to 
y then, by default, it must be greater than.
I won't post the code corrections here, but I think that the idea is 
beginning to dawn. :)
Thanks

- Andy

0
Andy_not (48)
1/1/2004 1:18:16 PM
Reply:

Similar Artilces:

Upgrading from 3.3 to 3.6
I'm just wondering if the upgrade option provided by the boot disk will work. I know reinstallation will work, but of course it's not the option I want. So if I follow the changes listed on the mini-upgrade page, will such a upgrade be possible? Thanks very much. Gary wrote: > I'm just wondering if the upgrade option provided by the boot disk > will > work. I know reinstallation will work, but of course it's not the option I > want. So if I follow the changes listed on the mini-upgrade page, will > such a upgrade be possible? Than...

tkzinc 3.3.4 & 3.3.6+ ?
Hi ! Do anybody know what different between version 3.3.4 and 3.3.6+ ? And, where can I find documentation on tkzinc 3.3.6+ ? Thanks. ...

[RELEASED] Python 3.2.6, Python 3.3.6
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On behalf of the Python development team, I'm happy to announce the release of Python 3.2.6 and 3.3.6. Both are security-fix releases, which are provided source-only on python.org. The list of security-related issues fixed in the releases is given in the changelogs: https://hg.python.org/cpython/raw-file/v3.2.6/Misc/NEWS https://hg.python.org/cpython/raw-file/v3.3.6/Misc/NEWS To download the releases visit one of: https://www.python.org/downloads/release/python-326/ https://www.python.org/downloads/release/...

[RELEASED] Python 3.2.6, Python 3.3.6
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On behalf of the Python development team, I'm happy to announce the release of Python 3.2.6 and 3.3.6. Both are security-fix releases, which are provided source-only on python.org. The list of security-related issues fixed in the releases is given in the changelogs: https://hg.python.org/cpython/raw-file/v3.2.6/Misc/NEWS https://hg.python.org/cpython/raw-file/v3.3.6/Misc/NEWS To download the releases visit one of: https://www.python.org/downloads/release/python-326/ https://www.python.org/downloads/release/...

Help: PIX 6.3.3.ED or 6.2.3.GD ?
Can anyone tell me the difference between these two releases or any reason I would not want to go with the newer (but larger) release ? This is for a PIX501. Out at the Cisco site, they have the _same_ release date on them. The 6.3.3 release is roughly 1mb bigger. They seem to have the same requirements though (?) pix623.bin 6.2.3.GD 28-AUG-2003 1677312 PIX OS version 6.2(3) software. Requires a minimu of 8 MB Flash and 16 MB RAM. pix633.bin 6.3.3.ED 28-AUG-2003 2064384 PIX OS version 6.3(3) software. Requires a minimu of 8 MB Flash and 16 MB RAM. Thanks, "Jimmy...

Update from 7.3.3 to 7.3.6
Hi, We are in the process of moving from PostgreSQL 7.3.3 to 7.3.6 (Red Hat). However, we are having a number of problems importing the database schema. Some of the SPs are written in TCL and it would seem that the library has changed. In the dump of the schema we have (comments removed for brevity): CREATE TRUSTED PROCEDURAL LANGUAGE plpgsql HANDLER plpgsql_call_handler; CREATE FUNCTION pltcl_call_handler () RETURNS language_handler AS '$libdir/pltcl', 'pltcl_call_handler' LANGUAGE c; CREATE TRUSTED PROCEDURAL LANGUAGE pltcl HANDLER pltcl_call_handler; However, w...

Update from 7.3.3 to 7.3.6
Hi, We are in the process of moving from PostgreSQL 7.3.3 to 7.3.6 (Red Hat). However, we are having a number of problems importing the database schema. Some of the SPs are written in TCL and it would seem that the library has changed. In the dump of the schema we have (comments removed for brevity): CREATE TRUSTED PROCEDURAL LANGUAGE plpgsql HANDLER plpgsql_call_handler; CREATE FUNCTION pltcl_call_handler () RETURNS language_handler AS '$libdir/pltcl', 'pltcl_call_handler' LANGUAGE c; CREATE TRUSTED PROCEDURAL LANGUAGE pltcl HANDLER pltcl_call_handler; However, w...

[tao-announce] ACE/TAO 6.3.3/2.3.3 RPMs available!
Hi, We have uploaded the new micro release to OpenSuSE Build Service which now provides RPMs for this new micro release. For more details see ORBzone.org at http://www.orbzone.org/node/387 Best regards, Johnny Willemsen Remedy IT Looking for support and consultancy related to ACE/TAO, see http://www.theaceorb.nl! ...

[ace-announce] ACE/TAO 6.3.3/2.3.3 RPMs available!
Hi, We have uploaded the new micro release to OpenSuSE Build Service which now provides RPMs for this new micro release. For more details see ORBzone.org at http://www.orbzone.org/node/387 Best regards, Johnny Willemsen Remedy IT Looking for support and consultancy related to ACE/TAO, see http://www.theaceorb.nl! ...

[ace-users] ACE/TAO 6.3.3/2.3.3 RPMs available!
Hi, We have uploaded the new micro release to OpenSuSE Build Service which now provides RPMs for this new micro release. For more details see ORBzone.org at http://www.orbzone.org/node/387 Best regards, Johnny Willemsen Remedy IT Looking for support and consultancy related to ACE/TAO, see http://www.theaceorb.nl! ...

[tao-users] ACE/TAO 6.3.3/2.3.3 RPMs available!
Hi, We have uploaded the new micro release to OpenSuSE Build Service which now provides RPMs for this new micro release. For more details see ORBzone.org at http://www.orbzone.org/node/387 Best regards, Johnny Willemsen Remedy IT Looking for support and consultancy related to ACE/TAO, see http://www.theaceorb.nl! ...

Command-click no longer opens url in background: Firefox 3.6.x (3.6.3)
In Eudora 6.2.4 (Mac) doing a command-click on a link no longer opens it in the background (Firefox 3.6.2-3.6.3). Initially with FF 3.6.0, there were problems with links that had spaces in them (not just in Eudora), so I went back to FF 3.5.8 (or maybe 3.5.7 at the time). In FF 3.6.2 you could double-click on a link and it would open (with focus shifted to FF) but command-click no longer worked. Same with FF 3.6.3. Opening a html page in the browser also works for 3.6.2-3.6.3. Interestingly, in Google Chrome command-click does open the link in the background. Bummer, opening a li...

gcc 3.3.10 on Power (AIX 3.3.3)
Is there any issue using this compiler on a Power RS/6000 ? Can it compile 64 bit? ...

gawk 3.0.6 vs 3.1.3
Hello, I wrote several routines a while ago using gawk 3.0.6. Now I updated gawk to 3.1.3 and I found that the following routine listing the contents of a directory doesn't work in new version of gawk: BEGIN{ _dir="/home/pdebicki/work"; while ( ("ls -aF " _dir | getline) > 0 ) { print $0; } close("ls -aF " _dir) } when I rearranged the routine in the following way it started to work: BEGIN{ _dir="/home/pdebicki/work"; command="ls -aF " _dir while ( (command | getline) > 0 ) { print $0; } close(command) } Can anybody ...

Sgi Irix for sale 5.3, 6.2,6.3,6.4 and 6.5
I have some old Sgi OS CD's to sell ideally to someone here in the UK. The complete list is as follows There are 15 CDs in total held in a nice Sgi "Multi disc Binder " Here is the complete list p/n Title 812-0119-006 IRIX 5.3 812-0469-001 IRIX 6.2 1 OF 2 812-0470-001 IRIX 6.2 2 OF 2 812-0595-002 IRIX 6.3 812-0599-003 IRIX 6.3 APPLICATIONS 812-0637-005 02 IRIX 6.3 REC/REQ PATCHES 812-0616-002 IRIX 6.4 812-0758-002 IRIX 6.5 INSTALLATION TOOLS 812-0759-002 IRIX 6.5 FOUNDATION 1 812-0760-002 IRIX 6.5 FOUNDATION 2 812-0877-004 IRIX 6.5 A...

ABI information for 3.3 and 3.3.2
HI All, I'm looking to find out if there are any ABI differences between the VxWorks 3.3 and 3.3.2 tool chains regarding C++isms. My scenario is this: I am using the 3.3.2 tool chain to compile and link using VxWorks 5.5 headers and libraries against a mixture of C and C++. The second half of this equation factors in the fact tha I am also linking in 3rd party objects build and linked using against VxWorks 5.5 using the 5.5 libraries but built with the 3.3 tool chain. What I have here is really a difference in ABI's between the 3.3 (5.5) and 3.3.2 (.6.2) tools chains. Can anyone tel...

PIX 506E PDM 3.0(1) PIX 6.3(3) NAT/PAT (Part 3)
Hello, I have this strange problem and i can't seem to understand it. I have the following situation, i have been posting here before under the same name and subject. So you can read back. Though probably that is not needed. Internet (Zyxel P660HW) WAN : Public IP (natted) LAN : 192.168.168.1 subnet 255.255.255.252 Cisco Pix 506e WAN : 192.168.168.2 subnet 255.255.255.252 (natted) LAN : 192.168.68.8 subnet 255.255.255.0 Internal PC LAN 192.168.68.1 subnet 255.255.255.0 Now what i want is to run several services on my PC (server) DNS, HTTP, HTTPS, RDP, VPN, FTP, SMTP, POP3. Below is...

gcc: 3.3 vs 3.2.3
I've relegated 3.3 back to testing status, making 3.2.3 the "current" gcc again. "DJ Delorie" <dj@delorie.com> wrote in message news:200307010352.h613qRrl030701@envy.delorie.com... > > I've relegated 3.3 back to testing status, making 3.2.3 the "current" > gcc again. http://gcc.gnu.org says 3.3 is for recursion testing only. What's that mean? Bill -----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World! -----== Over 80,000 Newsgroups - 16 Different Ser...

[RELEASED] Release candidates for Python 2.6.8, 2.7.3, 3.1.5, and 3.2.3
We're pleased to announce the immediate availability of release candidates for Python 2.6.8, 2.7.3, 3.1.5, and 3.2.3 . The main impetus for these releases is fixing a security issue in Python's hash based types, dict and set, as described below. Python 2.7.3 and 3.2.3 include the security patch and the normal set of bug fixes. Since Python 2.6 and 3.1 are maintained only for security issues, 2.6.8 and 3.1.5 contain only various security patches. The security issue exploits Python's dict and set implementations. Carefully crafted input can lead to extremely long computation times a...

Problem installing matplotlib 1.3.1 with Python 2.7.6 and 3.3.3 (release candidate 1)
Hello, I tried to install matplotlib 1.3.1 on the release candidates of Python 2.7.6 and 3.3.3. I am on Mac OS X 10.6.8. Although the installation gave no problems, there is a problem with Tcl/Tk. The new Pythons have their own embedded Tcl/Tk, but when installing matplotlib it links to the Frameworks version of Tcl and TK, not to the embedded version. This causes confusion when importing matplotlib.pyplot: objc[70648]: Class TKApplication is implemented in both /Library/Frameworks/Python.framework/Versions/2.7/lib/libtk8.5.dylib and /Library/Frameworks/Tk.framework/Versions/8.5/Tk....

[RELEASED] Release candidates for Python 2.6.8, 2.7.3, 3.1.5, and 3.2.3
We're pleased to announce the immediate availability of release candidates for Python 2.6.8, 2.7.3, 3.1.5, and 3.2.3 . The main impetus for these releases is fixing a security issue in Python's hash based types, dict and set, as described below. Python 2.7.3 and 3.2.3 include the security patch and the normal set of bug fixes. Since Python 2.6 and 3.1 are maintained only for security issues, 2.6.8 and 3.1.5 contain only various security patches. The security issue exploits Python's dict and set implementations. Carefully crafted input can lead to extremely long computation times and denials of service. [1] Python dict and set types use hash tables to provide amortized constant time operations. Hash tables require a well-distributed hash function to spread data evenly across the hash table. The security issue is that an attacker could compute thousands of keys with colliding hashes; this causes quadratic algorithmic complexity when the hash table is constructed. To alleviate the problem, the new releases add randomization to the hashing of Python's string types (bytes/str in Python 3 and str/unicode in Python 2), datetime.date, and datetime.datetime. This prevents an attacker from computing colliding keys of these types without access to the Python process. Hash randomization causes the iteration order of dicts and sets to be unpredictable and differ across Python runs. Python has never guaranteed iteration order of keys in a dict or set,...

Browser speed test: IE, 59.7; FF3.03, 11.3; Chrome, 3.6; FF 3.1 Beta, 2.6
<Quote> ....there's a new Web speed-demon, Firefox 3.1 beta 1. [Slowed down by debug code...Turning on TraceMonkey...] Speaking of speed, let's get down to the numbers. I installed Firefox 3 Beta 1 on an older Gateway 503GR. This PC uses a 3GHz Pentium IV CPU, 2GB of RAM, an ATI Radeon 250 graphics card, and a 300GB SATA (Serial Advanced Technology Attachment) hard drive. On this, I was running XP SP3. I also ran Firefox 3 Beta 1 on my main openSUSE 11 PC, but since Chrome still doesn't run natively on Linux, I tested both browsers on the XP computer. I then put both brows...

[RELEASED] Second release candidates for Python 2.6.8, 2.7.3, 3.1.5, and 3.2.3
We're chuffed to announce the immediate availability of the second release candidates for Python 2.6.8, 2.7.3, 3.1.5, and 3.2.3. The only change from the first release candidates is the patching of an additional security hole. The security issue fixed in the second release candidates is in the expat XML parsing library. expat had the same hash security issue detailed below as Python's core types. The hashing algorithm used in the expat library is now randomized. A more thorough explanation of the "hash attack" security hole follows. The main impetus for these releases is fi...

[RELEASED] Second release candidates for Python 2.6.8, 2.7.3, 3.1.5, and 3.2.3
We're chuffed to announce the immediate availability of the second release candidates for Python 2.6.8, 2.7.3, 3.1.5, and 3.2.3. The only change from the first release candidates is the patching of an additional security hole. The security issue fixed in the second release candidates is in the expat XML parsing library. expat had the same hash security issue detailed below as Python's core types. The hashing algorithm used in the expat library is now randomized. A more thorough explanation of the "hash attack" security hole follows. The main impetus for these releases is fixing a security issue in Python's hash based types, dict and set, as described below. Python 2.7.3 and 3.2.3 include the security patch and the normal set of bug fixes. Since Python 2.6 and 3.1 are maintained only for security issues, 2.6.8 and 3.1.5 contain only various security patches. The security issue exploits Python's dict and set implementations. Carefully crafted input can lead to extremely long computation times and denials of service. [1] Python dict and set types use hash tables to provide amortized constant time operations. Hash tables require a well-distributed hash function to spread data evenly across the hash table. The security issue is that an attacker could compute thousands of keys with colliding hashes; this causes quadratic algorithmic complexity when the hash table is constructed. To alleviate the problem, the new releases add rando...

Cannot connect to 6.3 orbix server from orbixweb 3.3.4 client.
Hi All, I have ported my application to 6.3 orbix running on linux and it is a transient server registered with the naming service.I have used implicit activation(using _this()) on the server side. I have a orbix 3.3.4 client running on windows connecting to this 6.3 server. Im able to resolve the name using the naming service reference and even narrow it down to the correct type. I verified both the iors (using iordump) for the reference,on the server as well as the client side. They look alike except for the fact that one was Little Endian and the other was Big Endian. But when ...

Web resources about - HTDP - Ex 6.3.3. Assistance required - comp.lang.scheme

Election Assistance Commission - Wikipedia, the free encyclopedia
The Election Assistance Commission ( EAC ) is an independent agency of the United States government created by the Help America Vote Act of 2002 ...

Bundaberg police investigating death call for public assistance
Bundaberg police appeal for public assistance after fishermen discovered the body of a man in Mullet Creek, Yandaran, norther of Bundaberg.

Police repeat call for assistance in Andrew Carville murder investigation
Police repeat call for assistance in Andrew Carville murder investigation.

Julian Assange requests Australian passport, consular assistance after UN ruling
WikiLeaks founder Julian Assange called on Britain and Sweden to let leave the Ecuadorian embassy in London after a UN panel ruled he had been ...

Julian Assange requests Australian passport, consular assistance after UN ruling
WikiLeaks founder Julian Assange called on Britain and Sweden to let leave the Ecuadorian embassy in London after a UN panel ruled he had been ...


Democratic Election Assistance commissioner pushes back on state proof-of-citizenship requirements
Earlier this week, the Election Assistance Commission announced that it had granted requests from Kansas, Georgia and Alabama to require proof-of-citizenship ...

Obama giving $80 million in assistance to Flint
President Barack Obama announced Thursday that his administration is giving $80 million in aid to Michigan to help with repairing infrastructure ...

Soccer's rule-makers approve in-game trials with video assistance for referees
CARDIFF, Wales (AP) — Soccer's rule-makers approve in-game trials with video assistance for referees.

Ten foot Dusky Shark approaches diver for life saving assistance - Communities Digital News
Further proving we can live together with the sea’s most feared denizens, a 10 Dusky Shark with a stainless steel hook in it mouth, seeks help ...

Resources last updated: 3/5/2016 10:10:24 PM