What you want is more something like this: search(L):- write('searching for: '),read(E), finde(E). Finde(E) :- find(L,E,Pos), % find succeeds out(L,E,Pos),!. % and prints, cut ensures we don't try next clause finde(E) :- write('Element '),write(E),write(' is not present!'),!.
% called if not found That way you don't need a strange find clause that always succeeds with a large number. Just let it fail.
I think your answer is very efficient! Thanks! – Aleg May 4 at 8:52.
Remember that you're not limited to returning numbers from your find/3 predicate. You can instead return any other atom, like so: find(, _, notfound). Find(H|_, H, 1) :-!
Find(_|T, H, Pos) :- find(T, H, Found), ( integer(Found),! , Pos is Found+1 );( Pos = notfound ). However, this style is a little more awkward to write with, though your return value is much more descriptive.
Edited to fix an error.
You might also enable tail recursion, e.g. Using find/4 that has the possibility of failing and find/3 that returns a descriptive or zero value in a fall-through case on missing items. – hardmath May 4 at 13:39 I don't understand... in my swi-prolog i've this output: 15? - find(2,3,4,2,Pos).
Pos = 1. 16? - find(2,3,4,3,Pos).
ERROR: is/2: Arguments are not sufficiently instantiated 17? - find(2,3,4,4,Pos). ERROR: is/2: Arguments are not sufficiently instantiated 18?
- find(2,3,4,5,Pos). Pos = notfound. – Aleg May 4 at 14:59 @hardmath, @Aleg, Bah, that's what I get for writing it up without testing.
I've fixed it now, though I now see that it's not the best solution. – Raceimaztion May 4 at 16:37.
Search(L):- write('searching for: '), read(E), find(L,E,Pos), fail. Search(_). Find(L,E,0) :- \+(append(_,E|_,L)),!.
Find(L,E,Pos) :- append(L0,E|_,L), length(L0,Len0), Pos is Len + 1. Find(_,_,-1). Out(E,0) :- writef('element %t is not present.',E),!.
Out(E,-1) :- writef('element %t is no longer present. ',E),!. Out(E,Pos) :- writef('element %t is in position %t.
',E,Pos),!.
There seems to be a typo in the second rule/clause for find/3. The variable Len has not been bound when you compute Pos, so presumably you meant Len0 here (bound in the previous subgoal). – hardmath May 4 at 13:44.
% NewLine was missing. Out(E,0) :- writef('element %t is not present. \n',E),!.
Out(E,-1) :- writef('element %t is no longer present. \n',E),!. Out(E,Pos) :- writef('element %t is in position %t.
\n',E,Pos),!.
Instead of posting a "new" answer, you probably meant to edit your previous one. Just below the left side of your answer text you will find an "edit" function. You can also delete the extra answer once your original answer is updated.
– hardmath May 4 at 11:44.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.