Results 1 to 1 of 1

Thread: [RESOLVED] Experimenting with functional programming.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Resolved [RESOLVED] Experimenting with functional programming.

    I have been experiment with functional programming. I made a function that escaped non-displayable characters in a string and uses string concatenation to create a new string containing the result ("Escaped"):

    Code:
    open System
    
    //This function converts non-displayable characters in the specified text to escape sequences.
    let Escape (text: String) = 
       let mutable Escaped = ""
    
       text.ToCharArray() |> Array.iter(
             fun character -> 
             Escaped <- Escaped +
             match character with
             | character when character = '\t' || character >= ' ' -> character |> string
             | _ -> "/" + String.Format("{0:X}", (character |> byte)).PadLeft(2, '0')
          )
    
       Escaped
    
    [<EntryPoint>]
    let main argv = 
        printfn "%s" (Escape("ASCII character #3: " + ((3 |> char) |> string) + "."))
        0
    Using a mutable string is a non-functional approach. Does any one here know how I can make this function so it's completely written in a functional programming style? I'm using F# in Microsoft Visual Studio btw.

    EDIT:
    I decided the use a StringBuilder object, although I suppose I could a recursive function as well.
    Last edited by Peter Swinkels; Feb 12th, 2017 at 07:15 AM. Reason: Found a sollution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width