can't make this prolog program work, prolog rules, 'or' operator, prolog arithmetics -


i'm new prolog , i'm stuck exercise.

i have prolog database called "languages.pl" , i'm working on file called "program.pl", have rules.

a language have name , year of creation, example:

cobol, 1960 pascal, 1971 c, 1971 

and languages have predecessors, like:

scheme, lisp ------ lisp predecessor os scheme.

so, want find language lp precedes language l with, @ least, decade of years between creations, like:

?- ling_precedes_decade(lp, l). lp = ’c++’, l = ’rust’ . 

i tought of like:

ling_precedes_decade(lp, l) :-     language(lp, x),     language(l, y),     ((x-y) > 9); ((x-y) < -9). 

but doesn't work.

there couple of things mention respect question posting.

you listed example facts as:

cobol, 1960 pascal, 1971 c, 1971 

since didn't show these actual prolog facts, can speculate how asserted them. correct way, in order cobol atom, use quotes, since in prolog initial capital letter otherwise make cobol variable.

language('cobol', 1960). language('pascal', 1971). language('c', 1971). 

then in predicate, not being mindful of precedence rules , versus ;. ; lower precedence ,. predicate behavior if grouped follows:

lang_precedes_decade(lp, l) :-     ( language(lp, x),       language(l, y),       ((x-y) > 9) )     ; ((x-y) < -9). 

you need group "or" appropriately:

lang_precedes_decade(lp, l) :-     language(lp, x),     language(l, y),     ( ((x-y) > 9); ((x-y) < -9) ). 

then if query, you'll this:

| ?- lang_precedes_decade(lp, l).  l = 'pascal' lp = 'cobol' ? ;  l = 'c' lp = 'cobol' ? ;  l = 'cobol' lp = 'pascal' ? ;  l = 'cobol' lp = 'c' ? ;  no 

notice redundant results due symmetry of checking against facts. since language(lp, x), language(l, y) capture symmetry (you'll solutions lp , l swapped), don't need check < -9:

lang_precedes_decade(lp, l) :-     language(lp, yearlp),     language(l, yearl),     (yearl-yearlp) > 9.    % lp precedes l 

this result in:

| ?- lang_precedes_decade(lp, l).  l = 'pascal' lp = 'cobol' ?  l = 'c' lp = 'cobol'  no | ?-  

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -