F# List of String Manipulation

I am learning a valuable lesson about stack overflow – if I write down my question 1st and then search the key words using my favorite search engine, I can sometimes find the answer.

For example, I wanted to turn a list of string into 1 long string: ["A"; "B"; "C"] becomes “ABC”

I first tried via the functions found in the List class:

  1. let stringList = ["A"; "B"; "C"]
  2. let sumValues = List.sum stringList

 

The problem was I got the Red Squiggly Line Of Approbation on the stringList

 image

So then I typed a question for stack overflow with the words “string concatenation” and then I thought to try and try that into Google.  The 1st result was this: http://msdn.microsoft.com/en-us/library/ee353761.aspx

And low and behold, the example is exactly what I want to do

  1. let stringList = ["A"; "B"; "C"]
  2. let sumValues = String.concat "" stringList

image

I then applied that to a list of ints:

  1. let intList = [0..10]
  2. let concatValues =
  3.     intList
  4.     |> Seq.map string
  5.     |> String.concat ""

 

and after feeling good about figuring this out, I immediately began wondering how I can reduce that to 1 line of code

Smile

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: