6 ms·
OCaml at First Glance
- lou1306 4y agoI feel like F# addresses quite a few items on the "dislike" list: * It has // for single-line comments * You can use newlines as a list separator, as in: let some_list = [ 1 2 3 ] * Similarly, multiple let-bindings can be separated by newlines and the "in" is optional in most cases This comes from some drawbacks: for starters, when I mention "newlines", it's actually significant whitespace, so you may or may not like it (as a heavy Python user, I just don't care).
- devmunchies 4y agoYes, and if you use a code formatter, like Fantomas, it can automatically modify code to be in the more legible format (like Prettier.js), so you don't confuse tuples with list elements. I've been using f# for a couple years, and after the honeymoon phase is over, I've started to really value the benefit of native interop with dotnet libraries. Most enterprise SDKs/libraries (e.g. AWS S3, Stripe, Twilio, Rollbar, etc) have a dotnet SDK, and it just drops into an F# codebase as if it were a 1st party lib (you reference them using object-oriented dot notation instead of functional style, but still it just works). Even when Microsoft puts work into, for example, cryptography security updates or JSON parsing performance, F# benefits from that.
- yodsanklai 4y agoI'm surprised people care about the syntax... that's pretty much the least important part of the language. I've got a few issues with OCaml 1. The standard library is inconsistent. Different projects use different alternatives and it's annoying to switch between then. 2. Concurrency using Lwt/Async. Monads feel more ad-hoc in OCaml than Haskell because there's no type classes and again no consensus on how it should be done. Some projects use tons of variation of `>>=`, other use `let%bind`. Again, no clear consensus and reliance on external libraries which aren't always well documented. I find code relying too much on monads to be much less elegant and simple that plain OCaml. 3. "includes". I tend to get lost in codebase which abuse includes. 4. It's a small community. If you want to work on "real world" stuff, most likely you will stumble into tooling/libraries issues. Not all of these things are very mature or documented and they're evolving fast. I'm wondering if F# suffers from the same issues.
- dmitriid 4y ago> I'm surprised people care about the syntax... that's pretty much the least important part of the language. Until the syntax starts impeding with your understanding of the code, or is impossible to look up in documentation/google. OCaml IMO is a bit over the line with the number of ascii art used. I definitely agree with your other points. Though I only touched OCaml very briefly.
- momentoftop 4y ago> Some projects use tons of variation of `>>=`, other use `let%bind` It gets better. Since 4.08, the preference would be `let*`.
- hvs 4y agoF# has the entire .NET library available, so that won't be an issue.
- oaiey 4y agoI was surprised that the article mentions dozen of languages but no word of F# which is a mainstream clone/derivate of OCaml. Especially considering exactly that point.
- lf-non 4y agoI think that's understandable. Those dozens of the languages are the ones that the author has personally dabbled with in the past. The focus of article is their personal experience & first impressions. It was not intended to be an exploration of what else is available in the market and how ocaml compares to them.
- adelarsq 4y ago1. For F# you can can use both .NET and JavaScript libraries (with tool called Fable), so isn't a issue. 2. There a lot of ways to the that, which one with pros and cons, but isn't a issue in my opinion 3. For imports there are a thing called AutoOpen that is strange at first. Just need to be carefull where to use 4. .NET documentation tend to be writed for C#. So you need to understand at least a little of C# if you want to use that libraries. But also there are a lot that can be use idiomaticaly with F#, so isn't a big issue.
- dustingetz 4y agoHow is macros or metaprogramming or anything that fills a similar role?
- sleepydog 4y agoNot great, not terrible; the language supports annotations which mean nothing to the compiler but which pre-processors can take advantage of, and there is a framework called ppx which you can use to write your own preprocessor. There exist many pre-processors to do things like add inline tests, generate getter/setter/pretty-printing functions, and so on. Here is an example: https://github.com/ocaml-ppx/ppx_deriving https://github.com/ocaml-ppx/ppx_deriving
- toolslive 4y agothere's BER MetaOCaml https://okmij.org/ftp/ML/MetaOCaml.html https://okmij.org/ftp/ML/MetaOCaml.html
- acchow 4y agoStill can't figure out what the BER stands for
- JassicaB4 4y ago
- nerdponx 4y agoThis pretty much squares with my experience as well. I also want to shout out CS3110 as being an excellent (free!!) learning resource, and that having both a well-written textbook with exercises and short accompanying video lectures is a wonderful combination. I will never not be amazed at and grateful for high-quality university learning material like this being made freely available.
- acka 4y agoFor those who are interested in the CS3110 textbook and don't like Google sending them wristwatch shopping instead, here is a link to the textbook{1]. [1] https://cs3110.github.io/textbook/ https://cs3110.github.io/textbook/
- a-nikolaev 4y agoJust to add to this, I wrote additional lecture notes for the class on OCaml I taught, which was largely based on this book. http://a-nikolaev.github.io/fp/ http://a-nikolaev.github.io/fp/ - if anyone is interested in more learning resources.
- yallop 4y ago> Things I Disliked > [...] > • using ; heavily as element separator (e.g. in lists and records): > > let some_list = [1; 2; 3; 4; 5] > > You get used to this, but it’s a pretty big departure from the norm to use commas. I wonder what’s the reasoning behind this. It appears to be a result of the limitations of early parser technology, as Dave MacQueen explains: "One of the kind of interesting things is that the parser was written based on Vaughan Pratt's precedence parser [...] and it had the peculiar property that it was hard to reuse symbols for multiple syntactic purposes, and hence you get things like comma used for pairing that means you have to use semicolon for list separator, that means you have to use semicolon-semicolon for statement separator, so there's some peculiarities based on the fact that we didn't really know how to parse and we accepted this quick hack from Vaughan Pratt" (https://www.youtube.com/watch?v=ua3EYopCURo&t=179s)
- acjohnson55 4y agoMy biggest frustration with OCaml is having to deal with all of that cruft. I've learned many languages over the years, and I think I've just reached the point where I've lost patience with having to deal with what I would consider substandard syntactic elegance.
- toolslive 4y agowhat's your stance on the curly braces (which is also a consequence of early parser technology) ?
- acjohnson55 4y agoI think they're often helpful for reading, but I admire what Scala did in Scala 3 to eliminate their necessity.
- yawaramin 4y agoScala 3 actually doesn't really eliminate braces in any meaningful way. Sure, they're optional for delimiting traits, classes, and objects. But braces are heavily used for block scoping in Scala too. E.g. a very common pattern: retry(Seq(1.second, 2.second, 3.second)) { // some action that can fail } This still requires the braces as of right now. I think the syntax changes will just confuse more people than it helps, at least for the time being.
- gavinray 4y agoI really want to like OCaml, as a more practical Haskell, but the syntax to me is just weird. I think the ReasonML/ReScript guys had the right idea tbh, but I'm not interested in writing JS with it.
- donut2d 4y agoYou can actually compile ReasonML to native code with dune. What are some of the things that weird you out about OCaml's syntax?
- gavinray 4y ago> You can actually compile ReasonML to native code with dune. Yeah but the community doing native ReasonML is small and the maintenance has really petered off. With OCaml 5 looming you're better off just biting the bullet and writing OCaml if you're going to do it IMO. > What are some of the things that weird you out about OCaml's syntax? For whatever reason, I really dislike the named-argument syntax -- and some of the operators make it hard to read compared to named alternatives IMO. Below examples showcases some of these: let variables = (json_variables :> (string * Graphql_parser.const_value) list) ~resolve:(fun info () -> Lwt_result.ok (Lwt_unix.sleep duration >|= fun () -> duration) )
- yawaramin 4y agoOK but you can write code golf in any language, OCaml provides every opportunity to write readable code. The second snippet you provided is incomplete, if we were to write it in Python terms it would be something like: (resolve=lambda info: ...) But let's say for the sake of argument that it's an argument to a function `f`: f ~resolve:(fun info () -> ...) One of the simplest ways to improve readability is to use argument name punning: let resolve info () = let sleep = let open Lwt.Syntax in let+ () = Lwt_unix.sleep duration in duration in Lwt_result.ok sleep in f ~resolve
- WastingMyTime89 4y ago
- AzzieElbab 4y agoI had a very brief experience with OCaml debugging some seriously complicated networking code. Putting aside the quirkiness of the language, its compiler is a wonder of the world. It is practically as fast as GO which is a miracle given how much more sophisticated OCaml is.