Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 595

Iterating over split string in OCaml

$
0
0

Let's say I have a string:

"ab bc cdv gfed    aqb ahf sdabcdef

I want toa) Split it by '' and/or '\r\n', '\t'b) Iterate over newly created list of these substrings, split by separators and match each of them to some criteria (for example, only choose words starting with 'a', aka ["ab", "ahf", "abcdef"]

Note: also we can't use Str or any other additional libraries.

I came up with some sort of this code:

let f g =  String.split_on_char '' g  |> List.iter (fun x -> x);;

Obviously though, it shows an error. And even if it worked, it wouldn't have split out the '\r\n'. Instead of List.iter I could have used List.map (fun x -> x), but I will just get the split (by '' character only) list of substrings. So now another question: how can I use

"match (something?) with| ..." 

in this case? I see no way in adding match into the code above. Do we use the reverse |> and List.iter in this case or is there another way I'm not aware of?


Viewing all articles
Browse latest Browse all 595

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>