recursion - Scheme: recursive zeno function -
the following function sums 1/2^n until reaches 0 . can tell me whether or not need else statement, , how arrange parenthesis there no compile errors?
(define (zeno n) (if (= n 0) (+ 0) (else ((+ (/ 1 (expt 2 n ))) ((zeno(- n 1)))))))
i have hat, tea, , nothing while @ work. know time is.
[dons code-review hat]
firstly, if had properly indented code, read
(define (zeno n) (if (= n 0) (+ 0) (else ((+ (/ 1 (expt 2 n))) ((zeno (- n 1)))))))
if
in scheme not if
/then
/else
construct of c-like languages. it's ternary operator. in other words, else
makes no sense in context.
(define (zeno n) (if (= n 0) (+ 0) ((+ (/ 1 (expt 2 n))) ((zeno (- n 1)))))))
you don't need use +
return number; numbers self-evaluating.
(define (zeno n) (if (= n 0) 0 ((+ (/ 1 (expt 2 n))) ((zeno (- n 1)))))))
when have expression (foo bar baz)
, means
"call function
foo
argumentsbar
,baz
"
you can't add parentheses please; change meaning of expression. example, ((foo) (bar baz))
means
"call function
foo
no arguments, , call result result of callingbar
argumentbaz
"
in other words,
... ((+ (/ 1 (expt 2 n))) ((zeno (- n 1))))))
what you're saying, , don't mean, here
"call function
(+ (/ 1 (expt 2 n)))
result of calling result of callingzeno
argument(- n 1)
no arguments."
what seem mean is
"add
1
divided2^n
result of callingzeno
1 lessn
"
which means should is
(define (zeno n) (if (= n 0) 0 (+ (/ 1 (expt 2 n)) (zeno (- n 1)))))
Comments
Post a Comment