ciao icon indicating copy to clipboard operation
ciao copied to clipboard

call_nth/2 (was: (playground) is there a Ciao Prolog equivalent of call_nth/2 ?)

Open Jean-Luc-Picard-2021 opened this issue 2 years ago • 3 comments

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.

Jean-Luc-Picard-2021 avatar Aug 22 '22 21:08 Jean-Luc-Picard-2021

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
}

Jean-Luc-Picard-2021 avatar Aug 22 '22 21:08 Jean-Luc-Picard-2021

More information here: https://www.complang.tuwien.ac.at/ulrich/iso-prolog/call_nth

jfmc avatar Aug 24 '22 08:08 jfmc

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).

jfmc avatar Aug 24 '22 09:08 jfmc

I've edited the name of this issue since this is not related to the playground.

jfmc avatar Aug 24 '22 22:08 jfmc

Since you have '$setarg'/4 I guess it would be quite trivial to implement call_nth/2?

Jean-Luc-Picard-2021 avatar May 04 '24 03:05 Jean-Luc-Picard-2021