Live data from Hacker News

A brief introduction to OCaml

lexicallyscoped.com

21–30 of 39 posts

Re: A brief introduction to OCaml

#21

This is also great cheat sheet for someone still getting used to the OCaml syntax. I usually advise people to pretend that double semicolons don't exist for anything but the interactive top level. In source files, you can forget about double semis entirely as long as you just remember to always assign the result of an imperative statement to the dummy "_" name. let _ = print_string "hi" Then you can think of ;; as me…

I never use double semi colons for anything, but you'd use a simple semi colon after that statement:

    let msg = "hi" in
    print_string msg;
What's better about using "let _"?

Re: A brief introduction to OCaml

#22

How does OCaml compare to F# from a language point of view?

It's easy to get a quick comparison by searching stackoverflow so I won't reitterate those answers, but I would just mention a couple of my favorite things from OCaml that F# doesn't have that those answers tend to leave out: - Polymorphic variants - GADTs - Named arguments (that are still curried if you can believe it!) - Last time I checked OCaml had better record label scoping. F# doesn't allow two record types in…

Also, OCaml has functors.

On the other hand, interop with C# means instantly much larger ecosystem.

Re: A brief introduction to OCaml

#23
post #7

How does concurrency in OCaml compare to Go, Clojure, Rust, etc? Thanks

Concurrency is not built-in, you can get monadic concurrency from libraries (either lwt or Async). There is no parallelism support yet, but multicore support should be there by the end of the year.

Re: A brief introduction to OCaml

#24

How does OCaml compare to F# from a language point of view?

I would not actually compare them from language point of view but ecosystem point of view - but then again, I like simplistic stuff. Give me algebraic datatypes, pattern matching and type inference and I'm happy.

That said, F# in my opinion benefits hugely from the Visual Studio tooling (if one can use it). The main benefits for me are:

- the integrated F# command line facilitates pretty much similar development as with lisps - one can instantiate a context, and then develop ones program code at the same time as the context is live. With static types. I.e. create a function, interpret in repl, run function, observe the side-effects etc. Once done compile. Then load dll in repl, build new functionality on top of previous module etc etc.

- Visual Studio intellisense debugs my logic as I type it. Having the IDE proofreading my sources as I type it is a huge productivity win. Of course, if the program logic is built around types, that is - but with F# this is the easiest way to write stuff any way.

- This is a fringe issue but one can write Unity scripts in F# :)

Re: A brief introduction to OCaml

#25

This is also great cheat sheet for someone still getting used to the OCaml syntax. I usually advise people to pretend that double semicolons don't exist for anything but the interactive top level. In source files, you can forget about double semis entirely as long as you just remember to always assign the result of an imperative statement to the dummy "_" name. let _ = print_string "hi" Then you can think of ;; as me…

I never use double semi colons for anything, but you'd use a simple semi colon after that statement: let msg = "hi" in print_string msg; What's better about using "let _"?

The idea is you can write this:

    let _ = print_string "foo"
    let _ = print_string "bar"
at the top level.

Of course you can write

    let msg = "foo" in print_string msg
    let msg = "bar" in print_string msg
(which isn't at issue, except no semicolons are needed.)

But you can't write

    print_string "foo"
    print_string "bar"
without semicolons.

Re: A brief introduction to OCaml

#26
It seems `int_of_float` and `float_of_int` are deprecated by the Jane Streets Core library. It even throws errors on utop. I believe they need to be replaced by `Int.of_float` and `Float.of_int` respectively.

EDIT: Some more corrections:

1. `List.hd []` doesn't raise an error as its return type is an option. So the result is a `None`.

2. `List.map` and `List.filter` arguments should be reversed. First argument should be a list and the second should be the mapping function.

Re: A brief introduction to OCaml

#27

This is also great cheat sheet for someone still getting used to the OCaml syntax. I usually advise people to pretend that double semicolons don't exist for anything but the interactive top level. In source files, you can forget about double semis entirely as long as you just remember to always assign the result of an imperative statement to the dummy "_" name. let _ = print_string "hi" Then you can think of ;; as me…

I never use double semi colons for anything, but you'd use a simple semi colon after that statement: let msg = "hi" in print_string msg; What's better about using "let _"?

Single semicolons simply evaluates several expressions and returns the last one, but if you use them in a module definition (like every file is by default), you'll be coerced into using double semis.

The advantage over your version (with the single semicolon) is that in your version, you cannot now create a standard exported module value binding after that single semicolon, because the parser interprets that next let binding as a continuation of the single semicolon expression, so your only hope is to use double semis to escape back to normal module body parser context.

Function bodies (or any other expression really) don't have that same issue, but I might suggest avoiding single semicolons even in that case so that you have fewer edge cases to memorize. Here is the simplest possible convention I can think of that requires the smallest amount of memorization:

- Forget about all semicolons, single or double (just think of ;; as a way to press enter in the top interactive REPL).

- Module bodies (like every file by default) are just exporting a series of bindings. If you want to evaluate an imperative command inside a module body, export its result to "_".

- Function bodies are expressions. If you want to evaluate an imperative command inside a function body (or any other expression), use `let in` like you would for any other temporary variable, but use the variable name "_" and simply don't use it.

Re: A brief introduction to OCaml

#28
post #15

This is also great cheat sheet for someone still getting used to the OCaml syntax. I usually advise people to pretend that double semicolons don't exist for anything but the interactive top level. In source files, you can forget about double semis entirely as long as you just remember to always assign the result of an imperative statement to the dummy "_" name. let _ = print_string "hi" Then you can think of ;; as me…

This is one thing I'm on the fence about F# doing "better", double semis are replaced by requiring you do something with your result, either assigning the result of piping it to the "ignore" builtin.

In some way, OCaml can be thought of doing this as well: https://xivilization.net/~marek/blog/2014/12/28/reinventing-... You can consider the ; operator as a binary operator evaluating the left side, throwing away the result and then avaluating the right side and returning that result.

Re: A brief introduction to OCaml

#29
I do really like OCaml, although Rust is the one I'm starting to look to where in the past I'd consider OCaml (yes, two different languages, but similar in practical function approach and safety).

The one things that I absolutely can't stand about OCaml is the lack of an easy, available, include-in-the-standard-library, debugging function. Every damn time I work with OCaml, I end up having to write annoyingly time-consuming "print_blah_blah" functions to handle debugging. And such a debugging feature should handle errors already, and not return the Option type. I shouldn't have to write a function to handle errors on a debugging statement.

And yes, I'm sure there are some nice versions of this already available in 3rd-party libraries, but this should be in the standard library.

Re: A brief introduction to OCaml

#30

It's cool to see a link about learning OCaml so high on HN. I started teaching myself OCaml a few months ago and I really like it. Here's a few things I like so far and why I think anyone that uses Go or Rust should give OCaml a look: 1. OCaml compiles to a binary like Go and Rust do 2. The type system is helpful without being a huge burden on learning/productivity (unlike Rust and Haskell) 3. Garbage collection 4. P…

My power combo is F# and Rust. F# is fairly fast, has great tooling and rather expressive. And for the parts I need the speed, Rust fits in nicely. I can keep safety, while maintaining decent expressiveness. Something like Rust is needed, because when you need manual memory control, you really need it. With F#, I could measure the impact of every single allocation, even though short lived GC items are cheap.

Does F# have easy C interop, so you can call into Rust easily like you can from say, Python?
Post reply on HN