From be8b528b5cc878f7576a5b7d98bb457e9099662c Mon Sep 17 00:00:00 2001 From: Martin Henz Date: Thu, 11 Jul 2024 02:08:46 +0800 Subject: [PATCH] fixes #986 (#1044) --- xml/chapter4/section1/subsection6.xml | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/xml/chapter4/section1/subsection6.xml b/xml/chapter4/section1/subsection6.xml index 69e7c334a..ab112127d 100644 --- a/xml/chapter4/section1/subsection6.xml +++ b/xml/chapter4/section1/subsection6.xml @@ -387,6 +387,45 @@ function f(x) { + + Part (a) + + +// solution provided by GitHub user LucasGdosR + +// The Fibonacci function receives n as an argument +// It applies the fib function recursively, passing n as an argument, +// as well as the initial arguments (k = 1, fib1 = 1, fib2 = 1) +(n => (fib => fib(fib, n, 2, 1, 1)) + // The fib function is then defined as ft, + // with parameters n, k, fib1, and fib2 + // Establish the base cases: n === 1 or n === 2 + ((ft, n, k, fib1, fib2) => n === 1 + ? 1 + : n === 2 + ? 1 + : + // Iterate until k equals n. Notice k starts at 2, and gets incremented every iteration + k === n + // When k reaches n, return the accumulated fib2 + ? fib2 + // Otherwise, accumulate the sum as the new fib2 + : ft(ft, n, k + 1, fib2, fib1 + fib2))); + + + Part (b) + + +// solution provided by GitHub user LucasGdosR + +function f(x) { + return ((is_even, is_odd) => is_even(is_even, is_odd, x)) + ((is_ev, is_od, n) => n === 0 ? true : is_od(is_ev, is_od, n - 1), + (is_ev, is_od, n) => n === 0 ? false : is_ev(is_ev, is_od, n - 1)); +} + + +