[plt-scheme] solution to HTDP exercise-10.1.4
> Hi everybody,
>
> I am providing here help on solving exercises of
> HTDP
> (have a look @ the PREFACE of the book @ htdp.org
> and you
> will never leave it).
>
> My purpose is NOT PROVIDING solutions to exercises
> in the book (they are already provided @ htdp.org)
> but to SHOW that our way of dwelling our BRAINS
> into solving problems is LESS PRODUCTIVE than if
> we follow the DESIGN-PROCESS (the core of HTDP) and
> I show
> this not only by showing you the SOURCE-CODE of the
> solution i have developed BUT ALSO by providing
> PRECISE EXPLANATION on WHAT different THINGS
> represent
> and how they solved the problm and i do this using
> comments and little but exact explanations (I do not
> TRUST too much of theory, looking at the SOURCE-CODE
> and working through it is MUCH BETTER and
> PRODUCTIVE).
> If programming only meant solving problems then it
> could have been easier (it is interesting but it is
> a
> different case) but it is not as it does not only
> mean solving problems but solving WHICH problems?
> Every BUG in a programme has as many ways of
> debugging as number of people but CHOOSING a WAY,
> A PROCESS to debug better than all others is the
> DESIGN-PROCESS (I am sorry there is no BEST way to
> debug
> a programme as at every new confrontation we may get
> a
> new solution which can solve the problem better) . I
> have
> many HOURS & DAYS with my head banging into problems
> constantly and in the end I came to know my
> "CONCEPT of PROBLEM" was wrong. When i thought in a
> differnet way i found the solution in a minute. this
> is
> WHOLE MOTIVATION behind bringing this email you.
> SINCE i am just a
> NEWBIE learning throught book HTDP i can only help
> you if you are
> NEWBIE @htdp. This email is of NO-IMPORTANCE to a
> programmer.
>
> for PROGRAMMERS only one thing can be useful which
> is the "CONCEPT of
> DESIGN-PROCESS" (which is explained better @
> htdp.org) as it is
> independent of LANGUAGE. DSIGN-PROCESS is
> PURE-PROGRAMMING and it makes
> you more productive by chnaging you from SYNTAX
> oriented to PROGRAMMING
> oriented. I am not SURE whether it will help you or
> not but if it does
> please send me a THANKS note so that i can know i
> have made a small
> difference to someone's life. (actually someone
> helped me now i am
> returning the favour as it happens in "Free-Software
> world", the
> sharing of thoughts and ideas)
>
> Ok now the exercis and solution using
> DESIGN-PROCESS:
>
> 1.) FIRST please have look at the EXERCISE STATEMENT
> in the book, then
>
> 2.) WRITE solution by YOURSELF, and in the end
>
> 3.) CHECK my solution and watch the difference (if
> any), see if you can
> make out something out of TWO SOLUTIONS.
>
> ************ EXERCISE 10.1.4
> **************************
>
> ;; HOW I used DESIGN-PROCESS to approach "ex:
> 10.1.4" of HTDP
> ;; <have a look @ PREFACE of the book @ htdp.org &
> you will never leave
> it>
>
> ;;######################### COMMENTS
> ################################
>
> ;; this is about:
> ;; how I GENERLISED an EXISTING function to generate
> a new one.
> ;; also have a look at the use of AUXILIARY
> functions
>
>
;;###################################################################
>
> ;; DATA-DEFINITIONS
> ;; A list-of-euros(numbers) is either:
> ;; 1. an empty list, <empty>, or
> ;; 2. <(cons n loe)>, where n= number, loe=
> list-of-euros(numbers)
>
> ;; CONTRACT, PURPOSE and HEADER:
> ;; convert-euro : list-of-numbers -> list-of-numbers
> ;; to create a list-of-dollers from list-of-euros
> ;; (define (convert-euro lon)...)
>
> ;; EXAMPLES
> ;; (convert-euro empty) "= empty"
> ;; (convert-euro (cons 20 (cons 10 (cons 1 empty))))
> "=
>
> ;; (cons (* 1.22 20) (cons (* 1.22 10) (cons (* 1.22
> 1) empty)))"
>
> ;; TEMPLATE: euro-template : lon -> ???
> ;; (define (euro-template lon)
> ;; (cond
> ;; [(empty? (first lon))...]
> ;; [else...(first lon).....(euro-template (rest
> lon))....]))
>
> (define (convert-euro a-lon)
> (cond
> [(empty? a-lon) empty]
> [else (cons (euro->doller (first a-lon))
> (convert-euro (rest
> a-lon)))]))
>
> (define (euro->doller euro)
> (* 1.22 euro))
>
> ;; TEST
> (convert-euro empty) "= empty"
> (convert-euro (cons 20 (cons 10 (cons 1 empty))))
> "="
> (cons (* 1.22 20) (cons (* 1.22 10) (cons (* 1.22 1)
> empty)))
>
> ;; now I will GENERLISE this function into
> <convert-euro-1> which WILL
> take 2 arguments.
>
>
"***************************************************************************"
>
> (define (convert-euro-1 ex-rate a-lon)
> (cond
> [(empty? a-lon) empty]
> [else (cons (euro->doller-1 ex-rate (first
> a-lon)) (convert-euro-1
> ex-rate (rest a-lon)))]))
>
> (define (euro->doller-1 ex-rate doller)
> (/ doller ex-rate))
>
> ;; TEST-1
> (convert-euro-1 1.22 (cons 20 (cons 10 empty))) "="
> (cons (/ 20 1.22)
> (cons (/ 10 1.22) empty))
>
> ;;##################### OUTPUT
> ########################
> Welcome to DrScheme, version 209.
> Language: Beginning Student.
> empty
> "= empty"
> (cons 24.4 (cons 12.2 (cons 1.22 empty)))
> "="
> (cons 24.4 (cons 12.2 (cons 1.22 empty)))
>
"************************************************************************"
> (cons 16.3934426229508196721311475... (cons
> 8.1967213114754098360655737... empty))
> "="
> (cons 16.3934426229508196721311475... (cons
> 8.1967213114754098360655737... empty))
>
> ;;#################### E. O. P
> ########################
>
> ;; EOP means "END OF PROGRAMME", it is the one i
> like to use instead
> of traditional EOF.
>
>
"the Great Intellectuals"
__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com