13.11.2014 Views

Introduction to Computational Linguistics

Introduction to Computational Linguistics

Introduction to Computational Linguistics

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5. Modules 15<br />

let length l = length_aux 0 l<br />

First, the function is defined via an auxiliary function. You expect therefore <strong>to</strong><br />

be able <strong>to</strong> use a function List.length_aux. But you cannot. The answer <strong>to</strong><br />

the puzzle is provided by the file list.mli. Apart form the commentary (which<br />

OCaML ignores anyway) it only provides a list of functions and their types. This<br />

is the type interface. It tells OCaML which of the functions defined in list.ml<br />

are public. By default all functions are public (for example, if you did not make<br />

a type interface file). If a type interface exists, only the listed functions can be<br />

used outside of the module. The second surprise is the definition of the function<br />

length_aux. It uses only one argument, but at one occasion uses two! The<br />

answer is that a function definition of the form let f x = specifies that x is<br />

the first argument of f , but the resulting type of the function may be a function<br />

and so take further arguments. Thus, unless you have <strong>to</strong>, you need not mention<br />

all arguments. However, dropping arguments must proceed from the rightmost<br />

boundary. For example, suppose we have defined the function exp x y, which<br />

calculates x y . The following function will yield [5; 25; 125] for the list [1;2;3]:<br />

(39) let lexp l = List.map (exp 5) l<br />

You may even define this function as follows:<br />

(40) let lexp = List.map (exp 5)<br />

However, if you want <strong>to</strong> define a function that raises every member of the list <strong>to</strong><br />

the fifth power, this requires more thought. For then we must abstract from the<br />

innermost argument position. Here is a solution:<br />

(41) let rexp = List.map (fun y -> exp y 5)<br />

A more abstract version is this:<br />

(42)<br />

let switch f x y = f y x;;<br />

let lexp = List.map ((switch exp) 5)<br />

The function switch switches the order of the function (in fact any function). The<br />

so defined reversal can be applied in the same way as the original, with arguments<br />

now in reverse order.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!