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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

always false

Status
Not open for further replies.

jonison

Programmer
Joined
Jul 12, 2009
Messages
4
Location
GB
Dear Everyone:

I am a new starter,and make a programme: here are the code:

matchsort([H|T],YList,XList,[N|M]) :-
nth1(Index,YList,H),
write('******'),nl,write(H),nl,
nth1(Index,XList,N),
write(N),nl,write('------'),nl,
matchsort(T,YList,XList,M).

matchsort([],[],[],[]).

run with

"matchsort([17,12,8,5,4],[4,8,12,5,17],[3,9,6,4,9],List)."

the results are:
******
17
9
------
******
12
6
------
******
8
9
------
******
5
4
------
******
4
3
------
false.

No 'List' value....and with false...

With this false problem, I made some changes, the code are changed as:

matchsort([H|T],YList,XList,[N|M]) :-
nth1(Index,YList,H),
write('******'),nl,write(H),nl,
nth1(Index,XList,N),
write(N),nl,write('------'),nl,
matchsort(T,YList,XList,M).

matchsort([],YList,XList,[]).

run this with

"matchsort([17,12,8,5,4],[4,8,12,5,17],[3,9,6,4,9],List)."
get the results:

******
17
9
------
******
12
6
------
******
8
9
------
******
5
4
------
******
4
3
------
*********[9, 6, 9, 4, 3]
true .

here I get the value of List and true, I donot know why I can get this result,especially for the List value, could you give me explainations about these?

Many Thanks for your patience for New starter like me.
 
Code:
matchsort([H|T],YList,XList,[N|M]) :-
    nth1(Index,YList,H),
    write('******'),nl,write(H),nl,
    nth1(Index,XList,N),
    write(N),nl,write('------'),nl,
    matchsort(T,YList,XList,M).
        
matchsort([],[],[],[]).

matchsort([],[],[],[]) never happens because YList and XList are always the same, but matchsort([],YList,XList,[]) is correct, you should write matchsort([],_YList,_XList,[]) with '_' in front of XList and YList because these args are not used.


Now, explanation for the result :

Code:
matchsort([H|T],YList,XList,[N|M]) :-
    nth1(Index,YList,H),
    write('******'),nl,write(H),nl,
    nth1(Index,XList,N),
    write(N),nl,write('------'),nl,
    matchsort(T,YList,XList,M).

with matchsort([17,12,8,5,4],[4,8,12,5,17],[3,9,6,4,9],List).

At the first call
matchsort([17 | T], [4,8,12,5,17],[3,9,6,4,9], [9 | M])).
because
nth1(Index, [4,8,12,5,17], ,17), --> Index = 5
write('******'),nl,write(H),nl,
nth1(5 , [3,9,6,4,9],N), ==> N = 9
write('******'),nl,write(H),nl,
matchsort([12,8,5,4]],[4,8,12,5,17],[3,9,6,4,9],,M).

and so on ...


To explain the list [9,6,9,4,3]


 
Many thanks for your reply, yes, this is really helpful.
many many thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top