7 ms·
What happens if the loop body does not assign to the result variable? What if the condition is false before entering the loop? I think it would have been clea
by pwdisswordfish8 5y ago
What happens if the loop body does not assign to the result variable? What if the condition is false before entering the loop?
I think it would have been cleaner to pass the value the loop should return to a break primitive and otherwise compute it in a dedicated block.
- iamevn 5y agoMy understanding is that a while (or a valof) that doesn't assign to result is invalid. If the condition is false before entering the loop then result would be 1 (its initial value) if it helps and if I understand correctly, this would be the same algorithm expressed in scheme: (define (fib n) (let loop ([i 1] [f 1] [pf 1]) (if (< i n) (loop (+ i 1) (+ f pf) f ) f ))) ((let name args body) is essentially sugar for creating a function and calling it with some default args)
- iamevn 5y ago(code with formatting http://pasterack.org/pastes/80204 http://pasterack.org/pastes/80204 )
- bade 5y agoI wouldn't use the word "assign" these are definitions and their order is not significant. You're right, a while without a definition of result produces a syntax error. If the condition is initially false, the definition of result is immediately evaluated. Looks like I fooled some people into assuming that whiles are imperative. They're not