
Lakshen Perera provides a list of basic patterns for everyday programming, illustrated in Javascript and Ruby. I thought it would be interesting to provide an OCaml illustration as well, and perhaps a handful of additional patterns as well.
Verify object’s availability before calling its methods or properties
In many languages, there is a possibility for objects to be missing — whether this is represented as NULL, null, nil or None. Regardless of the language, it is important to keep in mind at all times whether a given value is optional or not. If it is not optional, then you may assert so using a type declaration if your language supports it :
method name : string (* not optional *) method email : string option (* optional *)
In other languages, a runtime assert will do, for instance in PHP :
assert (isset($this->name))
OCaml will not require you to explicitly declare all values as optional or mandatory. Instead, it will deduce that information from the way you use each value. For instance, since json_of_string expects a non-optional string argument, you can simply write :
let parsed_content = json_of_string json (* json is not optional *)
If json is an optional string, this will create a compilation error unless you explicitly define what should happen when the string is missing. The most common approach is to decide that the result should be missing too :
let parsed_content = match json with | None -> None | Some s -> Some (json_of_string s) (* by definition, s is not optional *)
If using the Batteries library (which, by the way, you should), you can express this more easily :
let parsed_content = BatOption.map json_of_string json
Set a default value with assignments
This advice is almost identical to the previous one : if you need to construct a non-optional value, but only have access to an optional value, you will have to provide a default value. Using standard OCaml :
let role = match person.role with | None -> `Guest | Some role -> role
Using Batteries, there is an almost equivalent version :
let role = BatOption.default `Guest person.role
This version is almost equivalent, because it will evaluate the default value even if it is not required. Let’s assume that the default value is itself provided by a complex computation, such as a database access :
let role = match person.role with | None -> readfrom database (* only executed if person.role is missing *) | Some role -> role let role = BatOption.default (readfrom database) (* Always executed, even if unnecessary *) role
The Batteries library provides an alternate function for that :
let role = BatOption.map_default readfrom database (* only executed if person.role is missing *) role
Checking whether a variable equals to any of the given values
If the values have a legitimate reason to be strings, integers or other types with unlimited numbers of values, then « is in list » predicates are the preferred choice :
if List.mem current_day ["Monday";"Wednesday";"Friday"] then (* do something *)
Days are a quite bad example, because these are better represented as a variant (which then type-checks whether you have written Mornday instead of Monday). If you have already defined the type of weekdays, then :
if List.mem (current_day : weekday) [`Monday;`Wednesday;`Friday] then (* do something *)
If this is a one-shot check, or if you would rather not define a weekday type yet, you should instead go for an exhaustive pattern-matching :
match current_day with | `Monday | `Wednesday | `Friday -> (* do something *) | `Tuesday | `Thursday | `Saturday | `Sunday -> ()
Extract complex or repeated logic into functions
This is a fairly fundamental concept — but it consists in two distinct parts. There is a separation and naming part (you pull out a piece of code and give it a name, which helps understand what it does and how it relates to the rest of the program) and there is an extraction and reuse part (the piece of code is pulled out into a more globally accessible location and parametrized, so that it may be used in other places).
With the above example, simple separation-and-naming would be :
let is_discount_day = match current_day with | `Monday | `Wednesday | `Friday -> current_date > 20 | `Tuesday | `Thursday | `Saturday | `Sunday -> false in if is_discount_day then (* do something *)
The variable is defined in the same scope it is use in, and it assumes that current_day and current_date values have been defined previously in that scope. Extraction-and-reuse would go further :
type weekday = [ `Monday | `Tuesday | `Wednesday | `Thursday | `Friday | `Saturday | `Sunday ] let is_discount_day (day:weekday) date = List.mem day [`Monday;`Wednesday;`Friday] && date > 20 ... if is_discount_day current_day current_date then (* do something *)
Now, is-discount-day is a global function available from everywhere in the code, and it uses the provided parameters to determine whether this is indeed a discount day.
Memoize the results of repeated function calls
OCaml has several ways to perform memoization. One of them is lazy evaluation :
val discount_day = lazy (is_discount_day current_day current_date)
method discount_day = Lazy.force discount_day
The lazy expression will only be evaluated the first time the Lazy.force function is called on it.
Note that if the current day or current date can change, then the memoization actually breaks things !
Memoization is also helpful when dealing with a function that requires arguments, in which case a different result will be provided for each argument set. A common solution is to use a hash table to store these :
let fibonacci = let memo = Hashtbl.create 100 in let rec fib n = try Hashtbl.find memo n with Not_found -> let result = fib (n-1) + fib (n-2) in Hashtbl.add memo n result ; result in fib
This works fine for short-lived functions — don’t do this for global functions that might stick around for a long time, because the memoization hash table will grow and its contents will never be garbage-collected. If you really have to, use a weak hash table, such as Batteries’ BatInnerWeaktbl, so that the garbage collector may reclaim the memoized values when it runs out of memory.
Also don’ t overdo memoization — it only works when arguments are reliably passed more than once and the time to compute the value is significantly larger than the time to retrieve and store it and it is worth the memory usage and the function has no side-effects.
Use the seven list manipulation primitives
Almost any processing on collections of items can be expressed in terms of seven fundamental patterns. Recognizing those patterns can help improve the clarity of both the code and the underlying algorithm.
1. Map transforms a list into another, item by item, in linear time. Use a map operation when all you need is a one-to-one transformation. The line below extracts three recipes from the database using their identifier.
let recipes = List.map from_database [ "omelet" ; "cheeseburger" ; "risotto" ]
2. Reduce transforms a list of values into a single value by repeatedly applying a function that combines together two values into one. The typical example is a fold, which uses a function to combine each list element, in turn, with an accumulator. It can be used to extract the sum of values in a list, for example :
let total = List.fold_left (+) 0 [ 5 ; 6 ; 3 ; 8 ; 9 ; 0 ; 7 ; 6 ]
This transform allows a preliminary map step which transform the values inside the list into values that can be combined. For instance, to find the age of the oldest person in a list of people :
let oldest = List.fold_left (fun acc person -> max age person.age) 0 people
3. Extract works like a map, but the transformation function returns zero, one or more results for each call. All the results are included in the final list. The most elementary implementation is literally to have a map (that transforms a list into a list of lists) followed by a concatenation (that transforms a list of lists into a list). For instance, to get all the ingredients involved in a list of recipes :
let ingredients = List.concat (List.map (fun recipe -> recipe.ingredients) recipes)
4. Filter is a subset of Extract where the transform may not return more than one result — but it may still return none, so its result is simply an optional type. For instance, to extract the list of all recipes that have a wine recommandation along with their recommended wine :
let wines = BatList.filter_map (fun recipe -> BatOption.map (fun wine -> recipe,wine) recipe.wine) recipes
The OCaml language also provides a standard List.filter function which keeps values for which a property is true. For instance, to get the list of recipes that have a wine recommendation :
let have_wines = List.filter (fun recipe -> recipe.wine <> None) recipes
This approach is weaker — the wines in the resulting list are still treated as optional by the type system, so you will need bogus pattern matching for a case that never happens (no wine) to extract the actual wine values. A filter_map lets you encode the filter property in the type of the result, which makes using the filtered list easier.
5. Sort unsurprisingly sorts the list. The canonical sort — using the canonical order relationship — is a theoretical curiosity, and in practice most sorts use a projection function p such that A < B iff p(A) < p(B). This is best illustrated in SQL, in the form of the ORDER BY <projection> statement. Two useful helper functions :
let project compare p a b = compare (p a) (p b) let descending compare a b = compare b a
For instance, to sort the list of recipes based on how long each of them takes :
let by_duration =
List.sort (project compare (fun recipe -> recipe.duration)) recipes
6. Group works like Sort, but further regroups « equal » items together by returning a list of lists. It works using a comparison function that is usually based on a projection function. For instance, to get three lists containing one-star, two-star and three-star recipes :
let by_stars =
BatList.group (project compare (fun recipe -> recipe.stars)) recipes
There is a special case when the projection function returns booleans, which is known as a partition. For instance, to extract recipes that are desserts and recipes that are not :
let desserts, non_desserts =
BatList.partition (fun recipe -> recipe.is_dessert) recipes
7. Search extracts one element from the list (if possible) based on a certain condition or property. Elementary searches are « first element » and « last element. » More complex searches : finding a value by key in a list of key-value pairs using List.assoc or using a predicate using List.find. The heavy-duty search tool is BatList.find_map, used below to find a recipe that is recommended by at least one person, and the recommending person :
let recipe, recommender = BatList.find_map (fun recipe -> BatOption.map (fun person -> recipe, person) recipe.recommended) recipes
These seven patterns can be used in conjunction to perform almost any algorithm on collections, sequences or lists. For a more complex example, assume we need the ten ingredients that appear the most often in desserts. We would filter recipes by desserts, extract their ingredients, group them by name, sort the sub-lists by list length and take the name of the first element of the first ten sub-lists :
open BatPervasives (* for operator |> *) open BatList let ten_best_ingredients recipes = recipes |> filter (fun r -> r.dessert) |> map (fun r -> r.ingredients) |> concat |> group (project compare (fun i -> i.name)) |> sort (descending (project compare length)) |> take 10 |> map hd |> map (fun i -> i.name)
Article © brewbooks — Flickr
Of all the existing constraint-solving algorithms out there, my favourite is simulated annealing. To use it, you need a mutation function that randomly and slightly alters a solution, and a fitness function that lets you compare two solutions. Based on these, the algorithm is simple :
Grant Muller writes that 





Hi. I'm Victor Nicollet,
Recent Comments