Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Jean-Luc-Picard-2021 opened this issue Aug 22, 2022 · 3 comments

Comments

@Jean-Luc-Picard-2021
Copy link

Jean-Luc-Picard-2021 commented Aug 22, 2022

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
Copy link
Member

jfmc commented Aug 24, 2022

@jfmc
Copy link
Member

jfmc commented Aug 24, 2022

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 jfmc changed the title (playground) is there a Ciao Prolog equivalent of call_nth/2 ? call_nth/2 (was: (playground) is there a Ciao Prolog equivalent of call_nth/2 ?) Aug 24, 2022
@jfmc
Copy link
Member

jfmc commented Aug 24, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants