Category: OCaml

  • Functional Style Selection Sort in OCaml

    In this post I talk about what I have tried and learnt in the process of writing the functional correspondence of the imperative selection sort I wrote earlier in OCaml.  I learnt a lot in this practice because functional selection sort is not straight forward! The First Attempt At the beginning, I thought functional selection…

  • Imperative Style Selection Sort in OCaml

    Exercise 2.2-2 of the book Introduction to Algorithms introduced the selection sort algorithm.  I decided to write it in OCaml in imperative style. The selection sort algorithm sorts a list first by switching places of the smallest element of a given list with the first element of the list.  Then, it selects the smallest element…

  • Imperative and Functional Insertion Sort in OCaml

    I wrote the insertion algorithm in both imperative and functional styles in OCaml!  I talk about the fun and challenges of each style below: Imperative Insertion Sort I strictly follow the pseudocode on p.18 of the book Introduction to Algorithms.  The pseudocode is in imperative style so all I need to do is to translate…

  • Imperative Programming in OCaml

    Imperative Vs Functional Programming So far, what I’ve done in OCaml is functional, i.e., I declare functions and the output value of a function depends only on the arguments that are passed to the function.  The data structures covered are mostly immutable (not able to change).  In contrast, with imperative programming, computations are structured as…

  • Data Structures in OCaml

    In this post I continue going through Chapter 1 of Real World OCaml.  I learnt that: Errors may be caught at compile time or at run time.  Compile-time errors are preferred to runtime errors, because it’s better to catch errors as early as possible in the development process.  An e.g. of a compile-time error is…

  • Moving on to Real World OCaml

    After finishing the first 4 chapters of OCaml from the very beginning, I feel I am ready to move on to other learning resources that assume more programming background.  After all, I can write the Fizzbuzz test now, right?  Real World OCaml and the OCaml tutorials also have the advantage of being more up to…

  • Learning About Lists in OCaml

    In Chapter 4 of OCaml from the very beginning, I learned about lists: A list is a collection of elements of the same type.  The type of the list is (the type of the element(s)) list E.g., a list of integers has the type int list. To define a list, put the elements in a…

  • The Fizz Buzz Test in OCaml

    I just found out about the Fizz Buzz Test yesterday, and I was so excited about solving it with the minimal OCaml knowledge I have!  So I did it!  You can find it on my github. While writing this program, I learned that semicolons are helpful markers!  You are to separate each expressions with a…

  • A More Powerful Exponential Function

    In this post I describe how I solved the following question (from Chapter 3 of OCaml from the very beginning): Use pattern matching to write a function which, given two numbers x and n, computes x to the power of n. Although the author provided an answer, his function returns a stack overflow error when…

  • Learning Pattern Matching in OCaml

    In Chapter 3 of OCaml from the very beginning, I learned about pattern matching.  It can replace the ‘if … then … else …’ construct, and can be easier to read.  It also has the advantage that if you haven’t considered all cases in the pattern matching, OCaml will warn you so.  Just for this…