FEW EXERCISE ISSUES :p
FEW EXERCISE ISSUES :p
(OP)
Hey friends I am pretty new to Prolog .
But this problem has intrigued me for over a month now.
I do not want to use cut or any non-logical Prolog built-ins.
And I would ask anybody who understand this language , to provide me the detailed logic and how his solution would actually work.
I am interested in learning by understanding from others too instead of just working hard and not asking others.
So here's my doubt.
Consider representing student grades using the following data:
A student name is represented using a Prolog atom like bill.
A student grade is represented using a Prolog int like 87.
A grade for a student is represented using the Prolog structure nameGrade(Name, Grade) where Name is a student name and Grade is a student grade.
A grade-tree is an ordered binary tree which is one-of the following:
[] represents the empty tree.
treeStudent(nameGrade(Name, Grade), SL, SR)
Represents a tree with Name a prolog atom representing a student name, Grade is a Prolog integer representing a grade and SL is a grade-tree containing only names less-than Name (using Prolog's binary relation @<) and SR is a grade-tree containing only names greater-than Name (using Prolog's binary relation @>).
Write Prolog's procedures:
obtainStudentGrade(GivenGradeTree, Name, Grade)
Given a grade-tree GivenGradeTree, matches Grade with the grade stored for name Name in GivenGradeTree. Fails if there is no match for Name in GivenGradeTree. Should use the ordering of the tree to minimize the search.
addStudentGrade(GivenGradeTree, NameGrade, GradeTreeS)
Given a grade-tree GivenGradeTree and a name-grade NameGrade returns GradeTreeS as GivenGradeTree with NameGrade added, respecting the ordering relation among the nodes in the gradetree. Note that if there was a previous node in GivenGradeTree with the same name as in nameGrade, then it is replaced by one containing the same grade as in NameGrade.You can use infix operators @<, @> for comparing arbitrary terms.
Example log:
?- addStudentGrade([], nameGrade(jim, 95), S1),
| addStudentGrade(S1, nameGrade(tom, 90), S2),
| obtainStudentGrade(S2, tom, Grade).
S1 = treeStudent(nameGrade(jim, 95), [], [])
S2 = treeStudent(nameGrade(jim, 95), [], treeStudent(nameGrade(tom, 90), [], []))
Grade = 90 ;
false.
?- addStudentGrade([], nameGrade(tom, 90), S1),
| addStudentGrade(S1, nameGrade(tom, 85), S2),
| obtainStudentGrade(S2, tom, Grade).
S1 = treeStudent(nameGrade(tom, 90), [], [])
S2 = treeStudent(nameGrade(tom, 85), [], [])
Grade = 85 ;
false.
?- addStudentGrade([], nameGrade(tom, 90), S1),
| addStudentGrade(S1, nameGrade(bill, 85), S2),
| obtainStudentGrade(S2, ann, G).
false.
?-
I think the 3rd example fails because there is no grade for ann.
But this problem has intrigued me for over a month now.
I do not want to use cut or any non-logical Prolog built-ins.
And I would ask anybody who understand this language , to provide me the detailed logic and how his solution would actually work.
I am interested in learning by understanding from others too instead of just working hard and not asking others.
So here's my doubt.
Consider representing student grades using the following data:
A student name is represented using a Prolog atom like bill.
A student grade is represented using a Prolog int like 87.
A grade for a student is represented using the Prolog structure nameGrade(Name, Grade) where Name is a student name and Grade is a student grade.
A grade-tree is an ordered binary tree which is one-of the following:
[] represents the empty tree.
treeStudent(nameGrade(Name, Grade), SL, SR)
Represents a tree with Name a prolog atom representing a student name, Grade is a Prolog integer representing a grade and SL is a grade-tree containing only names less-than Name (using Prolog's binary relation @<) and SR is a grade-tree containing only names greater-than Name (using Prolog's binary relation @>).
Write Prolog's procedures:
obtainStudentGrade(GivenGradeTree, Name, Grade)
Given a grade-tree GivenGradeTree, matches Grade with the grade stored for name Name in GivenGradeTree. Fails if there is no match for Name in GivenGradeTree. Should use the ordering of the tree to minimize the search.
addStudentGrade(GivenGradeTree, NameGrade, GradeTreeS)
Given a grade-tree GivenGradeTree and a name-grade NameGrade returns GradeTreeS as GivenGradeTree with NameGrade added, respecting the ordering relation among the nodes in the gradetree. Note that if there was a previous node in GivenGradeTree with the same name as in nameGrade, then it is replaced by one containing the same grade as in NameGrade.You can use infix operators @<, @> for comparing arbitrary terms.
Example log:
?- addStudentGrade([], nameGrade(jim, 95), S1),
| addStudentGrade(S1, nameGrade(tom, 90), S2),
| obtainStudentGrade(S2, tom, Grade).
S1 = treeStudent(nameGrade(jim, 95), [], [])
S2 = treeStudent(nameGrade(jim, 95), [], treeStudent(nameGrade(tom, 90), [], []))
Grade = 90 ;
false.
?- addStudentGrade([], nameGrade(tom, 90), S1),
| addStudentGrade(S1, nameGrade(tom, 85), S2),
| obtainStudentGrade(S2, tom, Grade).
S1 = treeStudent(nameGrade(tom, 90), [], [])
S2 = treeStudent(nameGrade(tom, 85), [], [])
Grade = 85 ;
false.
?- addStudentGrade([], nameGrade(tom, 90), S1),
| addStudentGrade(S1, nameGrade(bill, 85), S2),
| obtainStudentGrade(S2, ann, G).
false.
?-
I think the 3rd example fails because there is no grade for ann.





RE: FEW EXERCISE ISSUES :p
RE: FEW EXERCISE ISSUES :p
nickyboye009@gmail.com