lisp - Error: (/) bad argument type: #<unspecified> Chicken Scheme Square root approximation -


i following sicp lectures mit, , tried find square root approximation of number heron of alexandria's method. first time trying out lisp, sorry making noobie mistakes.

(define guess 1)  (define (avg b)   (/ (+ b) 2))  (define (try guess x)   (if (goodenough guess x)       guess       (improve guess x)))  (define (improve guess x)   (define guess (avg guess (/ x guess)))   (try guess x)   )  (define (goodenough guess x)   (= guess (avg guess (/ x guess))))  (print (try 1 25)) 

i using chicken scheme compiler print this. output:

error: (/) bad argument type: #<unspecified>      call history:      1.a.squarerootapproximation.scm:29: try        1.a.squarerootapproximation.scm:17: goodenough         1.a.squarerootapproximation.scm:27: avg        1.a.squarerootapproximation.scm:19: improve     <-- 

updated: have changed approach towards problem using lisp more abstraction, yet can't figure out new error wants imply. fixes? thanks!

the value #<unspecified> "void" in other languages. used return value whenever procedure has nothing useful return (for example, print return this). in situations used temporary placeholder value, example when handling inner define.

normally temporary placeholder should not visible user of language, appears you've hit strange edge case in language (congratulations! happens rarely). error happens because (define guess (avg guess (/ x guess))) in improve procedure simultaneously defining variable , using variable. behaviour of doing not well-specified, , scheme implementations chicken doing (guile, gauche, gambit) whereas others give more meaningful error message (mit, scheme48, racket). reason ill-specified has fact inner define expands letrec, because allows mutually recursive procedures defined, creates bit of issue: should happen (define b) (define b a), example?

your intention seems using old guess variable that's passed input procedure, instead of using define use let bind new value guess (how should behave well-specified), or use different name it, new-guess.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -