Live data from Hacker News

What I wish I knew when learning OCaml (2018)

baturin.org

71–80 of 88 posts

Re: What I wish I knew when learning OCaml (2018)

#71

Could someone please explain ? : type 'a list = 'a :: 'a list | [] The article says "::" is a Data Constructor. I can make sense of type 'a = Left of 'a | Right of 'a where Right and Left are the Data constructors but I don't see the link with the part I don't understand.

The definition of list is:

  type 'a list = [] | (::) of 'a * 'a list
When you write a list like:

  [x; y; z]
This is syntactic sugar for:

  x::y::z::[]
Which is syntactic sugar for:

  (::) (x, (::) (y, (::) (z, [])))
One can imagine using more ordinary constructor names instead:

  type 'a list = Nil | Cons of 'a * 'a list
And then the above would be:

  Cons (x, Cons (y, Cons (z, Nil)))
In OCaml, data constructor names mar be either a capital letter followed by set or more capital/lower letters/underscores/apostrophes/digits, or one of the following:

  []
  ()
  true
  false
  (::)
Type directed constructor disambiguating means you can do funky things like:

  type 'a nonempty = (::) of 'a * 'a list
And write such a value just like you would a normal list.

Re: What I wish I knew when learning OCaml (2018)

#72
post #70
post #53

Earlier quoted context omitted.

I fully agree. F# is my favorite language, and it almost feels like a statically typed Scheme at times, just with built-in pattern matching and pipes thrown in. It's a really great way to program, and it's easy to go imperative or OOP when needed without friction. The only thing missing from F# is an Elixir/Erlang/OTP-like process system (`MailboxProcessor` is pretty good though), but then again, every language excep…

I am not further enough in my F# journey to have tried this, but you could technically pair it with orleans for actor oriented systems ? Love the language though I have spent only a week working with it. It seems to have a lot of things I liked in Kotlin.

F# does have the `MailboxProcessor`. It is quite simple and capable.

https://fsharp.github.io/fsharp-core-docs/reference/fsharp-c...

https://fsharpforfunandprofit.com/posts/concurrency-actor-mo...

I haven't looked at Orleans in a while. Last time I did, I came away with the viewpoint that it was very much designed to have a C# interface, which while manageable in F#, doesn't really provide for an idiomatic F# experience. I'm also not too sure, but I think it basically requires a cloud or distributed environment. Can it just be run on a local computer?

But even the `MailboxProcessor` doesn't do what Elixir and Erlang do on top of the BEAM VM. An instance of a BEAM VM is a single OS process, but the BEAM can handle millions of Erlang processes, which are not OS processes or even threads. They are their own thing that are extremely lightweight. The BEAM scheduler is very nice as well, such that it switches among all the running processes.

Even though I develop in Elixir, I am not yet a BEAM expert. So before I say something incorrect, I recommend taking a look at the talk The Soul of Erlang and Elixir by Sasa Juric.

https://www.youtube.com/watch?v=JvBT4XBdoUE

It fully encapsulates and describes what makes processes so special in Elixir and Erlang.

There is the Gleam language, which is a statically typed language on the BEAM VM. However, they moved away from the ML-dialect syntax, which is unfortunate.

Re: What I wish I knew when learning OCaml (2018)

#73
post #72
post #70

Earlier quoted context omitted.

I am not further enough in my F# journey to have tried this, but you could technically pair it with orleans for actor oriented systems ? Love the language though I have spent only a week working with it. It seems to have a lot of things I liked in Kotlin.

F# does have the `MailboxProcessor`. It is quite simple and capable. https://fsharp.github.io/fsharp-core-docs/reference/fsharp-c... https://fsharpforfunandprofit.com/posts/concurrency-actor-mo... I haven't looked at Orleans in a while. Last time I did, I came away with the viewpoint that it was very much designed to have a C# interface, which while manageable in F#, doesn't really provide for an idiomatic F# experie…

... and also Caramel which is a Ocaml/Reason dialect so you get to keep the Ocaml syntax

I have never tried it though

https://caramel.run/manual/introduction.html

Re: What I wish I knew when learning OCaml (2018)

#74

Could someone please explain ? : type 'a list = 'a :: 'a list | [] The article says "::" is a Data Constructor. I can make sense of type 'a = Left of 'a | Right of 'a where Right and Left are the Data constructors but I don't see the link with the part I don't understand.

It is equivalent to the following: type 'a list = Cons of 'a * 'a list | Tail ...but with a fancy syntax for Cons and Tail.

