8 ms·
Functional Programming in OCaml
- nikivi 7y agoHN thread on 2019 edition: https://news.ycombinator.com/item?id=19292067 https://news.ycombinator.com/item?id=19292067
- melling 7y agoReasonML is Facebook’s OCaml for JavaScript. https://reasonml.github.io/ https://reasonml.github.io/ They have used it extensively: https://reasonml.github.io/blog/2017/09/08/messenger-50-reason.html https://reasonml.github.io/blog/2017/09/08/messenger-50-reas... With impressive results: “ Messenger used to receive bugs reports on a daily basis; since the introduction of Reason, there have been a total of 10 bugs (that's during the whole year, not per week)! *”
- emmanueloga_ 7y agoIf I get it right (from posts I've seen before and the docs on the project [1]) Reason is not really "OCaml for JavaScript" but just a parser for a more appealing, mostly compatible, syntax for the OCaml. So one should be able to compile to the same targets as with traditional OCaml. Re: Mostly compatible: I remember there were certain things that were not easily expressible with Reason (but available in the OCaml syntax) a few years ago, not sure if it is still the case. Subjective: OCaml syntax is definitely on the weird side, although I would not call it ugly. This coming from someone who enjoys writing clojure and xslt, go figure :-p. 1: https://github.com/facebook/reason/blob/master/src/README.md#repo-walkthrough https://github.com/facebook/reason/blob/master/src/README.md...
- desert_boi 7y agoBuckleScript (https://bucklescript.github.io/ https://bucklescript.github.io/) is used for using OCaml to output JavaScript. Reason can target native just like vanilla OCaml syntax can.
- emmanueloga_ 7y agoOh, I was browsing BuckleScript's history and for a minute thought evmar [1] was involved (author of the ninja build system) [2]. But I think evmar's commits are just ninja commits. First BS commit: [3]. Almost looks as if the author of BuckleScript maybe forked the ninja repo and started to work in BuckleScript from there. Why would anybody do that? Such a mystery... 1: https://github.com/BuckleScript/bucklescript/graphs/contributors?from=2010-10-10&to=2011-03-02&type=c https://github.com/BuckleScript/bucklescript/graphs/contribu... 2: https://github.com/ninja-build/ninja https://github.com/ninja-build/ninja 3: https://github.com/BuckleScript/bucklescript/commit/a626ee8bd3c77b05a38e2ebb2f82f49738e404e2 https://github.com/BuckleScript/bucklescript/commit/a626ee8b...
- adamcanady 7y agoJs_of_ocaml [1] is a good alternative to BuckleScript for standard OCaml (non-Reason). For example, it works well with Core_kernel [2] and Async_kernel [3] to provide high-level functionality that is cross compatible between Unix applications and browser applications. [1] https://github.com/ocsigen/js_of_ocaml https://github.com/ocsigen/js_of_ocaml [2] https://github.com/janestreet/core_kernel https://github.com/janestreet/core_kernel [3] https://github.com/janestreet/async_kernel https://github.com/janestreet/async_kernel
- ernst_klim 7y agoJs_of_ocaml works fine with Reason, though.
- abathologist 7y agoTo be clear, Bucklescript also works with standard OCaml (because Reason is just an alternative surface syntax).
- agumonkey 7y agoyet ml use in the industry is thin, alas quality goes with patience
- js4ever 7y agoIs that with the same team or with a new one? Maybe the reason is the new team and full rewriting of the initial version and not the new language...
- thelazydogsback 7y agoI don't know why they felt they had to mess with OCaml's syntax - one of the nicest around AFAIC. With F#, even MS didn't feel like they had to add extra braces and other baggage and change keywords ("match" to "switch ()", etc.) to make C# programmers have an easier first ten minutes of transition time.
- emmanueloga_ 7y agoAre you referring to Reason? I don't see anything related to messing OCaml syntax in the OP.
- thelazydogsback 7y ago"Reason, also known as ReasonML, is a syntax extension and toolchain for OCaml created by Jordan Walke at Facebook.[2] Reason offers a syntax familiar to JavaScript programmers, and transpiles to OCaml.[3]" https://en.wikipedia.org/wiki/Reason_(syntax_extension_for_OCaml) https://en.wikipedia.org/wiki/Reason_(syntax_extension_for_O...
- emmanueloga_ 7y agogot it, I was just confused since usually top level comments refer to the original post, which in this case is a Textbook for OCaml that doesn't mention Reason anywhere afaict
- thelazydogsback 7y agoSorry, my bad - I meant to reply to @melling, not the top-level
- nv-vn 7y agoThere's a few legitimate issues with OCaml syntax that have been fixed by Reason at least. E.g. `match`/`if` not having an `end` means you need to use parens or `begin`/`end` to nest match and have ; inside of an `if` body. There's also some ambiguity between functional `if` and imperative `if` in some situations.
- amatecha 7y agoAs I don't have a lot of time to investigate at this moment - is this intended to be a generally-standalone guide that one could use to learn OCaml? I'm guessing not, since it doesn't seem to dive in too deep into any subject, just giving a very broad overview.
- jdeisenberg 7y agoIf you want something more in-depth about OCaml itself, http://dev.realworldocaml.org/ http://dev.realworldocaml.org/ is a good resource.
- pjmlp 7y agoAny news on how the 2nd edition is going on?
- acjohnson55 7y agoIt's totally usable and preferable in its beta format to the first edition. The first edition is pretty out of date with current practice.
- pjmlp 7y agoThanks!
- xvilka 7y agoLearn OCaml in Y minutes: https://learnxinyminutes.com/docs/ocaml/ https://learnxinyminutes.com/docs/ocaml/
- kernoble 7y agoAn great course and a very good professor. I'm glad they've compiled the course into an ebook. It's the only way I can think of FP, so I'm glad I can know point people to the same source I learned it from. It would be quite awesome if they made it fully interactive; in the vein of several other textbooks that have been shared on HN recently.
- danb232 7y agowhich textbooks are interactive like that?
- afiori 7y agoTwo that I know are SICP[0] and Eloquent Javascript[1] [0] https://sicp.comp.nus.edu.sg/ https://sicp.comp.nus.edu.sg/ [1] https://eloquentjavascript.net/ https://eloquentjavascript.net/
- throwaway17_17 7y agoI am working on two projects in one that may be similar to what you’re thinking. I really like the way the University of Waterloo’s CS Python tutorial [1] is done, so I’m trying to get a little bit of comfort with web development, and I decided I wasn’t happy just being familiar with Haskell and wanted to get into ML... So I’m slowly going through the 2019 version of this book and inserting dummy formatted JS input blocks with the intention of hooking them up to an online interpreter. I doubt I’ll get all the way through the text, but if I want messing with fancy formatting and trying to learn the web design aspects (and therefore messing around with CSS waaaay too much) it doesn’t seem like an insurmountable task. —- Also, the responsive design of the book works pretty well on iOS, and that has been a rarity in this area from my experience. [1] https://cscircles.cemc.uwaterloo.ca/ https://cscircles.cemc.uwaterloo.ca/
- a-nikolaev 7y agoVery nice practical and modern textbook for OCaml. I were using it last year to teach this subject at my school. Here are my additional notes and exercises, if you are interested: https://a-nikolaev.github.io/fp/ https://a-nikolaev.github.io/fp/
- willtim 7y ago> Java didn't have generics until version 5 in 2004; the ML family had it in 1990. I thought Standard ML had it in the 70's (!)
- noelwelsh 7y agoYes, 1975 IIRC. Standard ML was published in 1990, so that is probably the source of the confusion. It's a bit sad that the foundations of modern programming languages were basically known by 1975 (either via Standard ML or Scheme) but the majority of languages in common use (mostly of a 1990s vintage) didn't learn those lessons. Thankfully that seems to be changing with more recent languages (Go being the exception).
- pjmlp 7y agoAnd CLU. Having researched a bit in the 60's and 70's systems programming languages, I would assert that the same issues with Go apply to C (language design vs existing competition), and they only got lucky because Bell Labs was forbidden to profit from UNIX.
- devmunchies 7y agoI haven't used OCaml in a couple of years. has the stdlib story been improved/standardized? is there still the whole async vs jwt problem or did jwt finally win out? I think i remember that there wasn't a good web framework, and that the existing popular one had a memory leak. I was learning from a book (Real World OCaml, i think) and it kept pushing jane street libs like Async and Core.
- LeonidasXIV 7y agoMost of the issues go away if you use Async and Core (or Base these days) since it is a very good and rather complete standard library and a thoughful Async implementation. The issue with the web framework still stands. Not many people do web development in OCaml so the support is rather sparse.
- abhijat 7y agoDo you mean LWT?
- devmunchies 7y agoAh. Yes.
- ode 7y agoThere may not be that many people doing web development in OCaml directly (Reason ML may be changing this) but it is do-able. Read 'A Lightweight OCaml Webapp Tutorial' - https://shonfeder.gitlab.io/ocaml_webapp/ https://shonfeder.gitlab.io/ocaml_webapp/ for a up to date example of an OCaml web stack. Reason uses the OCaml to Javascript compiler Bucklescript to make OCaml more accessible for web programming. Listen to the podcast 'Reason Town' - https://player.fm/series/reason-town https://player.fm/series/reason-town to hear professionals talking about their experience with it.
- ldeangelis 7y agoYaron Minsky is one of the author of Real World OCaml, and is the one that brought OCaml to Jane Street so that may be why there's pushing these libs.
- crimsonalucard 7y agoHow does OCaml compare with haskell?
- wyager 7y agoOcaml does not make any effort to control impure code or mutability. The type system is not as powerful, but the powerful module system takes up some of the slack. Lots of things that are really convenient in Haskell (like point-free function composition) work poorly or not at all in OCaml due to constraints imposed by the existence of impure mutable references. The OCaml runtime is pretty primitive compared to Haskell’s, especially around stuff like concurrency. GHC has a world-class concurrency story and OCaml barely has support for multiple OS-level threads. OCaml uses tagged pointers for boxing, which means you have weird stuff like integers being 63 bits instead of 64. Haskell uses bitmasks on heap objects to avoid that. Subjectively, I think OCaml is a huge improvement over, say, Java, but if you’re deciding between learning one of OCaml or Haskell in 2020 I think Haskell is the better option for most people.
- smabie 7y agoI don't consider OCaml and Haskell to be really comparable. I love statically typed FP and have professional experience with OCaml, Haskell, and Scala and Haskell is significantly different than the other two (also much different than F#). I've personally never found Haskell's encoding of mutability or impurity into the type system to be of any real benefit. Most of the time your impurity is coming from logging, networking, or interacting with files or a db; in which case, it's entirely obvious what's going on. Other times, you have a pure function that's implementation is unpure, which is also obvious on what's going on. In any case, if you're programming in good FP-style all Haskell is providing is a bunch of headaches around finding the write way to make all your type annotations match up: it's kind of a annoying experience when you're just trying to get something done. I think Haskell's a good way to start learning functional programming because it really beats the habit of loops and mutability out of you real fast by making the functional way easier. But if you're already an experienced FP programmer, less restrictive languages are better. What makes Haskell interesting is all the crazy optimizations it can do since the compiler has so much information about the program. It would be really cool if other languages had a "pure" annotation or keyword that signified the function has a pure interface. This could dramatically improve the performance of a lot of Scala/F#/OCaml code.
- wing328hk 7y agoIf you want to access RESTful APIs via OCaml, you may consider using OpenAPI Generator to generate the OCaml client automatically instead of manually creating one. Here are 3 simple steps to do so: 1. Download the OpenAPI Generator CLI Java JAR (https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/4.2.3/openapi-generator-cli-4.2.3.jar https://repo1.maven.org/maven2/org/openapitools/openapi-gene...) 2. Rename the JAR as "openapi-generator-cli.jar" 3. Run the following command to generate an OCaml API client for the Petstore API (https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml https://raw.githubusercontent.com/OpenAPITools/openapi-gener...): Mac/Linux: $ java -jar openapi-generator-cli.jar generate -g ocaml -i https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml https://raw.githubusercontent.com/OpenAPITools/openapi-gener... -o /var/tmp/ocaml/ Windows: $ java -jar openapi-generator-cli.jar generate -g ocaml -i https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml https://raw.githubusercontent.com/OpenAPITools/openapi-gener... -o C:\tmp\ocaml If you've any feedback or question, please let us know via https://github.com/OpenAPITools/openapi-generator/issues/new https://github.com/OpenAPITools/openapi-generator/issues/new