CS/SICP in JS

연습문제 1.36

띵킹 2023. 2. 19. 15:21
const tolerance = 0.0001;

function abs(x){
    return x > 0 ? x : -x;
}

function fixed_point(f, first_guess) {
    function close_enough(x, y) {
        return abs(x - y) < tolerance;
    }
    function try_with(guess) {
        const next = f(guess);
        display(next);
        return close_enough(guess, next)
            ? next
            : try_with(next);
    }
    return try_with(first_guess);
}

function average(x,y) {
    return (x+y)/2; 
}

fixed_point(x=> math_log(1000)/math_log(x), 4);

fixed_point(x=> average(x, math_log(1000)/math_log(x)), 4);

 

728x90

'CS > SICP in JS' 카테고리의 다른 글

연습문제 1.40  (0) 2023.02.20
연습문제 1.39  (0) 2023.02.19
연습문제 1.35  (0) 2023.02.19
연습문제 1.34  (0) 2023.02.19
연습문제 1.32, 1.33  (0) 2023.02.19