Introduction
This functional challenge thread is intended to serve two goals:
Programming Challenge
Create a function called "recursive" that has a single parameter, a closure (lambda), that can be used to create recursive function variables for many recurrence relationship computations, for example:
Usage example 1: Factorial of n
5! = 5 * 4 * 3 * 2 * 1 = 120
[table="width: 500, class: outer_border, align: center"]
[tr]
[td]Note: This part of the above recursive function is the parameter value i.e. a closure (lambda) to calculate the n'th factorial
Which is essentially a ternary (if statement) that recursively calls the outer closure, resulting in the following composition through the recursion:
[/td]
[/tr]
[/table]
Please use the language you are most comfortable in to submit your solution. Ps. please also go ahead and submit a OOP styled solutions if you're more comfortable working with class types. Only proviso is that is it's flexible enough to solve all 3 problems without rewriting the
body or method signature.
Usage example 2: n'th Fibonacci number
12th Fibonacci = 89
Usage example 3: first n'th Even numbers as an Array
Array of the 4 positive Integer numbers above zero = [8, 6, 4, 2]
[table="width: 600, class: outer_border, align: center"]
[tr]
[td]Hint: The recursive function is a higher order function, meaning it takes a function ./ closure as it's single parameter; the recursion occurs naturally within the body of the function in that that the function calls itself during the computation of the closure.[/td]
[/tr]
[/table]
Second part scope
The second part of this thread is targeted to be beginner friendly and therefore I'm going to try to avoid making any assumptions. This part of the thread will cover areas like:
This functional challenge thread is intended to serve two goals:
- Challenge MyBB programmers to a "function" oriented programming problem.
- Provide a beginner friendly introduction to one of the key building blocks of programming: Functions, and to hopefully share some interesting ways in which these can be leveraged in your code.
Programming Challenge
Create a function called "recursive" that has a single parameter, a closure (lambda), that can be used to create recursive function variables for many recurrence relationship computations, for example:
Usage example 1: Factorial of n
5! = 5 * 4 * 3 * 2 * 1 = 120
PHP:
let factorial = recursive({ recurse in
{ n in n > 0 ? n * recurse(n - 1) : 1 }
})
print(factorial(5))
\\\ 120
[table="width: 500, class: outer_border, align: center"]
[tr]
[td]Note: This part of the above recursive function is the parameter value i.e. a closure (lambda) to calculate the n'th factorial
PHP:
{ recurse in
{ n in n > 0 ? n * recurse(n - 1) : 1 }
}
PHP:
5 * (5 - 1) * (4 - 1) * (3 - 1) * (2 - 1)
[/tr]
[/table]
Please use the language you are most comfortable in to submit your solution. Ps. please also go ahead and submit a OOP styled solutions if you're more comfortable working with class types. Only proviso is that is it's flexible enough to solve all 3 problems without rewriting the
body or method signature.
Usage example 2: n'th Fibonacci number
12th Fibonacci = 89
PHP:
typealias F3 = (x: Int, y: Int, n: Int)
let fibonacci = recursive({ (recurse: escaping (F3) -> F3) in
{ (e: F3) in e.n > 2 ? recurse((e.y, e.x + e.y, e.n - 1)) : (e.x, e.y, e.n) }
})
print(fibonacci(12))
\\\ 89
Usage example 3: first n'th Even numbers as an Array
Array of the 4 positive Integer numbers above zero = [8, 6, 4, 2]
PHP:
typealias E2 = (n: Int, v: [Int])
let even = recursive({
(recurse: escaping (E2) -> E2) in {
(e: E2) in e.n > 0 ? (e.n, e.v + recurse((e.n - 1, [2 * e.n])).v) : (e.n, e.v)
}
})
print(even((40, [])).1) \\ [80, 78, 76, 74, 72, 70, 68, 66, 64, 62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
\\\ returns the first 40 even numbers as an array.
[table="width: 600, class: outer_border, align: center"]
[tr]
[td]Hint: The recursive function is a higher order function, meaning it takes a function ./ closure as it's single parameter; the recursion occurs naturally within the body of the function in that that the function calls itself during the computation of the closure.[/td]
[/tr]
[/table]
Second part scope
The second part of this thread is targeted to be beginner friendly and therefore I'm going to try to avoid making any assumptions. This part of the thread will cover areas like:
- Define a function (and clarify how is it differs from a method)
- Function Application
- Function Composition
- Higher order functions
- What makes a function pure?
- Side effects
- Computing with effects
- Composition with effects
- Testing with effects.
- Functional Operators (I will also show how this can be achieved with methods i.e. in the absence of operator overloading):
- |> pipe forward (aka reverse application)
- <| pipe backward (aka normal application)
- >>> forward composition (occasionally this is also represented as >>): NB this is not bit shifting
- <<< reverse composition (occasionally this is also represented as <<): NB this is not bit shifting
- >=> fish operator (aka composition with effects)
Last edited:







