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!

Help pls

Status
Not open for further replies.

mzam

Programmer
Joined
Jan 8, 2009
Messages
1
Location
MT
I am trying to write a clause for the following list handling rule : bubble(N,List,NewList) which binds NewList with the List having its Nth element brought to the front. e.g.bubble(3,[a,b,c,d,e],Ans). binds Ans to [c,a,b,d,e]
Here is my code. I don't know why it is not working. Can somebody helpppp me plsssss?? 10x

Code:
 del(1,[H1|T],T).

del(N,[H|T],[H|Rest]):- NewN is N - 1, 
                        
                        del(NewN,T,Rest).

bubble(N,[H|T],[H1|New]):-del(N,[H|T],New).

bubble(N,[_|T],[H|Rest]):- bubble(N,T,Rest).
 
You have almost the solution !
Just look at the clause (wich has a very bad name : del, it should be get !): del(1,[H1|T],T).
You loose the information that it's the element you must remember.
You should have del(1,[H,| T], H, T).
And the work is done !

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top