Live data from Hacker News

A brief introduction to OCaml

lexicallyscoped.com

31–39 of 39 posts

Re: A brief introduction to OCaml

#31

Earlier quoted context omitted.

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.

> The idea is you can write this:

>

> let _ = print_string "foo"

> let _ = print_string "bar"

>

> at the top level.

Ah. Well, the solution is not use "let _" or ";;", the solution is to avoid sociopathic code like that. Module definition with built-in, unavoidable side effects are evil.

Re: A brief introduction to OCaml

#32
post #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 w…

> the integrated F# command line facilitates pretty much similar development as with lisps

OCaml has Utop, and that's a pleasure to work with.

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

You can use Merlin to do the same thing in vim or emacs. This doesn't grant you the rest of IDE features, but you still get typechecking, you can check the type of a variable, and you get intelligent renaming (within the same file).

Re: A brief introduction to OCaml

#33
post #5

im quite amazed that ocaml seems to be on a rise (judging from how often i've recently seen it on here). it reminds me of the time i started to dig into it more than a decade ago when mldonkey [0] was a thing. also, this is the way to briefly introduce a programmer to a new language. just show the commented syntax. i just hate reading all the prefaces and texts of language manuals. it's good they're there but i'm jus…

You might want to give this a look: https://realworldocaml.org/v1/en/html/a-guided-tour.html

Re: A brief introduction to OCaml

#34
post #30

Earlier quoted context omitted.

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?

C interop is quite easy from .NET languages. Here's a simple example in F#:

https://msdn.microsoft.com/en-us/library/dd393785.aspx

That example is a bit Windows specific, but it works similarly with Mono on Linux and OS X:

http://www.mono-project.com/docs/advanced/pinvoke/

Those examples there are in C#, but PInvoke works almost identically in F#.

Re: A brief introduction to OCaml

#35

Earlier quoted context omitted.

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.

> The idea is you can write this: > > let _ = print_string "foo" > let _ = print_string "bar" > > at the top level. Ah. Well, the solution is not use "let _" or ";;", the solution is to avoid sociopathic code like that. Module definition with built-in, unavoidable side effects are evil.

For serious development, yes, but consider the people who are learning OCaml. One of the first programs is going to be some kind of side effecting hello world etc.

Re: A brief introduction to OCaml

#36

Earlier quoted context omitted.

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.

> The idea is you can write this: > > let _ = print_string "foo" > let _ = print_string "bar" > > at the top level. Ah. Well, the solution is not use "let _" or ";;", the solution is to avoid sociopathic code like that. Module definition with built-in, unavoidable side effects are evil.

You need at least one module that does something at top-level, otherwise your program won't do anything.

Re: A brief introduction to OCaml

#37
post #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…

The code examples doesn't use Jane Streets Core as the primary standard library. If you do, for example by adding `open Core.Std` to your .ocamlinit, you'll get the behaviour you're describing.

It is probably a good idea to use Core, but for this introduction I wanted as little requirements as possible.

edit: spelling

Re: A brief introduction to OCaml

#38
post #37
post #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…

The code examples doesn't use Jane Streets Core as the primary standard library. If you do, for example by adding `open Core.Std` to your .ocamlinit, you'll get the behaviour you're describing. It is probably a good idea to use Core, but for this introduction I wanted as little requirements as possible. edit: spelling

Right. Since you mention the installation instructions from Real World OCaml and the examples in the book need you to use Core, it is likely that beginners would experience these issues.

Re: A brief introduction to OCaml

#39
post #38
post #37

Earlier quoted context omitted.

The code examples doesn't use Jane Streets Core as the primary standard library. If you do, for example by adding `open Core.Std` to your .ocamlinit, you'll get the behaviour you're describing. It is probably a good idea to use Core, but for this introduction I wanted as little requirements as possible. edit: spelling

Right. Since you mention the installation instructions from Real World OCaml and the examples in the book need you to use Core, it is likely that beginners would experience these issues.

That's a good point. I'll add a note to explicitly say that the examples doesn't use Core.
Post reply on HN