[plt-scheme] multiply-by-pi problem
hai there,
again i am on section-11 of HtDP i did create 2 programmes (ex 11.5.1
& 11.5.2) whch work as desired but failed to create a programme based
on those 2. i created "add & multiply" which add and multiply 2
numbers without using + and * but i am not able to create
"multiply-by-pi". i am providing here the full programmes with problem
statements except for failed one for which i am able to provide
data-definition and template only.
i will appreciate any help from anybody.
thanks
"arnuld"
----------------------------------------------------
Exercise 11.5.1. Define add, which consumes two natural numbers, n
and x, and produces n + x without using Scheme's +.
Exercise 11.5.2. Develop the function multiply-by-pi, which consumes
a natural number and multiplies it by 3.14 without using *. For
example,
(= (multiply-by-pi 0) 0)
(= (multiply-by-pi 2) 6.28)
(= (multiply-by-pi 3) 9.42)
Define multiply, which consumes two natural numbers, n and x, and
produces n * x without using Scheme's *. Eliminate + from this
definition, too.
Hint: Recall that multiplying x by n means adding x to itself n times.
;;------------ failed attempt of ex 11.5.2's 1st part (multiply-by-pi)
-------------------------------
;; a natural number is either:
;; 1. 0, or
;; 2. (add1 n), where n is a natural number
;; multiply-by-pi : N -> number
;; to multiply n by pi without using *.
;; (define (multiply-by-pi n)..)
#| TEMPLATE
(define (multiply-by-pi n)
(cond
[(zero? n)....]
[else...(multiply-by-pi (sub1 n))....]))
;; examples
(= (multiply-by-pi 0) 0)
(= (multiply-by-pi 2) 6.28)
(= (multiply-by-pi 3) 9.42) |#
;;-------------- solution to ex 11.5.2's 2nd part (multiply two
numbers) -------------------------------
;; multiply : N, N -> N
;; to multiply n & x natural numbers without using *.
;; (define (multiply n x)..)
#| TEMPLATE
(define (multiply n x)
(cond
[(zero? n)...]
[else ...(multiply (sub1 n) x)...]))
;; examples
(= 30 (multiply 6 5))
(= 0 (multiply 6 0))
(= 0 (multiply 0 5))
(= 0 (multiply 0))
(= 63 (multiply 9 7)) |#
(define (multiply n x)
(cond
[(zero? n) 0]
[else (add x (multiply (sub1 n) x))]))
;; tests
(= 30 (multiply 6 5))
(= 0 (multiply 6 0))
(= 0 (multiply 0 5))
(= 0 (multiply 0))
(= 63 (multiply 9 7))
;;----------------------- solutionto ex 11.5.1 (add two numbers)
--------------------------------------
;; add : N, N -> number
;; to compute n + x without using +
;; TEMPLATE
#| (define (add n x)
(cond
[(zero? n)...]
[else....(add (sub1 n) x)...]))
but i did not understand one thing when < hellos > uses one input and
< add > uses 2 inputs then why templates in both
cases match? |#
(define (add n x)
(cond
[(zero? n) x]
[else (add1 (add (sub1 n) x))]))
;; tests
(= 8 (add 0 8))
(= 3 (add 3 0))
(= 11 (add 3 8))
(= 0 (add 0 0))
(= (+ 20 67) (add 20 67))
;; ------------- default programme from HtDP (add-to-pi)
-----------------------------
;; add-to-pi : N -> number
;; to compute n + 3.14 without using +
#| TEMPLATE
(define (add-to-pi n)
(cond
[(zero? n) ...]
[else ... (add-to-pi (sub1 n)) ... ]))) |#
(define (add-to-pi n)
(cond
[(zero? n) 3.14]
[else (add1 (add-to-pi (sub1 n)))])
;; tests
(= (add-to-pi 0) 3.14)
(= (add-to-pi 2) 5.14)
(= (add-to-pi 6) 9.14)
;;---------------------- end of programme
----------------------------------------
--
"the great intellectuals"