Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here





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 ;

?- 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 ;

?- addStudentGrade([], nameGrade(tom, 90), S1),
| addStudentGrade(S1, nameGrade(bill, 85), S2),
| obtainStudentGrade(S2, ann, G).

I think the 3rd example fails because there is no grade for ann.


Guys I solved it. Please delete the post :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close