CS/SICP in JS

연습문제 1.35

띵킹 2023. 2. 19. 15:09
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);
        return close_enough(guess, next)
            ? next
            : try_with(next);
    }
    return try_with(first_guess);
}

fixed_point(x => 1 + 1/x, 1.0);

 

728x90

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

연습문제 1.39  (0) 2023.02.19
연습문제 1.36  (0) 2023.02.19
연습문제 1.34  (0) 2023.02.19
연습문제 1.32, 1.33  (0) 2023.02.19
연습문제 1.31  (0) 2023.02.18