ciao-lang / ciao

Ciao is a modern Prolog implementation that builds up from a logic-based simple kernel designed to be portable, extensible, and modular.
https://ciao-lang.org
GNU Lesser General Public License v3.0
272 stars 21 forks source link

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

Open Jean-Luc-Picard-2021 opened 2 years ago

Jean-Luc-Picard-2021 commented 2 years ago

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.
jfmc commented 2 years ago

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

jfmc commented 2 years ago

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 commented 2 years ago

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