Counts a letter in a text- back to content

	
		Let's see how many times is a in the file called mary.txtcontaining:
			Mary has a little lamb
			   Little lamb
		?- start.
		Text is in file= 'mary.txt'.
		What letter do you want to count= a.
		This letter occurs 5 times.

start:- write('Text is in file= '),read(F), write('What letter do you want to count='),read(Let), see(F),process(Let,0),seen. process(Let,N):- get0(Char), process1(Char,Let,N). /* The end of the file is -1 */ process1(-1,Let,N):- write('This letter occurs '),write(N), write(' times.'),!. process1(Char,Let,K):- name(Let,[Char]),K1 is K+1,process(Let,K1). process1(Char,Let,K):- process(Let,K).
/* Another solution (without recursion!). Be careful the counter must be put back to 0 at the end! I did not do this!*/ counter(0). start:- write('Text is in file= '),read(F), write('What letter do you want to count= '),get0(MyChar), see(F),process(MyChar),seen, counter(Final), write('This letter occurs '),write(Final),write(' times'),nl. process(MyChar):- repeat,get0(I),(dothis(I,MyChar) ;true), I = -1. dothis(I,MyChar):- I=MyChar,counter(K), R is K + 1, retractall(counter(_)), assert(counter(R)).