<< >> Up Title Contents

2.1.4. Exercises

Hint: in all of the following, the base involves the case where the first list argument is the empty list.

1. Define a predicate delete/3 whose arguments are 1) a term, 2) a list and 3) another list consisting of the second argument minus the first occurrence of the term, if it occurs. For example, delete(b, [a,b,c,a,b], X) should give X = [a,c,a,b] . Note also that delete/3 should not fail; if the item to be deleted is not on the list, the original list should be returned as the value of the third argument. E.g. delete(a, [b,c,d], X) should give X = [b,c,d]

2. Define a predicate delete_all/3, like delete/3 except that the third argument is minus all occurrences of the first argument. E.g. delete_all(b, [a,b,c,a,b], X) should give X = [a,c,a] . delete_all/3 should behave like delete/3 if its first argument is not on the list.

3. Define a predicate reverse/2 whose arguments are both lists, the second being the mirror image of the first. E.g. reverse([a,b,c], X) should give X=[c,b,a]. Hint: you will find it useful to use append/3 in the recursive clause of reverse/2.

4. Write a recursive definition that will translate a string of English words into their French (or German or Swedish or .... counterparts). You will need a set of 2-place facts giving the English and French counterparts, and a two-place recursive rule that works its way down a list, looking up the word-translations item by item, and putting the resulting translation into the second argument. The predicate should produce the following kind of result:

| ?- translate(['John', is, an, idiot], French).

French = [Jean,est,un,imbecile] yes


<< >> Up Title Contents