Skip to content

Instantly share code, notes, and snippets.

@ejamesc
Created September 13, 2011 15:38
Show Gist options
  • Save ejamesc/1214138 to your computer and use it in GitHub Desktop.
Save ejamesc/1214138 to your computer and use it in GitHub Desktop.
Operand matching
/* End cases, where str is atom or number */
count(Str, Op, 0) :- atom(Str).
count(Str, Op, 0) :- number(Str).
/* Program */
count(Str, Op, R) :- Str =..[A,B,C],
test(A, Op, R1),
count(B, Op, R2),
count(C, Op, R3),
R is R1 + R2 + R3.
count(Str, Op, R) :- Str =..[A,B],
count(A, Op, R1),
count(B, Op, R2),
R is R1 + R2.
/* Test returns 1 if operand matches; else 0 */
test(Str, Op, R) :- Str = Op, atom(Str), R is 1; R is 0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment