I am submitting an assignment and the activity says:
Given a score obtained by a competitor in a jump for a category also received as an argument, show the jump number index (the order number of the list). For example, in high jump HUGO LENIN obtained in category 2 a score of 8 the answer would be 2 because it is in the second place on the list. If the score received was not obtained in any jump, return -1, if the score is repeated, return the first one you find.
%jump(name, class, score
jump('HUGO LENIN',2,[7,8,8,5]).
%class (n° class, name class, number of attempts allowed)
class(1, 'Long Jump',4).
class(2,'High Jump',5).
I really don't know how to do it
CodePudding user response:
In SWI-Prolog, nth1(Index, List, Elem) is true when Elem is the Index’th element of List, counting from 1. Alternatively, you can use nth0/3, if you prefer counting from 0. Both predicates are defined in library(lists)
, which is autoloaded.
Examples:
?- nth1(Index, [a,b,c], b).
Index = 2 ;
false.
?- nth1(2, [a,b,c], Elem).
Elem = b.
If you only need to know the index of the first occurrence of the element in the list, you can also use the ISO built-in predicate once/1:
Examples:
?- once(nth1(Index, [a,b,c], b)).
Index = 2.
?- once(nth1(Index, [a,b,c,b], b)).
Index = 2.
?- nth1(Index, [a,b,c,b], b).
Index = 2 ;
Index = 4.