Recursive function basics
In this chapter, I want to introduce you to the basics of recursive functions, leaving more detailed consideration for the more advanced contexts. At this point, I want to show how the F# default treatment of functions as non-recursive differs from a forced one when the function is explicitly declared recursive using the let
binding modifier, rec
.
Take a look at the following far-fetched snippet (Ch3_8.fsx
):
let cutter s = let cut s = printfn "imitator cut: %s" s let cut (s: string) = if s.Length > 0 then printfn "real cut: %s" s cut s.[1..] else printfn "finished cutting" cut s
The cutter
function here provides a non-empty string that's supposed to cut it from the left-hand side, symbol by symbol, until the argument is gone. Within the cutter
body, there are two definitions of the cut
internal function, of which the second definition apparently shadows the first. Also, it's important that within the second cut
definition...