F# - how to chop a string to substrings of given length?

StringInstance.start..end is much more readable than String. Sub Here's what I came up with: let chop (input : string) len = seq { for start in 0 .. len .. input. Length / len do yield input.start..start + len - 1 } |> Seq.

To_array Or you can use: let chop (input : string) len = Array. Init (input. Length / len) (fun index -> let start = index * len input.start..start + len - 1).

StringInstance.start..end is much more readable than String.sub. Here's what I came up with: let chop (input : string) len = seq { for start in 0 .. len .. input. Length / len do yield input.start..start + len - 1 } |> Seq.

To_array Or you can use: let chop (input : string) len = Array. Init (input. Length / len) (fun index -> let start = index * len input.start..start + len - 1).

I like second one more, no loop, no yield, function for initialization. Although intervention on my example is also nice - looks more readable. – zendar Dec 29 '08 at 23:01.

Craig Stunz left answer here that is now missing. Anyway, he pointed to article about F# that have two functions for string manipulation: explode and implode. These two functions are from standard ML library.

Here's the code: let rec explode str = let len = String. Length str in if len=0 then else (String. Sub str 0 1) :: explode (String.

Sub str 1 (len-1)) let rec implode lst = match lst with -> "" | x1 :: x2 -> x1 ^ implode x2 explode chops string into string list where each string is one character. Implode does opposite - concatenates string list into one string. Both functions are recursive, so it would be interesting to compare performance.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions