15.08.2013 Views

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Definition 88 types are program constructs that classify program objects into categories.<br />

In SML, literally every object has a type, and the first thing the interpreter does is to determine<br />

the type of the input and inform the user about it. If we do something simple like typing a number<br />

(the input has to be terminated by a semicolon), then we obtain its type:<br />

- 2;<br />

val it = 2 : int<br />

In other words the SML interpreter has determined that the input is a value, which has type<br />

“integer”. At the same time it has bound the identifier it to the number 2. <strong>General</strong>ly it will<br />

always be bound to the value of the last successful input. So we can continue the interpreter<br />

session with<br />

- it;<br />

val it = 2 : int<br />

- 4.711;<br />

val it = 4.711 : real<br />

- it;<br />

val it = 4.711 : real<br />

Programming in SML (Declarations)<br />

Definition 89 (Declarations) allow abbreviations for convenience<br />

value declarations val pi = 3.1415;<br />

type declarations type twovec = int * int;<br />

function declarations fun square (x:real) = x*x; (leave out type, if unambiguous)<br />

SML functions that have been declared can be applied to arguments of the right type, e.g.<br />

square 4.0, which evaluates to 4.0 * 4.0 and thus to 16.0.<br />

Local declarations: allow abbreviations in their scope (delineated by in and end)<br />

- val test = 4;<br />

val it = 4 : int<br />

- let val test = 7 in test * test end;<br />

val it = 49 :int<br />

- test;<br />

val it = 4 : int<br />

c○: Michael Kohlhase 61<br />

While the previous inputs to the interpreters do not change its state, declarations do: they bind<br />

identifiers to values. In the first example, the identifier twovec to the type int * int, i.e. the<br />

type of pairs of integers. Functions are declared by the fun keyword, which binds the identifier<br />

behind it to a function object (which has a type; in our case the function type real -> real).<br />

Note that in this example we annotated the formal parameter of the function declaration with a<br />

type. This is always possible, and in this necessary, since the multiplication operator is overloaded<br />

(has multiple types), and we have to give the system a hint, which type of the operator is actually<br />

intended.<br />

Programming in SML (Pattern Matching)<br />

Component Selection: (very convenient)<br />

- val unitvector = (1,1);<br />

val unitvector = (1,1) : int * int<br />

- val (x,y) = unitvector<br />

val x = 1 : int<br />

val y = 1 : int<br />

35

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

Saved successfully!

Ooh no, something went wrong!