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
fooargumentsbar,baz"
you can't add parentheses please; change meaning of expression. example, ((foo) (bar baz)) means
"call function
foono arguments, , call result result of callingbarargumentbaz"
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 callingzenoargument(- n 1)no arguments."
what seem mean is
"add
1divided2^nresult of callingzeno1 lessn"
which means should is
(define (zeno n) (if (= n 0) 0 (+ (/ 1 (expt 2 n)) (zeno (- n 1)))))
Comments
Post a Comment