15.08.2013 Views

General Computer Science; Problems and Solutions for ... - Kwarc

General Computer Science; Problems and Solutions for ... - Kwarc

General Computer Science; Problems and Solutions for ... - Kwarc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

1.2.3 Inductively Defined Sets in SML<br />

Problem 1.24: Declare an SML datatype pair representing pairs of integers <strong>and</strong> define SML<br />

functions fst <strong>and</strong> snd where fst returns the first- <strong>and</strong> snd the second component of q the pair.<br />

Moreover write down the type of the constructor of pair as well as of the two procedures fst <strong>and</strong><br />

snd.<br />

Use SML syntax <strong>for</strong> the whole problem.<br />

Solution:<br />

datatype pair = pair of int * int; (* val pair = fn : int * int -> pair *)<br />

fun fst(pair(x,_)) = x; (* val fst = fn : pair -> int *)<br />

fun snd(pair(_,y)) = y; (* val snd = fn : pair -> int *)<br />

25<br />

8pt<br />

8min

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

Saved successfully!

Ooh no, something went wrong!