[plt-scheme] again it is newbie with ex 11.4.6 N[<=20]

From: arnuld (arnuld3 at gmail.com)
Date: Thu Jan 26 05:12:02 EST 2006

hello everybody,

                                 well, it's me again. i know what are you
thinking, this newbie is really totally
brain-less. but i have spent my 6 months of financial-sums to order HtDP
from USA and i want to solve 100% of exercises given in the book at any cost
even if i need to spend 8 hrs. in front my AMD64. Also Mr. Matthias is very
helpful regarding problems faced by newbies.

anyway here is the original exercise from the book:

*--- Exercise 11.4.6.*   In exercises
11.2.2<http://htdp.org/2003-09-26/Book/curriculum-Z-H-15.html#node_thm_11.2.2>,
11.4.4<http://htdp.org/2003-09-26/Book/curriculum-Z-H-15.html#node_thm_11.4.4>,
and 11.4.5<http://htdp.org/2003-09-26/Book/curriculum-Z-H-15.html#node_thm_11.4.5>,
we developed functions that tabulate the mathematical function f in various
ranges. In both cases, the final function produced a list of posns that was
ordered in *descending* order. That is, an expression like (tabulate-f
3)yields the list

(cons (make-posn 3 2.4)
  (cons (make-posn 2 3.4)
    (cons (make-posn 1 3.6)
      (cons (make-posn 0 3.0)
        empty))))

 If we prefer a list of posns in *ascending* order, we must look at a
different data collection, natural numbers up to a certain point in the
chain:

A *natural number [<= 20]* (*N*[<=20]) is either

   1.

   20 or
   2.

   (sub1 n) if n is a natural number [<= 20].

 Of course, in high school, we refer to *N*[<=-1] as *the* negative
integers.

Develop the function

;; tabulate-f-up-to-20 : *N* [<= 20]  ->  *N*
(define (tabulate-f-up-to-20 n-below-20) ...)

which tabulates the values of f for natural numbers less than 20.
Specifically, it consumes a natural number n less than or equal to 20 and
produces a list of posns, each of which has the shape (make-posn n (f
n))for some
n between 0 and n (inclusively). ----

i have developed the solution but according to the data-definition it is
wrong (i know it) but i am not able to solve it, even i have solved all the
previous problems of sec-11.

may you help me out?

------------------------ start
----------------------------------------------------------
;; A natural number [<=20] is either:
;;               1. 20, or
;;               2. (sub1 n) where nis a natural number[<=20]

;; in this case natural numbers end upto 0, because beyond zero starts the
series of -ve integers..
;; which we do not refer to as natural numbers in this case.

;; tabulate-f-upto-20 : N[<=20] -> list
;; to pruduce a list of posns in ascending order which has shape (make-posn
n (f n)) for N[<=20] and upto n only.
;; (define (tabulate-f-upto-20 n)..)


#| TEMPLATE
(define (tabulate-f-upto-20 n)
  (cond
    [(= n 20)...]
    [else....(tabulate-f-upto-20 (add1 n))...]))

;; (= n 20) it is because we want the list in ascending order
;; 2nd it exactly matches the data-definition

(tabulate-f-upto-20 20)
;; expected answer
empty

(tabulate-f20 5)
;; expected answer
(cons (make-posn 0 (f 0))
      (cons (make-posn 1 (f 1))
            (cons (make-posn 2 (f 2))
                  (cons (make-posn 3 (f 3))
                        (cons (make-posn 4 (f 4)) (cons (make-posn 5 (f 5))
empty)))))) |#


(define (tabulate-f-upto-20 n)
  (cond
    [(= n 20) empty]
    [else (cons (make-posn n (f n)) (tabulate-f-upto-20 (add1 n)))]))


(define (f x)
  (+ (* 3 (* x x))
     (+ (* -6 x)
        -1)))

;; tests
(tabulate-f-upto-20 20)
;; expected answer
'empty

(tabulate-f-upto-20 5)
;; expected answer
(cons (make-posn 0 (f 0))
      (cons (make-posn 1 (f 1))
            (cons (make-posn 2 (f 2))
                  (cons (make-posn 3 (f 3))
                        (cons (make-posn 4 (f 4)) (cons (make-posn 5 (f 5))
empty))))))

--------------------------- end
-------------------------------------------------------
OUPUT it produces:

'empty
-- in 2nd test-case it produces all posn from 5-20 not from 0-5 as problem
wants.--


--
"the great intellectuals"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20060126/73200ee8/attachment.html>

Posted on the users mailing list.