Content deleted Content added
→Correct results for Prolog numeric grade example: Reset without password or app service Finnish or all Data password reset Gemini app privacy service or all take number delete formated Tags: Reverted Mobile edit Mobile web edit |
Ohnoitsjamie (talk | contribs) m Reverted edit by 2404:1C40:BC:28F8:1:0:D919:B5C3 (talk) to last version by Mindmatrix |
||
Line 57:
== Correct results for Prolog numeric grade example ==
The displayed results for Prolog's numeric grade example is not what the user would expect. The article says the results will display: X = 'A'. The expected results should display: X = 4. [[User:Timhowardriley|
:The example in its current form has two relations, which can be queried in isolation, or which can be queried in combination (like a join in a relational database). For example:
<syntaxhighlight lang="prolog">
numeric_grade('A', 4).
numeric_grade('B', 3).
numeric_grade('C', 2).
Line 69 ⟶ 71:
X = 'A',
Y = 4
</syntaxhighlight>
:Alternatively, the query could be used to define a new relation, which gives the grade of a student as a number:
<syntaxhighlight lang="prolog">
numeric_grade('A', 4).
numeric_grade('B', 3).
numeric_grade('C', 2).
numeric_grade('D', 1).
numeric_grade('F', 0).
numeric_grade(X, -1) :- not X = 'A', not X = 'B', not X = 'C', not X = 'D', not X = 'F'.
grade_as_number(Student, Number) :-
Line 81 ⟶ 85:
?- grade_as_number('The Student', Number).
Number = 4
</syntaxhighlight> [[User:Robert Kowalski|Robert Kowalski]] ([[User talk:
Followup: It would be nice to learn Prolog's syntax for forming functions. Could this example be converted to a function? If so, then how would the the driver program
▲Followup: It would be nice to learn Prolog's syntax for forming functions. Could this example be converted to a function? If so, then how would the the driver program execuAugust 2023 (UTC)
<syntaxhighlight lang="prolog">
▲: Here is the example in [[Ciao (programming language) | Ciao]] Prolog's functional syntax=
numeric_grade('A') := 4.
numeric_grade('B') := 3.
numeric_grade('C') := 2.
numeric_grade('D') := 1.
numeric_grade('F') := 0.
numeric_grade(X) := -1 :- X \= 'A', X \= 'B', X \= 'C', X \= 'D', X \= 'F'.
grade_as_number(Student) := numeric_grade(grade(Student)).
grade('The Student') := 'A'.
Line 96 ⟶ 108:
Number = -1,
Student = bob
</syntaxhighlight>
:Just for fun, the query asks for all input-output pairs that satisfy the functional relationship "grade_as_number". Good for debugging.
|