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.

42CHAPTER 4. COMPUTING WITH FUNCTIONS OVER INDUCTIVELY DEFINED SETS<br />

©: Michael Kohlhase 63<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 />

Definition 99 anonymous variables (if we are not interested in one value)<br />

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

val x = 1 :int<br />

Example 100 We can define the selector function for pairs in SML as<br />

- fun first (p) = let val (x,_) = p in x end;<br />

val first = fn : ’a * ’b -> ’a<br />

Note the type: SML supports universal types with type variables ’a, ’b,. . . .<br />

first is a function that takes a pair of type ’a*’b as input and gives an object of type ’a<br />

as output.<br />

©: Michael Kohlhase 64<br />

Another unusual but convenient feature realized in SML is the use of pattern matching. In<br />

pattern matching we allow to use variables (previously unused identifiers) in declarations with the<br />

understanding that the interpreter will bind them to the (unique) values that make the declaration<br />

true. In our example the second input contains the variables x and y. Since we have bound the<br />

identifier unitvector to the value (1,1), the only way to stay consistent with the state of the<br />

interpreter is to bind both x and y to the value 1.<br />

Note that with pattern matching we do not need explicit selector functions, i.e. functions that<br />

select components from complex structures that clutter the namespaces of other functional languages.<br />

In SML we do not need them, since we can always use pattern matching inside a let<br />

expression. In fact this is considered better programming style in SML.<br />

What’s next?<br />

More SML constructs and general theory of functional programming.<br />

©: Michael Kohlhase 65<br />

One construct that plays a central role in functional programming is the data type of lists. SML<br />

has a built-in type constructor for lists. We will use list functions to acquaint ourselves with the<br />

essential notion of recursion.

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

Saved successfully!

Ooh no, something went wrong!