0

I am teaching myself Prolog (using SWI on linux), and I am trying to learn basic arithmetic. Take a look at the example program below.

When I attempt to ask any of the following thee questions Prolog tells me the procedure total1/2 is undefined.

Question Statements:

total1('prawn crackers', COST).
total2('prawn crackers', 'fried rice',COST).
total3('spring roll', 'chow mien','boiled rice',COST).

Prolog Program:

dish('prawn crackers', 1).
dish('boiled rice',1).
dish('fried rice', 2).
dish('chow mein',3).
dish('chop suey',4).
dish('spring roll',2).


total1(X,T) :- dish(X,A) T is A.
total2(X,Y,T) :-  dish(X,A), dish(Y,B) T is A + B.
total3(X,Y,Z,T) :- dish(X,A), dish(Y,B), dish(Z,C) T is A + B + c.
false
  • 10,264
  • 13
  • 101
  • 209
Andrew S
  • 2,847
  • 3
  • 33
  • 50
  • 3
    If you fix your typographical errors, it should work: (1) `chow mien` => `chow mein`, (2) `dish(X,A) T` => `dish(X,A), T` (similarly in two other places), and (3) `A + B + c` => `A + B + C`. – lurker Apr 25 '14 at 02:23
  • Thank you, this is a symptom of a mild dyslexic burning the candle at both ends too much. Good to know I did not completely get the wrong end of the stick. – Andrew S Apr 25 '14 at 02:35
  • 3
    As an exercise, you should consider making this work with a list instead: for example, `total(['prawn crackers', 'fried rice']`, COST).` would yield, `COST = 3`. :) – lurker Apr 25 '14 at 02:42
  • 2
    Also, you can simplify the rule for `total1/2` to `total1(X,A) :- dish(X, A)`. – Daniel Lyons Apr 25 '14 at 04:15
  • Possible duplicate of [DRY arithmetic expression evaluation in Prolog](http://stackoverflow.com/questions/23854857/dry-arithmetic-expression-evaluation-in-prolog) –  Nov 11 '16 at 22:11
  • @j4nbur53 That question was asked after this one was posted. – Andrew S Nov 13 '16 at 01:44

0 Answers0