CS/SICP in JS

연습문제 2.31

띵킹 2023. 3. 2. 14:34
const x = list(1,list(2, list(3,4),5), list(6,7));

function tree_map(func, tree){
    return map(sub_tree=> is_pair(sub_tree)
                        ? tree_map(func, sub_tree)
                        : func(sub_tree), tree);
}

function suquare_tree(tree){
    return tree_map(square,tree);
}

suquare_tree(x)//[1, [[4, [[9, [16, null]], [25, null]]], [[36, [49, null]], null]]];
728x90

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

연습문제 2.30  (0) 2023.03.02
연습문제 2.23  (0) 2023.02.27
연습문제 2.21  (0) 2023.02.27
연습문제 2.20  (0) 2023.02.26
연습문제 2.17  (0) 2023.02.26