I have a payment/lockbox file archiving utility that is called several times per day and with different parameters depending on the files that need archiving.

I know I can flatten argv into a string using a simple method like the following:

let r = Array.toList(argv)

let a = 
    |> String.concat " "

so the utility can send me email with the parameters used to call it without all the brackets from a string [].

However, I wanted to see if I could flatten argv using tail recursion, so is the following example tail-recursive?

let squash_str_array (str_arg : string[]) =
    let array_len = str_arg.Length
    let rec inner_squash (inner_str_arg : string []) (build_up_list : string) idx =
        if idx = array_len then
            let build_up_list = inner_str_arg.[idx] + " " + build_up_list
            inner_squash inner_str_arg build_up_list (idx + 1)

    inner_squash str_arg "" 0

let main argv =
    let test_array = [| "/mu"; "/al"|]
    let new_list = squash_str_array test_array
  • 1
    \$\begingroup\$ Yes, this is tail recursive, although a bit hard to read because of all the underscores in identifiers. Also, you're not using the array_idx parameter. \$\endgroup\$ – Fyodor Soikin Jul 13 '17 at 20:29
  • \$\begingroup\$ @FyodorSoikin I know in F# libraries, Visual Studio 2015 frowns on mixed case names, so what do you suggest for naming convention? \$\endgroup\$ – octopusgrabbus Jul 13 '17 at 20:31
  • 1
    \$\begingroup\$ javaCase is traditional for F# - e.g. arrayIdx, testArray, newArray, etc. \$\endgroup\$ – Fyodor Soikin Jul 13 '17 at 20:33
  • \$\begingroup\$ Also, Visual Studio doesn't down on mixed case. Where did you get that idea? \$\endgroup\$ – Fyodor Soikin Jul 13 '17 at 20:33
  • \$\begingroup\$ @FyodorSoikin When using mixed case in an F# library, Visual Studio popped up a warning. I've also edited the OP to remove the unwanted parameter (as well as in the test code). Thanks. \$\endgroup\$ – octopusgrabbus Jul 13 '17 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.