@@ -387,6 +387,45 @@ function f(x) {
387
387
</SNIPPET >
388
388
</LI >
389
389
</OL >
390
+ <SOLUTION >
391
+ Part (a)
392
+ <SNIPPET >
393
+ <JAVASCRIPT >
394
+ // solution provided by GitHub user LucasGdosR
395
+
396
+ // The Fibonacci function receives n as an argument
397
+ // It applies the fib function recursively, passing n as an argument,
398
+ // as well as the initial arguments (k = 1, fib1 = 1, fib2 = 1)
399
+ (n => (fib => fib(fib, n, 2, 1, 1))
400
+ // The fib function is then defined as ft,
401
+ // with parameters n, k, fib1, and fib2
402
+ // Establish the base cases: n === 1 or n === 2
403
+ ((ft, n, k, fib1, fib2) => n === 1
404
+ ? 1
405
+ : n === 2
406
+ ? 1
407
+ :
408
+ // Iterate until k equals n. Notice k starts at 2, and gets incremented every iteration
409
+ k === n
410
+ // When k reaches n, return the accumulated fib2
411
+ ? fib2
412
+ // Otherwise, accumulate the sum as the new fib2
413
+ : ft(ft, n, k + 1, fib2, fib1 + fib2)));
414
+ </JAVASCRIPT >
415
+ </SNIPPET >
416
+ Part (b)
417
+ <SNIPPET >
418
+ <JAVASCRIPT >
419
+ // solution provided by GitHub user LucasGdosR
420
+
421
+ function f(x) {
422
+ return ((is_even, is_odd) => is_even(is_even, is_odd, x))
423
+ ((is_ev, is_od, n) => n === 0 ? true : is_od(is_ev, is_od, n - 1),
424
+ (is_ev, is_od, n) => n === 0 ? false : is_ev(is_ev, is_od, n - 1));
425
+ }
426
+ </JAVASCRIPT >
427
+ </SNIPPET >
428
+ </SOLUTION >
390
429
</EXERCISE >
391
430
</JAVASCRIPT >
392
431
</SPLIT >
0 commit comments