Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm pretty sure the Ocaml code is doing more than the Python code you posted. Hopefully someone who knows Ocaml can explain it to us and then we can write the actual python equivalent.


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.


Sounds 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.


In python3 something like:

    def start(env, lines):
        while lines:
            line, *lines = lines
            if len(line) > 4 and line.startswith("+++"):
                header(env, line)
                modified(env, 0, lines)
Notes:

    while lines:
is pythonic for

    while not lines == []:


Correction (I forgot the break):

    def start(env, lines):
        while lines:
            line, *lines = lines
            if len(line) > 4 and line.startswith("+++"):
                header(env, line)
                modified(env, 0, lines)
                break


Err. Ok. Just look at this. Is that concise?

    when String.length line > 4 && String.sub line 0 3 = "+++"
Specifying numbers like that in the code? Allocating an object? Wow. I don't understand why anyone wouldn't prefer to use something like:

    line.startswith("+++")


The ocaml standard library is minimal to a fault. It's just enough to implement the compiler itself. For higher level operations like starts_with, you use libraries. The Batteries library for example provides what you want:

    String.starts_with line "+++"


Note, that the original code reads:

    when String.length line > 4 && String.sub line 0 3 = "+++"
And the Batteries library is not being used in the original code. If that doesn't tell you anything, I'm sorry. And good luck.


It's a problem. Until people have standardized on which 'better standard library' to use, programs will keep building from these too low level building blocks.


It is a style, not a problem. OCaml had been there a long long time. I've brushed with it a couple of times over the years, and every time the same thing.

You look at some random OCaml code, and it looks like this:

    when String.length line > 4 && String.sub line 0 3 = "+++"
I'm looking at it, and I'm not even sure - is it a bug there? Should it be '>=3' or they've really wanted to say '> 4'. Numbers all other the place? Ten different variants of equal signs?


You can call it a style if that makes a difference. I think there are reasons behind that style, and I think the too minimal standard library is the main reason. The lack of a good package manager until recently may be another contributing reason.

I hesitate to answer your hyperbole about ten variants of equal signs. It's not uncommon for languages to have two notions of equality like ocaml has (or even more): Here's Python for example: http://stackoverflow.com/questions/132988/is-there-a-differe...

To answer your original question of why anyone would prefer to not use startswith: I don't think anyone does.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: