CAML

In consequence of my priority checking, I’m trying to focus my space time on programming. In particular, I’m (re)learning CAML (technically OCAML, but I haven’t used the object system yet) I’m doing toy projects for now; trying to tackle hard problems while wrestling with syntax and paradigms is a recipe for disaster.

Interactive top-level program are nice for learning languages, but CAML’s static binding causes some difficulty – having changed a definition, you then have to re-submit everything that depends on it. The little bit of emacs integration helps a little, but every time I want to test the function of a change, I have to select most of the file and then submit it.

CAML itself, however, is quite fun, and to any programmer who hasn’t experienced the madness of functional programming, I can’t recommend it enough.

For instance, in my first project, another implementation of my old text-based bidding combat game, has something like this for it’s main loop:

let players = [player1; player2];;

let to_players f =
  List.map f players
;;

let player_turns = [
  print_player;
  get_move; 
  deduct_power; 
  early_magic;
  late_magic;
  describe_magic;
  describe_combat;
  do_fight;
  recover;
];;

let round () = 
  print_header();
  List.map to_players player_turns;
;;

let play () = 
  start();
  while (to_players is_alive) do
    round()
  done
;;

Later, I started learning the graphics library for my second program. Finding that I had to manage edge detection on the mouse button myself, I started trying to do the standard nested-if familiar from my C experience. But this always feels clumsy, and given the short functions engendered by CAML, it felt even more so. So I came up with something like this:

let check_button () =
  match (!current_button, Graphics.mouse_button()) with
    (false, true) -> current_button := true; true
  | (true, false) -> current_button := false; false
  | _ -> false
;;

http://www.ocaml.org

http://www.bagley.org/~doug/shootout/

Posted Friday, April 30th, 2004 under Review.

Comments are closed.