Let's say I want to have some rule for comparison similar to
isin(0,_).
isin(N,List) :- member(N,List), write(N), N1 is N-1, isin(N1,List).
but the List will contain alphabet symbols (e.g. [a,b,d,e,h]). How can I send the next element to the iteration? (so N is a symbol, not a number). And if it's not possible, how can I ma开发者_Python百科ke something similar?
Thanks in advance!
@julkiewicz is almost there:
isin(a, _).
isin(Char, List) :-
member(Char, List),
char_code(Char, Code),
write(Char),
Code1 is Code-1,
char_code(Char1, Code1),
isin(Char1, List).
Note that the predicate will always succeed on a
, just like your original version always succeeds on 0
. You can prevent that by changing the base clause to something like
isin(Char, _) :-
char_code(a, A),
Char is A-1.
(But this is really an ugly hack.)
NOTE: This works on strings, not on symbols as requested by the OP.
Well it seems like characters are interpreted as number lists really. So this works:
?- X = "a".
X = [97].
?- X is "a".
X = 97.
?- X is "a" + 1.
X = 98.
So this is what I'd propose:
isin("a", _).
isin(N, List) :- member(N, List), N1 is N - 1, isin([N1], List).
Haven't written anything in this language for a long time though.
精彩评论