Blog - ocaml

Ocaml for CGI. URL encoding and decoding

Sat May 5 19:59:03 EST 2007

I want to try to use Ocaml for Common Gateway Interface (CGI). There are a number of libraries out there to do this, but as an exercise I want to have a shot myself first. The first task is to write functions that escape or unescape characters in a URI. Checking [RFC 2396] shows that aside from a limited set of unreserved characters everything else needs to be encoded into the form %20 = space, %25 = % etc if it is to be passed as a query string parameter to the URI, or as POST data.

let inrange x low high = (x >= low) && (x <= high)
let isupper c = inrange c 'A' 'Z'
let islower c = inrange c 'a' 'z'
let isnum c = inrange c '0' '9'
let isalphanum c = (isupper c) || (islower c) || (isnum c)
let ismarker c = match c with
  |'-' | '_' | '.' | '!' 
  | '~' | '*' | '\'' | '(' | ')' -> true
  | _ -> false
let unreserved c = (isalphanum c) || (ismarker c)

let char_to_str = String. (...)




Sun Dec 3 12:23:24 EST 2006

A stream cipher can be thought of as a machine that has internal state and generates a stream of random values as an input stream is fed into the machine, with a stream of random values coming out of the output when the input stream and generated stream are combined. The advantage of this viewpoint is that a general control function can be constructed that can take a number of different encryption machines as arguments eg a Caeser shift cipher, Vignere cipher or modern stream cipher such as RC4. (...)

[code] [ocaml]


More playing with blogging software

Tue Nov 28 20:33:30 EST 2006

My homebrew blogging software starts with a plaintext file and uses a lexer and parser to split this into a list of entries. It then iterates over the list, printing each entry to a category page (eg code or ocaml), the main page and a permalink page.

One problem with this as it stands is that each permalink gets regenerated each time the blogging program is run. This is usually unnecessary since previous blog entries rarely change. It is even more annoying because the permalink entries are the full versions, not restricted to the first 1000 characters of the entry.

Solution: store in a file a record of which entries have been processed, and ideally be able to recognize whether an entry has changed. Do this by creating a hashtable of permalink string (YYYYMMDD_HHMMSS) and MD5 digest of the actual output string. The relevant bit of the main program is shown below:

read_permhash "permhash.txt";
  (fun entry -> 
    let tlst = Entry. (...)

[code] [ocaml]


Tries, dictionaries and directories

Sat Nov 25 09:02:43 EST 2006

A trie, or prefix tree, is a tree structure that maps keys to values stored in the tree. The tree itself consists of a set of labelled edges and nodes containing the values. The key corresponding to the value at a particular node is the concatenation of the path labels leading from the root of the tree to the node.

A standard application is to use a trie to represent a dictionary (see the exercises at the end of chapter 2 of the ORA Ocaml book). Here the edges are labelled by the characters making up a word, and the associated value is a boolean stating whether the node is the end of a word.

A trie can also be used to represent the layout of a website consisting of a root index page followed by a hierarchy of subpages (ie the layout of my website). Here the node values are wrapper functions that wrap content depending on its position in the tree, for example producing a navigation menu that depends on the position of the element in the tree. (...)

[code] [ideas] [ocaml]


A calculator for uncertain quantities using Ocaml

Sun Nov 12 10:36:43 EST 2006

Error propagation is an important tool for measuring the uncertainty of a final result based on the uncertainties of the underlying measurements. It is also the bane of many first year science students' lives. A calculator that worked with uncertain quantities and did the error propagation automatically would be a useful tool, eg 22.0,5.0% + 10.0,1.0 to add 22 with an uncertainty of 5% to 10 with an uncertainty of 1.

Here I show how to accomplish this using Ocaml. Ocaml is a good choice for this task because the program can be broken down easily into its independent sub-tasks using the language facilities:

  • use the module system to define an UncertainNumber abstract datatype, with addition, subtraction, multiplication and division operations defined for this datatype.
  • use ocamllex and ocamlyacc to construct a calculator that deals with this datatype. (...)

[code] [ocaml]


code (31)

erlang (6)
ideas (24)
lisp (1)
me (16)
notes (6)
ocaml (5)
physics (46)
qo (7)
unix (8)
vim (4)