Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:09:39+00:00 2026-06-11T11:09:39+00:00

I am currently doing reasonably well in functional programming using F#. I tend, however,

  • 0

I am currently doing reasonably well in functional programming using F#. I tend, however, to do a lot of programming using recursion, when it seems that there are better idioms in the F#/functional programming community. So in the spirit of learning, is there a better/more idiomatic way of writing the function below without recursion?

let rec convert line =
    if line.[0..1] = "  " then
        match convert line.[2..] with
        | (i, subline) -> (i+1, subline)
    else
        (0, line)

with results such as:

> convert "asdf";;
val it : int * string = (0, "asdf")
> convert "  asdf";;
val it : int * string = (1, "asdf")
> convert "      asdf";;
val it : int * string = (3, "asdf")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:09:41+00:00Added an answer on June 11, 2026 at 11:09 am

    Recursion is the basic mechanism for writing loops in functional languages, so if you need to iterate over characters (as you do in your sample), then recursion is what you need.

    If you want to improve your code, then you should probably avoid using line.[2..] because that is going to be inefficient (strings are not designed for this kind of processing). It is better to convert the string to a list and then process it:

    let convert (line:string) = 
      let rec loop acc line =
        match line with
        | ' '::' '::rest -> loop (acc + 1) rest
        | _ -> (acc, line)
      loop 0 (List.ofSeq line)
    

    You can use various functions from the standard library to implement this in a more shorter way, but they are usually recursive too (you just do not see the recursion!), so I think using functions like Seq.unfold and Seq.fold is still recursive (and it looks way more complex than your code).

    A more concise approach using standard libraries is to use the TrimLeft method (see comments), or using standard F# library functions, do something like this:

    let convert (line:string) =
      // Count the number of spaces at the beginning
      let spaces = line |> Seq.takeWhile (fun c -> c = ' ') |> Seq.length
      // Divide by two - we want to count & skip two-spaces only
      let count = spaces / 2
      // Get substring starting after all removed two-spaces
      count, line.[(count * 2) ..]
    

    EDIT Regarding the performance of string vs. list processing, the problem is that slicing allocates a new string (because that is how strings are represented on the .NET platform), while slicing a list just changes a reference. Here is a simple test:

    let rec countList n s = 
      match s with 
      | x::xs -> countList (n + 1) xs
      | _ -> n
    
    let rec countString n (s:string) =
      if s.Length = 0 then n
      else countString (n + 1) (s.[1 ..])
    
    
    let l = [ for i in 1 .. 10000 -> 'x' ]
    let s = new System.String('x', 10000)
    
    #time 
    for i in 0 .. 100 do countList 0 l |> ignore    // 0.002 sec (on my machine)
    for i in 0 .. 100 do countString 0 s |> ignore  // 5.720 sec (on my machine)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently doing a console app that has to send scheduled emails. In one
I'm currently doing the following to compensate for boolean's not mapping well to radio
I am currently doing a C# WPF application that generates a table that does
I'm currently doing the calculator app that has the graph functionality. So then, I
I am currently doing an android application that contains customize alert dialog. It contains
I am currently testing an IPN script in PHP, using Paypal's sandbox. It seems
I'm currently using a free host that doesn't allow https connections, and since my
I've been using Git reasonably successfully (perhaps that's an optimistic evaluation) for going on
I currently doing some model transformations using EMF-UML-Implementation. In my model transformation I create
I'm currently doing facebook integration and have gone through this tutorial . So far,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.