Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Sorting

Status
Not open for further replies.

MinasTirith

Programmer
Joined
Mar 8, 2009
Messages
2
Location
GB
?Hey guys,

Im working on a family structure file tutorial and I am trying to sort a list using a bubble sort in descending order of salary. I have to firstly find all the person structures in the file whose salary are between 15000 & 35000 inclusive. Then sort them by salary and print them out.

The family structure I am using is:

Code:
family(
	person(alan,sartre,date(5,may,1962),works(mopper-ups,15000)),
	person(caitlin,sartre,date(11,october,1964),works(limolux,100000)),
	[	person(yvonne,sartre,date(12,november,1991),unemployed),
		person(alan_jr,sartre,date(4,august,1989),unemployed)
	]
My code is:

Code:
q18_get_list(List):-
	findall(Person, passes_test(Person), List).

passes_test(Person):-
	Person = person(Name, Surname, Dob, works(Co, Salary),
	exists(Person),
	Salary >= 15000,
	Salary =< 35000.
Syntax Error here!!!

bubblesort(List, Sorted):-
	swap(List, List1),!,
	bubblesort(List1, Sorted).
	bubblesort(Sorted, Sorted).

swap([X,Y|Rest], [Y,X|Rest]):-
	X<Y,
swap([Z|Rest], [Z,Rest1]).


print_list:-
	nl,nl,
	mylist(List),
	write_out(List).

write_out([Head|Tail]):-
	write(Head),
	nl,
	write_out(Tail).

write_out([]):-
	nl,nl.
I keep getting a syntax error just below the passes_test predicate. Could someone point me in the right direction as to where I am going wrong? Im really confused on this one.

Thanks
MT
 
Ok I managed to sort the syntax error, didnt close my brackets of properly, but now I am getting an Error 20 Predicate not defined:

! Error 20 : Predicate Not Defined
! Goal : bubblesort(_37214,_37216) :- swap(_37214,_37232),!,bubblesort(_37232,_37216)

Not sure what the numbers mean or where exactly the error is, I am pretty new to prolog.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top