Ok. So in your example the construction action (placing two things next to each other IIUC) is done by the * product type Operator and Cons has no special meaning whereas if I had used ::, I actually get an the (::) name to refer to the left hand of the union and the construction action. Did I get that right ?

Re: What I wish I knew when learning OCaml (2018)

#75

Could someone please explain ? : type 'a list = 'a :: 'a list | [] The article says "::" is a Data Constructor. I can make sense of type 'a = Left of 'a | Right of 'a where Right and Left are the Data constructors but I don't see the link with the part I don't understand.

The definition of list is: type 'a list = [] | (::) of 'a * 'a list When you write a list like: [x; y; z] This is syntactic sugar for: x::y::z::[] Which is syntactic sugar for: (::) (x, (::) (y, (::) (z, []))) One can imagine using more ordinary constructor names instead: type 'a list = Nil | Cons of 'a * 'a list And then the above would be: Cons (x, Cons (y, Cons (z, Nil))) In OCaml, data constructor names mar be ei…

Thanks for the detailed explanation. One more question though, when I write

    type 'a = 'a :: list 'a
It is syntactic sugar that allows me to get the * product type for free, right ?

Re: What I wish I knew when learning OCaml (2018)

#76

Earlier quoted context omitted.

I like Rust for syntax, tooling, and ML ideas but I wish there was a "Rust-lite" that was garbage collected.

that’s pretty much F# :)

If F# had static, native compilation by default, a closer Cargo analog, and eschewed the OO stuff (probably mostly just there for C# interop?) and the OCaml syntax.

Re: What I wish I knew when learning OCaml (2018)

#77
post #66

Earlier quoted context omitted.

Ah yes, it might be possible that during the time of Caml light, the bytecode compiler was for a time considered to be fast enough. I am not sure that a ephemeral stance from 25 years ago on an ancestor language still really matters nowadays.

I'm not talking about Caml Light or even Caml Special Light, which is when the native-code compiler was initially added. 25 years ago was 01997. Objective Caml was released in 01996 (and renamed OCaml in 02011). Projects like KDE, the GIMP, GNOME, Lucene, Jython, LLVM, Asterisk, Audacity, CMake, and Danger that got started in C, C++, or Java in the 01997–02002 period are still based on those languages today. OCaml wo…

I have to ask: you're concerned about the year 9999 problem?

Re: What I wish I knew when learning OCaml (2018)

#78

Earlier quoted context omitted.

The definition of list is: type 'a list = [] | (::) of 'a * 'a list When you write a list like: [x; y; z] This is syntactic sugar for: x::y::z::[] Which is syntactic sugar for: (::) (x, (::) (y, (::) (z, []))) One can imagine using more ordinary constructor names instead: type 'a list = Nil | Cons of 'a * 'a list And then the above would be: Cons (x, Cons (y, Cons (z, Nil))) In OCaml, data constructor names mar be ei…

Thanks for the detailed explanation. One more question though, when I write type 'a = 'a :: list 'a It is syntactic sugar that allows me to get the * product type for free, right ?

I think that is made up syntax from the article but maybe I’m wrong.

Re: What I wish I knew when learning OCaml (2018)

#79
post #52

Earlier quoted context omitted.

Why do you think that the native compiler was treated as a second class citizen? This is quite strange take from my point of view. For instance OCaml native compiler was available on the M1 Macs two months after the M1 release. Similarly, all major CPU architectures (x86, ARM, PowerPC, RISC-V, s390x) have been supported for years.

20 and 25 years ago, the attitude (as I remember it) was that the native-code compiler was not very important because the bytecode interpreter was fast enough for most uses. It's true that that is no longer the attitude. But if we want to understand why one language is more popular than another, we usually need to look at things that happened in the past, not just things that are happening right now. Even C's meteori…

> ...if we want to understand why one language is more popular than another, we usually need to look at things that happened in the past, not just things that are happening right now

Figuring out why OCaml didn't get popular in the past is not a super useful exercise. It's more useful to understand what is needed to popularize it today.

Re: What I wish I knew when learning OCaml (2018)

#80
post #40

ML really is a beautiful and under-appreciated family of languages. Haskell is a bit too supernatural for my taste, but ML and its derivatives hit the sweet spot between powerful type systems and natural syntax. As a Kotlin developer, I find reading OCaml code a breeze and the compiler is refreshingly snappy (much faster than Gradle). I just wish it had better developer tools.

I like Rust for syntax, tooling, and ML ideas but I wish there was a "Rust-lite" that was garbage collected.

how do you feel about nim?
Post reply on HN