prolog: built-in or other method to use function-like return values
I'm trying to le开发者_JAVA技巧t Prolog instantiate a variable to the opposite key of a public/private key pair, if that is even possible.
So I basically have:
publicKey(k_c).
privateKey(k_c-1).
keyPair(k_c,k_c-1).
Given the nature of Prolog I don't think you could manage something like
inverseOf(X) :-
(keyPair(X,Y), return(Y),!);(keyPair(Y,X),return(Y),!).
Why I want/need this?
I have a rule
init(Init_1, Init_2,Init_3)
and I want to check for something like
Init_3 == inverseOf(Init_2).
Any ideas?
Define
inverseOf(X, Y):- keyPair(X,Y), !.
inverseOf(X, Y):- keyPair(Y,X), !.
and then use
inverseOf(Init_2, Init_3).
With something like this defined:
keypair(pub1, priv1).
keypair(pub2, priv2).
keypair(pub3, priv3).
keymatch(A, B) :- keypair(A, B).
keymatch(A, B) :- keypair(B, A).
You could ask things like:
keymatch(pub1, X).
keymatch(priv2, X).
keymatch(priv3, pub3).
An aside...
Prolog does not have functions; you can't say something like Y = f(X)
in Prolog (actually you can, but it doesn't do what you'd expect if you're coming from a C/Java/Python/etc background.
To implement the functionality of Y = f(X)
in Prolog, you would do something like this
f(X, Y) :- Y is X + 1.
and invoke it like this
f(3, Y).
Y would evaluate to the value 4.
精彩评论