2 ms·
Well, for starters, start env = function | [] -> () | line :: lines when String.length line > 4 && String.sub line 0 3 = "+++" ->
by sampo 12y ago
Well, for starters,
start env = function
| [] -> ()
| line :: lines
when String.length line > 4 && String.sub line 0 3 = "+++" ->
header env line;
modified env 0 lines
| _ :: lines -> start env lines
is a bit elitist way to write a 2 argument function as a combination of 1 argument function and a lambda function. So let me rewrite:
start (env) (list) = match list with
| [] -> ()
| line :: lines
when String.length line > 4 && String.sub line 0 3 = "+++" ->
header env line;
modified env 0 lines
| _ :: lines -> start env lines
So the function `start` takes some kind of state `env` as a first argument, and then it takes a list as a second argument, here named `list`.
If the list is empty, [], it does nothing.
If the first list element, which is a string, starts with "+++", it calls two other functions: first header(env,line) where `line` is now the first list element, and then modified(env,0,lines) where `lines` is the rest of the list, not containing the first element.
If the first list element does not start with +++, it recurses to the rest of the list.
So this is not a filter: After first encounter of "+++" it calls those two other functions and stops.
- mercurial 12y agoSounds a bit silly to write something like that by hand, when you could use BatList.drop_while (from batteries) like so: let line_is_not_header line = String.length line < 4 || String.sub line 0 3 != "+++" match (BatList.drop_while line_is_not_header lines) with | header_line :: following_lines -> header env header_line; modified env 0 following_lines | _ -> () In practice, I find that it's rarely useful to write recursive functions by hand, unless you're willing to limit yourself to the default stdlib.