View workfile
Simple queries:
instructor(X, cse340)
instructor(instructor(john, johnson), cse340).
Side note: Looks really strange, but in the last line of the example the 2 instructor mentions mean 2 different things.
The first instructor is the Rule
The second instructor is actually the struct
Scope of a variable only exists in the fact, rule or query that contains the variable.
ate(Person, grilledcheese) : -
ate(Person, cheese),
ate(Person, bread).
Person
varible is instantiated 3 diferent times.
Person is different from this example:
ate2(Person, grilledcheese) :-
ate(Person, cheese1),
ate(Person, cheese2).
This is a new Person
Prolog weaknes is in the ability to manipulate numbers.
<variable> is <artithmetic operation>
Example:
x is 7+3
x is 10
x is 7+3
x is 5-2
x is 3*2
x is 5/2
5//2 = 2
72 mod 12 = 3
x is 2**3 = 8
Convert F to C Rule
/*Create a rule to convert F to C*/
avg_temp(phx, 100).
avg_temp(sf, 68).
/* C = (F - 32) * 5/9 */
avg_temp_cels(Location, C_Temp) :-
avg_temp(Location, F_Temp),
C_Temp is (F_Temp - 32) * 5//9.
Queries:
avg_temp_cels(phx, Cels).
avg_temp_cels(sf, Cels).
- Text editor & compiler all in one
- Syntax highlighting
- Query