F# List of String Manipulation
September 24, 2013 Leave a comment
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:
- let stringList = ["A"; "B"; "C"]
- let sumValues = List.sum stringList
The problem was I got the Red Squiggly Line Of Approbation on the stringList
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
- let stringList = ["A"; "B"; "C"]
- let sumValues = String.concat "" stringList
I then applied that to a list of ints:
- let intList = [0..10]
- let concatValues =
- intList
- |> Seq.map string
- |> String.concat ""
and after feeling good about figuring this out, I immediately began wondering how I can reduce that to 1 line of code