Introduction to Computational Linguistics
Introduction to Computational Linguistics
Introduction to Computational Linguistics
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
9. Objects and Methods 23<br />
of type ’a <strong>to</strong> objects of ’b -> ’a. It is legitimate <strong>to</strong> apply it <strong>to</strong> just one object:<br />
(63)<br />
# k "cat";;<br />
− :<br />
’_a −> string = <br />
This means that if you give it another argument, the result is a string (because the<br />
result is "cat"). You can check this by assigning an identifier <strong>to</strong> the function and<br />
applying it:<br />
(64)<br />
# let katz = k "cat";;<br />
val katz : ’_a −> string = <br />
# katz "mouse";;<br />
- : string = "cat"<br />
Effectively, this means that you define functions by abstraction; in fact, this is<br />
the way in which they are defined. However, the way in which you present the<br />
arguments may be important. (However, you can define the order of the arguments<br />
in any way you want. Once you have made a choice, you must strictly obey that<br />
order, though.)<br />
9 Objects and Methods<br />
The role of variables is played in OCaMl by objects. Objects are abstract data<br />
structures, they can be passed around and manipulated. However, it is important<br />
that OCaML has <strong>to</strong> be <strong>to</strong>ld every detail about an object, including how it is accessed,<br />
how it can communicate the identity of that object <strong>to</strong> you and how it can<br />
manipulate objects. Here is a program that defines a natural number:<br />
(65)<br />
class number =<br />
object<br />
val mutable x = 0<br />
method get = x<br />
method succ = x