Courses/CS 460/Fall 2005/Homework/Nghia Phan/Oct 1

From CSWiki

Jump to: navigation, search

%Function to query record by feature
%@return list of value of feature
%@return all record if P_feature = '*'
declare fun {QueryByFeature P_record P_feature P_returnList}
	local T in
		if (P_feature == '*') then
			P_returnList = P_record
		else
			{CondSelect P_record P_feature '#' T}
			P_returnList = T
		end
	end
end


%Function to find length of the list
declare fun {ListLength P_list P_result}
	if (P_list \= nil) then
		case P_list of H|T then
			local X in
				P_result = 1 + {ListLength T X}
			end
		end
	else
		P_result=0
	end
end


%Function to find the factorial
declare fun {Factorial P_value P_result}
	if (P_value == 0) then
		P_result = 1
	else
		local X in
			P_result = P_value*{Factorial P_value-1 X}
		end
	end
end


local V_table V_first V_last V_address V_zip V_state V_result1 V_result2 V_result3 V_result4 in
	V_first = ['Nghia' 'Duc' 'John']
	V_last = ['Phan' 'Trinh' 'Dole']
	V_address = ['123 ABC' '456 DEF' '789 GHI']
	V_zip = [12345 67891 23456]
	V_state = ['CA' 'CA' 'CA']

	V_table = tree(first_name:V_first last_name:V_last address:V_address zip:V_zip state:V_state)

	
	% 1) Query by first name
	{Browse {QueryByFeature V_table 'first_name' V_result1}}

	% 2) Query all columns
	{Browse {QueryByFeature V_table '*' V_result2}}

	% 3) Find the factorial
	{Browse {Factorial 10 V_result3}}

	% 4) Find length of V_zip list
	{Browse {ListLength V_zip V_result4}}

end