
OCaml Briefly - a0
http://mads379.github.io/ocaml/2014/11/13/ocaml-briefly.html
======
glifchits
After seeing that Facebook's Flow tool was written with an OCaml parser, I was
quite impressed by the beauty of its syntax. It makes functional programming
feel much less intimidating, compared to Haskell or a Lisp in my opinion, and
I can see the parallels to F#. Thanks for sharing this introduction, it was
very apropos for me!

~~~
pseudonom-
Care to elaborate on what you like about its syntax? I can imagine preferring
it to Haskell or Lisp, but not thinking it's beautiful.

~~~
glifchits
Looking at a random file in the Flow source [0]

    
    
          (* The entry point *)
          let rec go content =
            let env = { file = None; modified = []; line = 0; result = [] } in
            let lines = split_lines content in
            start env lines;
            match List.rev env.result with
            | [] -> []
            | _ :: results -> results
    
          (* Skip the text before the first +++ (to make things work with git show) *)
          and 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
    

Its beautiful to me because its very concise looking, while still using a fair
bit of real English, meaningful symbols, and idioms like [] makes an empty
array or whatever. I have to admit, much of this is lost on me, but it does
feel more accessible than other FP languages. It almost has a Python feel.

[0]
[https://github.com/facebook/flow/blob/master/hack/parsing/fo...](https://github.com/facebook/flow/blob/master/hack/parsing/format_diff.ml#L67-L83)

~~~
dchichkov
I'm not sure it is that concise. Just look at the code that you've given.
_What is it actually doing?_ How much of it is the juicy part and how much is
the language clutter? To me, it feels like in terms of the actual
functionality this code does not do that much. Skips the text before first
"+++". In more concise languages that whole piece would look like a single
line:

    
    
        env.lines = [line for line in env.lines if not line.startswith("+++")]
    

And just look at this:

    
    
        when String.length line > 4 && String.sub line 0 3 = "+++"
    

Really? Specifying numbers like that in the code? And are we really going to
alloc an object here? Wow. Wouldn't anyone prefer to use something like:

    
    
        line.startswith("+++") ?
    
    
    

_edit_ : my code doesn't implement the exact same functionality. I've only
attempted to give a feel/look of the code, not rewrite it. it looks like the
original code returns the lines after the '+++' lines, but I can not tell for
sure, as the header/modified function implementations are not given.

~~~
codygman
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.

~~~
sampo
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
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.

------
e12e
Having a quick look at ocaml again, I find I'm hoping someone will take the
time to write a "How I start"-article[1] for it, detailing things like
installing (something like wget raw.githug...opam_installer.sh &&
./opam_installer.sh bin - that gets you the latest opam locally), setting up
janestreet core and utop, and finally bootstrapping some kind of not-entirely-
trivial hello-world app and/or library for use with "opam install ...".

I did for instance find a couple of interesting (to me) libraries:
[https://github.com/esperco/ocaml-imap](https://github.com/esperco/ocaml-imap)
and [https://github.com/nojb/ocaml-maildir](https://github.com/nojb/ocaml-
maildir) . But only the first can i "opam install" \-- as a newcomer to ocaml
(or returnee of sorts) -- I my intuition tells me that being able to set up a
chroot/image with just stuff via opam would _probably_ be what I want.

But that's exactly why it'd be nice if someone that actually does this (and
uses ocaml in anger) wrote up a proper "How I start"-article.

[1] [https://howistart.org/](https://howistart.org/)

------
codygman
How does Ocaml do on having predictable memory usage? This is still a weak
spot in Haskell, though I absolutely love the language and it hasn't mattered
yet for my use cases.

Doesn't it hurt having a GIL?

How awesome is having the ML module system?

~~~
mads_hartmann
OCaml has an extremely predictable memory usage. Part III of Real World OCaml
covers this is great detail[1]

GC pauses haven't hurt us really. We usually just spawn a ton of processes so
if one process pauses a bit to GC we won't notice.

The module system is awesome. It's something i plan to cover next time I find
the time to extend the article.

[1]
[https://realworldocaml.org/v1/en/html/pt03.html](https://realworldocaml.org/v1/en/html/pt03.html)

------
carlob
This was written by an issuu engineer. Does anyone know if issuu uses OCaml?

~~~
mads_hartmann
Yeah, most of the services that I work on in my team at issuu are written in
OCaml :)

~~~
carlob
Great to know people using such an interesting language (and also being based
in Europe)!

------
vosper
I have been interested in OCaml for a while, and this is a nice piece. One
question, though - some of the examples start like this:

\- :

Is that the prompt from the OCaml REPL (does OCaml have a REPL)?

EDIT: I think I misread things - it's the output from the examples that has
the "\- :" prefix, so I'm guessing it is REPL output.

~~~
gnuvince
That's what the prompt answers back.

    
    
       # 3;;
       - : int = 3
       # fun x -> x*x;;
       - : int -> int = <fun>

------
rhizome31
Is there a tutorial out there showing off the OCaml tooling from a working
coder perspective? I mean setting up a project, managing dependencies,
debugging, testing, etc. I really think that average joe programmers like me
could benefit from features offered by strongly typed FPL, but lack of good
tooling is a show stopper. Elixir does an amazing job at this, but it's not as
safe and general purpose as OCaml or Haskell.

~~~
pjmlp
Something like this?
[https://realworldocaml.org/](https://realworldocaml.org/)

------
yodsanklai
I'm very surprised articles about OCaml (and more generally functional
programming) are popular on Hacker News. FP isn't exactly a new thing. I would
assume most CS graduates have experience with functional programming (I
learned "Caml Light" as an undergraduate in 1996).

~~~
agravier
As you learned Caml Light, chances are you went to a French university. As far
as I understand, outside of France, OCaml/Caml Light are seldom taught at
university. WRT FP, many universities will use Java or C++ to teach
programming fundamentals (a terrible thing imo). Some teach or used to teach
Scheme or Python.

~~~
drifkin
We did a little bit of OCaml in our undergrad PL class at UCLA. If I remember
correctly, it was only a couple weeks, and you're probably right in that it
was a bit unusual. The less exotic classes I took mostly used C++.

