25.07.2014 Views

VDM-10 Language Manual

VDM-10 Language Manual

VDM-10 Language Manual

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.

<strong>VDM</strong>-<strong>10</strong> <strong>Language</strong> <strong>Manual</strong><br />

Examples: The operation Remove demonstrates the use of a sequence-for loop to remove all<br />

occurences of a given number from a sequence of numbers:<br />

✞<br />

✡✝<br />

Remove : (seq of nat) * nat ==> seq of nat<br />

Remove (k,z) ==<br />

(dcl nk : seq of nat := [];<br />

for elem in k do<br />

if elem z<br />

then nk := nkˆ[elem];<br />

return nk<br />

);<br />

✆<br />

A set-for loop can be exploited to return the set of winners of all groups:<br />

✞<br />

✡✝<br />

GroupWinners: () ==> set of Team<br />

GroupWinners () ==<br />

(dcl winners : set of Team := {};<br />

for all gp in set dom gps do<br />

(dcl winner: Team := GroupWinner(gp);<br />

winners := winners union {winner}<br />

);<br />

return winners<br />

);<br />

✆<br />

An example of a index-for loop is the classic bubblesort algorithm:<br />

✞<br />

✡✝<br />

BubbleSort : seq of nat ==> seq of nat<br />

BubbleSort (k) ==<br />

(dcl sorted_list : seq of nat := k;<br />

for i = len k to 1 by -1 do<br />

for j = 1 to i-1 do<br />

if sorted_list(j) > sorted_list(j+1)<br />

then (dcl temp:nat := sorted_list(j);<br />

sorted_list(j) := sorted_list(j+1);<br />

sorted_list(j+1) := temp<br />

);<br />

return sorted_list<br />

)<br />

✆<br />

<strong>10</strong>8

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

Saved successfully!

Ooh no, something went wrong!