Prolog "filter" a list -


i trying solve exercise school in asked make function called filter(+lin,+min,+max,-lout) supposed modify lin(list) lout(list) has numbers within limits set min , max.

ex. ?- filter([1,2,3,4,5,6,7,8],3,6,l). l = [3, 4, 5, 6]. 

i tried lot of things, searched here answers there in logic of prolog can't quite get..

what have done far.i using chop function append head list each time if head inside limits.

chop([h|t],h,t).  filter([],min,max,[]). filter([h|t],min,max,lout):-(h>min,h<max),     chop(lout,h,list),filter(t,min,max,lout). filter([h|t],min,max,lout):-(h<min;h>max),     filter(t,min,max,lout). 

if can answer question , don't mind bit of explaining appreciate it!

thank time.

first must create rule handles empty lists:

filter([], _, _, []). 

then rule handle elements between min , max:

filter([x|lin], min, max, [x|lout]) :- x >= min,                                        x =< max, !,                                        filter(lin, min, max, lout). 

finally need rule ignores elements out of bounds:

filter([_|lin], min, max, lout) :- filter(lin, min, max, lout). 

that’s all!


Comments

Popular posts from this blog

ios - UITEXTFIELD InputView Uipicker not working in swift -

php - Tinymce Editor not coming up on Croogo on server -