Search code examples
listsortingprologprolog-setof

Prolog : Ordering a list alphabetically


I have the following problem, i'm supposed to order a List containing only Strings in alphabetical order using only 'bagof', 'findall', 'setoff' , i am not suposed to use any sort of sorting algorithm.

Here is the content of the .pl document i have to make the list with.

musico('R. Stevie Moore').
musico('Lane Steinberg').
musico('Yukio Yung').
musico('Jessie Evans').
musico('Miguel').
musico('Lucia Pamela').
musico('Shooby Taylor').
musico('Tiny Tim').
musico('The Legendary Stardust Cowboy').

Here is what i have so far:

all_musicians([Z]) :-
    findall(X, musico(X), Z).

This makes the list containing all musicians, but now i cant seem to understand how to order it.


Solution

  • setof/3 is what you want, since it produces a sorted list of results.

    all_musicians(Z) :- 
        setof(X, musico(X), Z).