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 arguments bar , 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 calling bar argument baz"

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 calling zeno argument (- n 1) no arguments."

what seem mean is

"add 1 divided 2^n result of calling zeno 1 less n"

which means should is

(define (zeno n)     (if (= n 0)         0         (+ (/ 1 (expt 2 n))            (zeno (- n 1))))) 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -