prolog - Create predicates given the last n elements of a list -
example:
?-lastn([1,2,3,4],3,t). t = [2,3,4]
this whay i'm write:
lastn(l,n,r):- length(l,x), x1 x-n, lastt(l, n,r). lastt(l,0,l). lastt(x,[h|t],l):- x2 x-1, lastt(t,x2,l).
i assume question 'predicate last n elements in list' , meant do. it's simple discarding of first element after counting how many elements have discarded, right? not deal improper input @
lastn(l,n,r):- length(l,x), x1 x-n, lastt(l,x1,r). lastt(l,0,l). lastt([h|t],x,l):- x2 x-1, lastt(t,x2,l).
Comments
Post a Comment