% Read and echo names; count them; check for uniqueness
const N: int := 50	% maximum names

type namelist:
	array 1 .. N of string


%
% We can define a linear search procedure separately like so.
%
function found(list: namelist, name: string, n: int): boolean
	var done: boolean := false
	var i: int
	i := 0;
	loop  % is name on the list already?
		exit when done or i = n
		i := i + 1
		done :=  (name = list(i))
	end loop
	result done
end found
.bp
%
% Now we can do things a little more cleanly in the main code.
%

var i, count, dups : int := 0
var names : namelist

put "Please type a series of names."
put "Tell me 'quit' at the end"

loop
	put "Next name? " ..
	var person : string
	get person
	exit when person = "quit" or i > N

%	OK, so a name has been entered.  Is it already on the list?

	if found(names, person, count) then
		put "You know, there's already a ",
			person, " on this list,"
		put "so I won't say hello to you again."
		dups := dups + 1
	else
		count := count + 1
		names(count) := person
		put "Hi, ", person, ".  I've added you to ",
		     "my list at position ", count
	end if
end loop
put "I greeted ", count, " people," 
put "and there were ", dups, " duplicate names entered."
