[plt-scheme] newbie's problem regarding ex-11.3.4

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Jan 26 08:11:31 EST 2006

On Jan 26, 2006, at 4:43 AM, arnuld wrote:

> hello,
>
>   ex-11.3.4 reads:
>
>  Exercise 11.3.4.   Develop the function create-prices, which consumes 
> a natural number and produces a list with a corresponding number of 
> prices between $.10 and $10.00 with increments of a dime.
>
> i think the it want to have a list in ascending order like this:
>
> (cons 0.10 (cons 0.20 (cons 0.30.........empty)))

1. You want (cons 10. (cons 9.9 (cons ... (cons .1 empty)))). Seriously.

> but i am getting exactly the opposite list. even after 3 hours of 
> brain-work i am not able to get the desired result. do you have any 
> idea?

2. For the opposite order you have to wait until part IV (or part VI to 
understand why it works) or be "wild".

-- Matthias


>
> -------------------------------- start of programme 
> ---------------------------
> ;; A natural number is either:
> ;;             1. 0, or
> ;;             2. (add1 n) where n is a natural number
>
> ;; create-prices : N -> list
> ;; to produce a list of prices of n elements with each next elemnt is 
> a dime more than the previuos.
> ;; (define (create-prices n)...)
>
> #| TEMPLATE
> (define (create-prices n)
>   (cond
>     [(zero? n)...]
>     [else....(create-prices (sub1 n))...]))
>
> examples
> (create-prices 0)
> ;; expected answer
> empty
>
> (create-prices 3)
> ;; expected answer
> (cons 0.10 (cons 0.20 (cons 0.30 empty))) |#
>
> (define (create-prices n)
>   (cond
>     [(zero? n) empty]
>     [else (cons (* n 0.10) (create-prices (sub1 n)))]))
>
>
> ;;(make-list -2 3)
>
> ;; i have created a function whch checks for values > 10 and removes 
> them as we only want between
> ;; 0.10 and 10.0
>
> (define (remove>10 alon)
>   (cond
>     [(empty? alon) empty]
>     [(> (first alon) 10.0) (remove>10 (rest alon))]
>     [else (cons (first alon) (remove>10 (rest alon)))]))
>
>
>  
> ;; tests
> ;;(create-prices 0)
> ;; expected answer
> empty
>
> (create-prices 3)
> ;; expected answer
> (cons 0.10 (cons 0.20 (cons 0.30 empty)))
>
> -------------------------------- end of programme 
> ----------------------------------------------------------
> OUTPUT:
>
> well, this is what i get:
> -------------------------------------------------------------
> Welcome to DrScheme, version 209.
> Language: Beginning Student.
> Teachpack: /usr/lib/plt/teachpack/htdp/draw.ss.
> empty
> empty
> (cons 0.3 (cons 0.2 (cons 0.1 empty)))
> (cons 0.1 (cons 0.2 (cons 0.3 empty)))
> >
> -----------------------------------------------------------------
>
> -- 
> "the great 
> intellectuals"_________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.