 Search:

# How to find grades with the below data using prolog?

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

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.

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:

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.

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:

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

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