Discussions related to Visual Prolog
Michel
Posts: 17
Joined: 8 Nov 2025 17:17

Problem with List sorting...

Post by Michel »

Hello from France.

It takes time to me to jump form "C" and "Turbo Prolog 1,2" to "Visual Prolog", but now I begin to like it.

Here is part of my code, my problem and my question.

Code: Select all

class facts     remedes : string* := []. /* remedies list, empty */   clauses     setRemedies(RemedesToAdd) :-         write("\n\nLa liste des remèdes brute à ajouter ", RemedesToAdd, "\ndevient après traitement :\n"),         computeRemedies(remedes, RemedesToAdd),         write("Les remèdes : ", remedes),         sortList(remedes).   class predicates     computeRemedies : (string*, string*). clauses     computeRemedies(remedes, RemedesToAdd) :-         X = list::getMember_nd(RemedesToAdd),         if list::isMember(X, remedes) then             Index = tryGetIndex(X, remedes),             Valided = nth(Index + 1, remedes),             Int = toTerm(integer, Valided) + 1,             ReturnTerm = toString(Int),             remedes := setNth(Index + 1, remedes, ReturnTerm)         else             remedes := list::append(remedes, [X, "1"])         end if,         fail.     computeRemedies(_, _).
It works fine... As you can see, I don't add any double but I increment the number
of the valided symptom :
La liste des remèdes brute à ajouter :

["Arnica","Tabacum","Placebo"]

devient après traitement :

["Arnica","1","Tabacum","1","Placebo","1"]

La liste des remèdes brute à ajouter :

["Arnica","Placebo"]

devient après traitement :

["Arnica","2","Tabacum","1","Placebo","2"]
Perfect, it is exactly what I expected... but, I don't succeed to get a sorted list based on the number of
time of each validation. How may I declare predicates and clauses
to obtain such a result :

["Arnica","2","Placebo","2","Tabacum","1"] and so on.

Such a result gives me the possibilty to choose one or some more best remedies for my patient.

Very warm regards for such help and, by the way, in the forum, does somebody speak french or use VP in France ?

Michel
Harrison Pratt
VIP Member
Posts: 469
Joined: 5 Nov 2000 0:01

Re: Problem with List sorting...

Post by Harrison Pratt »

Here are two ways of approaching what I believe you want to do:

The predicate test1() uses a list fact for temporary storage and uses tuples to bundle the remedies and their counts into a convenient data structure that you can sort in various ways using list::sortBy/3. Search the Language Reference (Book) for "sortBy" to see other ways of sorting.

The predicate test2() uses remedyCount( string, integer ) database facts to store the accumulated remedies and their counts.

If you are familiar with the VP debugger, you can set a breakpoint at each if the 2 nothing/2 statements to see the sorted outputs. Otherwise, you may want to write the sorted lists to stdio.

I hope this helps.
You do not have the required permissions to view the files attached to this post.
Michel
Posts: 17
Joined: 8 Nov 2025 17:17

Re: Problem with List sorting...

Post by Michel »

It look's great and, much of all, I shall be at last able to understand the tuple theory...

More after test, Best regards. Michel
Michel
Posts: 17
Joined: 8 Nov 2025 17:17

Re: Problem with List sorting...

Post by Michel »

IT WORKS !!!! :D

Thanks a lot, Warm regards

Michel
User avatar
Thomas Linder Puls
VIP Member
Posts: 1501
Joined: 28 Feb 2000 0:01

Re: Problem with List sorting...

Post by Thomas Linder Puls »

I will recommend that you also pay attention to our collection library.

A here is a solution that holds remedies in a mapM for the counting (I have focused on the pure algorithm not the text out put):

Code: Select all

implement main     open core, stdio   clauses     run() :-         addRemedies(["Arnica", "Tabacum", "Placebo"]),         addRemedies(["Arnica", "Placebo"]),         write(sortedRemedies()).   domains     remedyCount = tuple{string, integer}.   class facts     remedies : mapM{string, integer} := mapM_redBlack::new().   class predicates     addRemedies : (string* RemediesToAdd). clauses     addRemedies(RemediesToAdd) :-         foreach Remedy in RemediesToAdd do             OldCount = remedies:get_default(Remedy, 0),             remedies:set(Remedy, OldCount + 1)         end foreach.   class predicates     sortedRemedies : () -> remedyCount* Sorted. clauses     sortedRemedies() = Sorted :-         Unsorted = remedies:asList,         Sorted = list::sortBy(compareRemedyCount, Unsorted).   class predicates     compareRemedyCount : comparator{remedyCount}. clauses     compareRemedyCount(tuple(Name1, Count1), tuple(Name2, Count2)) = Result :-         Result = core::notEqual(compare(Count2, Count1)) otherwise compare(Name1, Name2).   end implement main
This program produces this output:
[tuple("Arnica",2),tuple("Placebo",2),tuple("Tabacum",1)]
I didn't explain anything. Ask if you need some explanations ;-).
Regards Thomas Linder Puls
PDC
Michel
Posts: 17
Joined: 8 Nov 2025 17:17

Re: Problem with List sorting...

Post by Michel »

Hello Thomas,
I did it like this :

Code: Select all

class facts     remedyTuples : tuple{integer Count, string RemedyName}* := [].   /********************************************************** * computeRemedies(List, Value) List = remedies to add Value = importance/impact of some remedies on the final choice **********************************************************/ clauses     computeRemedies(List, Value) :-         stdio::writef("\n\nAdd : %\n", List),         assertRemedies(List, Value),         ListeTriee = list::sort(remedyTuples, descending),         foreach tuple(N, R) in ListeTriee do             stdio::writef("--> %s    clicked %d number\n", R, N)         end foreach.   class predicates     assertRemedies : (string* RemedyNames, integer Value). clauses     assertRemedies(Remedies, Value) :-         foreach R in Remedies do             tuple(N, R) in remedyTuples,             UU = list::remove(remedyTuples, tuple(N, R)),             remedyTuples := [tuple(N + Value, R) | UU]             orelse             remedyTuples := [tuple(1, R) | remedyTuples]         end foreach.
It's fine but, by curiosity, I think about testing the possibility you wrote me....

One last question: moved to Question about objects

Best regards, Michel
User avatar
Thomas Linder Puls
VIP Member
Posts: 1501
Joined: 28 Feb 2000 0:01

Re: Problem with List sorting...

Post by Thomas Linder Puls »

Please put [code]...[/code] tags around all code lines.

And please: only one subject in a mail thread.
I have moved your "one last question..." in another thread.
Regards Thomas Linder Puls
PDC
Michel
Posts: 17
Joined: 8 Nov 2025 17:17

Re: Problem with List sorting...

Post by Michel »

Dear Thomas,

I am sorry !

Regards, Michel