Calculation of graduation grade in Prolog -


i have error in prolog program try calculate cgpa in winprolog , code follows:

grade(x).  start:-     (   write('please enter ur cgpa'), read(x), x > 0 , x < 2,         write('ur cgpa poor'), write(x)     ;   x >= 2, x < 3,           write('ur cgpa good')     ;   x >= 3,          write('ur cgpa excellent')     ).   // comments  //  %cgpa(x,good):-grade(x) , x>=2 , x<3.   // %cgpa(x,good):-grade(x) , x>=3 , write("your grade excelent").  // there missing, please me 

as @lurker noted, logical , (,) , logical or (;) operators different precedences. expression doesn't bind way think does. how expression

a , b ; c , d 

bind? it...

  • and having higher precendence or: (a,b) ; (c,d)
  • or having higher precedence and: a , (b;c) , d
  • equal precedence, left-associative: ( ( (a,b) ; c ) , d )
  • equal precedence, right-associative: ( , ( b ; (c,d)))

if you're not sure, make binding clear parentheses (but don't overdo it).

further, 1 thing prolog alternatives better specified individual clauses of predicate rather using logical or (;) operator. this

evaluate_grade( x , poor      ) :- x >= 0.0 , x < 2.0 . evaluate_grade( x ,      ) :- x >= 2.0 , x < 3.0 . evaluate_grade( x , excellent ) :- x >= 3.0 . 

is easier debug, extend, modify , understand your

( x>0 , x<2 ,   write('ur cgpa poor') , write(x) ;   x>=2 , x<3,     write('ur cgpa good') ;    x>=3 ,    write('ur cgpa excellent') ) 

taking approach helps lead single responsibility principle — fancy way of saying things should 1 simple thing.

so, might try decomposing problem smaller pieces each single thing. this:

start:-   read_grade(x) ,   evaluate_grade(x,r),   write('your cgpa ') ,   write(r) ,   nl   .  read_grade(x) :-   write('please enter cgpa: ') ,   read(x),   number(x)   .  evaluate_grade( x , poor      ) :- x >= 0.0 , x < 2.0 . evaluate_grade( x ,      ) :- x >= 2.0 , x < 3.0 . evaluate_grade( x , excellent ) :- x >= 3.0 . 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -