Posts tagged “ocaml”
Last update: 2020-08-28.
One thing I find odd about many interpreter projects is that they are designed as standalone and can't be used as embedded scripting languages. Lua-ML is completely different in that regard: it's designed as an extension language first and offers some unique features for that use case, including a reconfigurable runtime library. You can remove modules from its standard library or replace them with your own implementations. Of course, you can also pass OCaml values to Lua code and take them back in a type-safe manner too. That aspect isn't very obvious or well-documented, so in this post we'll try to uncover it.Read more
Last update: 2019-09-05.
In this post we'll examine what ReasonML really is and what it compiles to. Everyone coming from the ML community already knows the truth, but in the JS community, this misconception seems surprisingly common. It's not just about giving credit, but about the true potential of the language that is far greater than that of TypeScript or Elm.Read more
Last update: 2019-08-24.
Many parsers for custom formats aren't very user friendly when it comes to error handling. At best you get the line and column where the error is, at worst a “Parse error” message is all you get. Can we do better? With right tools, we definitely can and it's not that hard.Read more
Last update: 2019-08-10.
Most of the time, when people make extensible programs in typed functional languages,
they make a DSL, not least because it's much easier to make a DSL in a language with algebraic types
and pattern matching than in one without.
Some use cases really require a general-purpose language though. That's where things get more interesting. Commonly used embeddable interpreters such as Lua, Guile, or Chicken are written in C. It's possible to make OCaml or Haskell bindings for them and such bindings do exist, but that's two high level languages communicating through a low level one.
It would be much better to be able to expose native types to the embedded language in a type-safe and more or less convenient fashion. Here's my take at it.
Last update: 2018-08-18.
In OCaml, exceptions are not objects, and there are no exception hierarchies. It may look unusual now, but in fact exceptions predate the rise of object oriented languages and it's more in line with original implementations. The advantage is that they are very lightweight.Read more
Introduction to OCaml, part 4: higher order functions, parametric polymorphism and algebraic data types
Last update: 2018-08-12.
So far we have only worked with functions that take value of a single type known beforehand.
However, we have already seen functions whose types were left without explanation, such as
let hello _ = print_endline "hello world" that we used to demonstrate the wildcard pattern.
What is its type? If you enter it into the REPL, you will see that it's
'a -> unit.
What does the mysterious 'a mean? Simply put, it's a placeholder for any type.
Essentially, a variable for types —a type variable.
Last update: 2018-08-08.
This should have been covered earlier, but better late than even later. In this chapter we'll learn about booleans and conditional expressions.Read more
Last update: 2018-08-07.
In the previous chapter we've learnt how to use variables and arithmetic functions. Now it's time to learn how to make our own functions.Read more
Last update: 2018-08-06.
This post series started as a response to requests from some friends curious about OCaml. There are quite a few nice books already, but I realized that if I just recommend them any one of those books, it still will leave me with quite a few things to explain in depth, or force me to recommend another just to learn about that part. So I thought I may as well write something that hopefully will allow a person who already knows how to program in some other language get started with writing OCaml programs and continue learning on their own and find their own sources.Read more
Last update: 2018-04-12.
Implementation of sets using nothing but functions would be one of the first tricks in the “100 Fun Things to Do With Functions and Closures” book if that book existed. It may not be very practical, but it may help people get into the functional mindset. We'll use OCaml for demonstration.Read more
Last update: 2018-02-21.
So called “duck typing” is often poorly explained and thus often misunderstood. Its name and the adage associated with it (“if it walks like a duck and quacks like a duck, then it is a duck”) don't do it any favors either.Read more