ciao
ciao copied to clipboard
call_nth/2 (was: (playground) is there a Ciao Prolog equivalent of call_nth/2 ?)
SWI-Prolog has call_nth/2, originally in library solution_sequences, but meanwhile also adopted by a few other Prolog systems in various locations, for the SWI-Prolog definition see here:
call_nth(:Goal, ?Nth) True when Goal succeeded for the Nth time. If Nth is bound on entry, the predicate succeeds deterministically if there are at least Nth solutions for Goal. https://www.swi-prolog.org/pldoc/doc_for?object=call_nth/2
I didn't find a predicate by the same name. I need it to run this code:
{"quote":"Winning isn’t everything, but wanting to win is.","author":"Vince Lombardi"}.
{"quote":"I am not a product of my circumstances. I am a product of my decisions.","author":"Stephen Covey"}.
{"quote":"You miss 100% of the shots you don’t take.","author":"Wayne Gretzky"}.
{"quote":"Every strike brings me closer to the next home run.","author":"Babe Ruth"}.
{"quote":"Whether you think you can or you think you can’t, you’re right.","author":"Henry Ford"}.
member_comma(Z, (X,_)) :- member_comma(Z, X).
member_comma(Z, (_,Y)) :- !, member_comma(Z, Y).
member_comma(X, X).
:- op(100,yfx,'.').
:- op(800,xfx,':=').
X := A.B :- member_comma(B:X, A).
main :- statistics(wall, W), N is W mod 5+1, call_nth({ X }, N), Q := X."quote", A := X."author",
write(Q-A), nl.
Interestingly Ciao Prolog does not have problems parsing
the non-terminating period '.'. Hurray!
But the obstacle is now some call_nth/2, since I get:
?- use_module('/draft.pl').
{Compiling /draft.pl
ERROR: (lns 16-18) Predicate call_nth/2 undefined in source
ERROR: Aborted module compilation
}
More information here: https://www.complang.tuwien.ac.at/ulrich/iso-prolog/call_nth
Regarding .
, yes, we can define as an infix operator. We needed it for some language extensions (I'm aware that some of them were introduced first in Ciao and then appeared in other systems, but it could be just convergent evolution).
I've edited the name of this issue since this is not related to the playground.
Since you have '$setarg'/4 I guess it would be quite trivial to implement call_nth/2?