1

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.

repeat
  • 18,496
  • 4
  • 54
  • 166
eXistanCe
  • 735
  • 1
  • 9
  • 25
  • 1
    Keep in mind that `setof` is `bagof` + `sort`. But why did you not read the documentation of `setof` and `bagof`? –  Oct 06 '15 at 08:28

1 Answers1

2

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

all_musicians(Z) :- 
    setof(X, musico(X), Z).
VHarisop
  • 2,816
  • 1
  • 14
  • 28
  • Can setof be used to sort all types of Lists containing only one kind of variables? What if the List contains multiple types of variables? – eXistanCe Oct 06 '15 at 08:19
  • @eXistanCe Just read the documentation, in particular how `sort/2` sorts and what "standard order of terms" means. –  Oct 06 '15 at 08:28
  • 1
    @eXistanCe yes, `setof/3` or `sort/2` can sort all kinds of list elements. You should read the documentation which explains rules for [Standard Order of Terms](http://www.swi-prolog.org/pldoc/man?section=standardorder). – lurker Oct 06 '15 at 11:15