I have a simple function that lists every prime factor of a given input number. It is composed by:
let rec f n x a =
if x = n then
x::a
elif n % x = 0 then
f (n/x) x (x::a)
else
f n (x+1) a
let fact n = f n 2 []
fact 315
val factors : int list = [7; 5; 3; 3]
It works, but I would like to make of it a single function: how can I define fact with f directly nested in? I tried to apply the concept brilliantly expressed here, but I fail to abstract how to nest a three argument function (f) in a one arguments one (fact).
fact to a new line. Make sure that it's indented.fact and the function body you just moved.fun, and paste it into the blank line you created in the second step.fact.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With