OCaml – from the very beginning

I’m going to deep dive the language of OCaml and become an expert of this language.  Why OCaml?  Because I want to write programs that make no mistakes, and OCaml seems to help with that.  As explained in this website

OCaml is a general purpose programming language with an emphasis on expressiveness and safety……it (OCaml) has an advanced type system that helps catch your mistakes without getting in your way. It’s used in environments where a single mistake can cost millions and speed matters,’

Chapter 1 of ‘OCaml from the very beginning’

I have limited programming background.  I learned Pascal years ago.  I picked up LaTeX and Octave while I was doing economics research, but I never got formal training.  I decided to go through the ‘OCaml from the very beginning’ book chapter by chapter, because it is suitable for readers without much programming background.

I tried the first few chapters, and absolute love it!  The author is so concise!  In chapter 1, I learned a few basics:

  • To tell OCaml we are done, we type two semicolons and enter.
  • After we type in our expression (our OCaml code), OCaml evaluates the expression, and gives us a value (the answer), along with its type.
  • int stands for the type integer.
  • bool stands for the type boolean, it only has two values:
    • true
    • false
  • char stands for the type character, its value is any single character.  We write them in between single quote marks.  E.g. ‘?’ or ‘x’.
  • operators can be
    • mathematical ones like plus(+), minus(), multiple(*), divide (/), mod
    • constructs like ifthenelse
    • logical one like and(&&), or (||) for operands of type bool, gives values of type bool.
    • Comparison operators compare a and b.  Like a <= b (a is less than or equal to b), a >= b, a <> b (a is not equal to b).  They give values of type bool.
  • operands are the things the operators are applied to; in this case, integers.
  • We put a space between operators and operands for readability.
  • OCaml applies the operators with higher precedence before others.
  • You can only apply operators to operands of the appropriate types.  E.g., + can be applied to integers, but not boolean values.
  • operands of type bool has two operators:
    • && stands for ‘and’
    • || stands for ‘or’
    • && has higher precedence than ||
  • The number line wraps aroundmax_int, which represents the maximum integer, equals min_int – 1 and max_int + 1 = min_int.
  • The lowercase char are in alphabetical order, for example ‘p’ < ‘q’ evaluates to true . The uppercase characters are similarly ordered. The uppercase letters are all “smaller” than the lowercase characters. For type bool, false is considered “less than” true .
  • mod should be handled very carefully.  Looking into it further I found that mod behaves differently in different programming languages.  See the wikipedia page on modulo operation.

I find Keith Waclena‘s course material on built-in data types and operators and functions helpful.







Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.