The following is a very simple example of a swap function. It is described in high detail so that hopefully it covers most of the possible ways in which one can write a swap function :) This simple example also serves to introduce you to both languages in case you don't know both languages - or any of the languages at all.
First of all this is how to write comments in the langauges:
/* This is a comment in C or C++ */(* This is a comment in ML *)
A swap function written in C would problably look something like this:
void swap(int *a, int *b) { int temp;temp = *a; *a = *b; *b = temp; }
It works by declaring the parameters to the function as pointers to integers. The pointer type is written as a '*' symbol. A temporary variable temp is declared in the function. The values are swapped using assignments where '*' symbols are necessary to get to the values of the pointer variables. A call to this function could be:
swap(q, r); This would swap the values held in q and r. The call passes the adresses of q and r to the function. The 'adress of' operator in C is the '&' operator.
In ML one could implement a swap function like this:
fun swap(a, b) = (b, a)It just takes two variables as arguments and returns them again where they are swapped. Actually the function takes one tuple as argument and also returns one tuple, but never mind that for now. A call to this function might be:
val (q, r) = swap(q, r)Here the values of the returned variables q and r have been swapped compared to those passed as arguments. However it should be noted that the variables returned are two new variables - not modifed versions of the arguments. So we could have made this call instead:
val (s, t) = swap(q, r)In this case one keeps the old q and r values and also get new s and t variables. This is because ML is a functional language. In a functional language you can never change the value of a variable - only create new variables with a value. Actually ML also has reference variables which can be modified, but it is usually a good idea to avoid using them.
If we're writing in C++ the swap function in C can be made slightly prettier by using reference parameters:
void swap(inta, int b) /* declares reference types in C++ */ { int temp; temp = a; a = b; b = temp; }
Now the call to the function is also prettier:
swap(q, r);So what did we miss? First of all the C and C++ versions of the swap function can only swap integers! The ML version does not care which types of values are passed to it. So if we want to swap two characters in C we would need to write a new function:
void swapchars(char *a, char *b) { char temp; temp = *a; *a = *b; *b = temp; }This is virtually the same as before except that this function swaps characters. It is also called just as before, except that the variables to be swapped must be characters instead of integers. In ML we just use the same function - and it will work!
If we're using C++ we could implement the swap function by using templates. This will look something along the lines of:
<template T> void swap(Ta, T b) { T temp; temp = a; a = b; b = temp; }
This implementation of swap can actually swap variables of mulitiple types. For instance it can swap 2 integers with a call like this:
swap<int>(q, r);It can also swap 2 characters with this call:
swap<char>(q, r);Now let's move on to a more serious problem with the swap functions in C and C++ implemented as above. They cannot swap variables of two different types - unless you are lucky enough to have two types which occupy the same amount of memory and that you also do some ugly typecasts. This limitation is caused mainly because the result is returned in the same variables that are used to pass the parameters. In the ML version we can easily swap two values of different types:
val (q, r) = swap("Hello", 5)After this call q will have the integer value 5 and r will have the string value "Hello".
Modified: 2023-05-19
E-mail: Contact