Logo 
Search:

Artificial Intelligence Forum

Ask Question   UnAnswered
Home » Forum » Artificial Intelligence       RSS Feeds

How to find grades with the below data using prolog?

  Asked By: Sukesh    Date: Oct 23    Category: Artificial Intelligence    Views: 1604
  

How to represent student grades using the following data representation:?

A student name is represented using a Prolog atom like `jim`.
A student grade is represented using a Prolog int like 95.
A grade for a student is represented using the Prolog structure `name_grade(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.
`tree(name_grade(Name, Grade), TL, TR)`::

Represents a tree with `Name` a Prolog atom representing a
student name, `Grade` is a Prolog integer representing a grade and `TL` is a grade-tree containing only names less-than `Name` (using Prolog's binary relation `@<`) and `TR` is a grade-tree containing only names greater-than `Name` (using Prolog's binary relation `@>`).

I am thinking to Write below Prolog's procedures:

`get_grade(GradeTree, Name, Grade)`::
Given a grade-tree `GradeTree`, matches `Grade` with the grade stored for name `Name` in `GradeTree`. Fails if there is no match for `Name` in `GradeTree`. Should use the ordering of the tree to minimize the search.

`add_grade(GradeTree, NameGrade, GradeTreeZ)`::
Given a grade-tree `GradeTree` and a name-grade `NameGrade` returns `GradeTreeZ` as `GradeTree` with `NameGrade` added, respecting the ordering relation among the nodes in the grade-tree. Note that if there was a previous node in `GradeTree` with the same name as in
`NameGrade`, then it is replaced by one containing the same grade as in `NameGrade`.


Example log:

?- add_grade([], name_grade(jim, 95), T1),
| add_grade(T1, name_grade(tom, 90), T2),
| get_grade(T2, tom, Grade).

T1 = tree(name_grade(jim, 95), [], [])
T2 = tree(name_grade(jim, 95), [], tree(name_grade(tom, 90), [], []))
Grade = 90 ;
false.

?- add_grade([], name_grade(tom, 90), T1),
| add_grade(T1, name_grade(tom, 85), T2),
| get_grade(T2, tom, Grade).

T1 = tree(name_grade(tom, 90), [], [])
T2 = tree(name_grade(tom, 85), [], [])
Grade = 85 ;
false.

?- add_grade([], name_grade(tom, 90), T1),
| add_grade(T1, name_grade(bill, 85), T2),
| get_grade(T2, ann, G).
false.

?-

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on How to find grades with the below data using prolog? Or get search suggestion and latest updates.




Tagged: