Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution to Exercise 4.18 #986

Closed
LucasGdosR opened this issue Apr 1, 2024 · 0 comments · Fixed by #1044
Closed

Solution to Exercise 4.18 #986

LucasGdosR opened this issue Apr 1, 2024 · 0 comments · Fixed by #1044
Labels
Exercise solution Solutions to textbook exercises

Comments

@LucasGdosR
Copy link

LucasGdosR commented Apr 1, 2024

// Fibonacci
// The first 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)));

// Is even
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));
}

martin-henz added a commit that referenced this issue Jul 1, 2024
@martin-henz martin-henz added the Exercise solution Solutions to textbook exercises label Jul 1, 2024
@RichDom2185 RichDom2185 linked a pull request Jul 10, 2024 that will close this issue
RichDom2185 pushed a commit that referenced this issue Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Exercise solution Solutions to textbook exercises
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants