Prolog program about lists -
i new programmer in prolog , tried program says: make predicate penta(x)
, x
list , returns true
when in x
there 5 consecutive elements : first element sum between first , second. third element difference between 5th , 4th example: x = [ ... 5, 7, 12, 18, 30, ... ]
.
so did this:
penta(x) :- \+length(x,0), //here verify if lists contains less 5 elements gives false. \+length(x,1), \+length(x,2), \+length(x,3), \+length(x,4), (a, b, c, d, e | x), c + b, c d - e, penta(x).
this not compile doesn't work yet. tell me what's wrong if like.
thank much.
in prolog, write predicates not functions. predicate defines rule succeed or fail on sets of instantiated variables. want predicate, penta(x)
succeeds if x
list contains 5 consecutive elements meeting criteria.
start top. either 5 consecutive elements meet criteria @ head of list, or later in list.
% succeed if first 5 elements meets criteria penta([a, b, c, d, e |_]) :- ... % goes here succeed? % succeeds if rest of list succeeds, without first element penta([_|t]) :- penta(t).
i think these 2 rules need. else query doesn't match these fail default, want. don't need check length 0 through 4 length cases. cases fail above predicates.
you'll notice that, depending upon how implement above, might succeed several times. is, may find more 1 solution. need decide if that's want, or if want stop after 1 solution. i'll leave further exercise.
Comments
Post a Comment