This is an account of the problem solving example covered `live' in a first year lecture.
Background references to problem solving material are:
"Madam I'm Adam."
A palindrome is a string of text which reads the same backwards and forwards, ifAt this stage we can start to say something about the Miranda program we are to write. We will be writing a function, whose name and type we can give at this point. We will call the function
- we disregard the punctuation (punctuation marks and spaces) in the string;
- we disregard the case (upper or lower: that is capital or small) of the letters in the string.
What is its type? It will have an argument: the string we are checking; the result of the test will be a Boolean, sopalin
palin :: string -> bool
which says exactly that we have to reverse the string (reverse st) and compare it with the original ( ... = st). This means that we have to solve the problem of reversing a string:palin st = (reverse st = st)
How can we solve the whole problem? We need to do the same, but to a string which has had its punctuation and case disregarded:reverse :: string -> string
where the function which disregards punctuation and case ispalin st = (reverse st' = st') where st' = disregard st
which takes and returns a string.disregard :: string -> string
To reverse a string, which is a list of characters ([char]), we need to define the function from scratch (unless we look in the Miranda standard environment!). How to start with this? We can think of this being written in stages, left hand side first:
which are the two cases of an empty string and a non-empty string whose first element is a and whose remainder (or tail) is st.reverse :: [char] -> [char] reverse [] = reverse (a:st) =
An empty string reversed is empty:
while in the general case we can be guided by an example. In this sort of definition we define reverse (a:st) using reverse st. Take the example "door". In reversing the tail we have "roo" and we get what we want by sticking "d" at the end. So,reverse [] = []
where ++ joins together two strings and [a] is the string made up of the single character a.reverse (a:st) = reverse st ++ [a]
The final problem to solve is to find disregard which as we saw above consists of two parts:
We make disregard by applying these in turn. There are two ways we might do this:remove :: string -> string change :: string -> string
Which might we choose? Here is an example of reflecting upon our design par excellence; we can choose this without having implemented either function. We choose to do the latter, since under this definition we only change those items which remain in the string. Incidentally, we can write this definition in a different way again:disregard st = remove (change st) disregard st = change (remove st)
which says that the disregard function is made by composing the functions change and remove; first remove is applied, then change is applied to the result.disregard = change . remove
What about the (a:st) case? There will be two cases, depending whether a is a punctuation character or not:remove :: [char] -> [char] remove [] = [] remove (a:st) =
In the first case, a goes into the listremove (a:st) = .... , if notPunct a = .... , otherwise
In both cases the remainder of the result is got by removing punctuation from st:remove (a:st) = a : .... , if notPunct a = .... , otherwise
where we can check whether we have punctuation or not in a number of ways:remove (a:st) = a : remove st , if notPunct a = remove st , otherwise
where we say that ch is not one of the punctuation characters, ornotPunct :: char -> bool notPunct ch = ~(ch='.' \/ ch = ',' \/ .... )
where we say that we have either a letter or a character.notPunct ch = isLetter ch \/ isDigit ch
Finally, we need to think how to define
Here we need to affect each character in the list by converting it; this follows the pattern of double in the notes.change :: [char] -> [char]
and wherechange [] = [] change (a:st) = convert a : change st
convert :: char -> char convert ch = decode (code ch + offset) , if 'A' <= ch <= 'Z' = ch , otherwise where offset = code 'a' - code 'A'
An executable version of the program can be found here.