프롤로그에서 bubblesort 알고리즘을 연구 중입니다.프롤로그 Bubblesort Uninstantiated 인수
bubblesort([],SortedList).
bubblesort([X,Y|List], [Y,X|SortedList]) :- X>Y, bubblesort(List, SortedList).
bubblesort([N|List], [N|SortedList]) :- bubblesort(List, SortedList).
bubble(List, SortedList) :- bubblesort(List, Sorted),
(check(Sorted)
-> SortedList=Sorted
; bubble(Sorted, SortedList)).
check([]).
check([X,Y|SortedList]) :- X<Y, check(SortedList).
나는이 호출이 함수를 테스트 :
bubble([2, 11, -4, 6, 8, 10101, 61, -98, 55, 79, -32, -67, 54, 45, 19, 707, 43, -99, 32, 20], List).
가 정렬 처음, 잘 작동 여기에 지금까지 가지고있는 것입니다. 이 첫 번째의 결과를 정렬 할 때 그러나,이 오류가 발생합니다
이Call: (21) bubblesort([32|_G5214], _G5271) ? creep
Call: (22) 32>_G5273 ? creep
오류 메시지가 발생합니다
프로그램을 추적猭ERROR: >/2: Arguments are not sufficiently instantiated
Exception: (21) bubblesort([32|_G3208], _G3265) ? creep
Exception: (19) bubblesort([707, -99, 20, 32|_G3208], _G3256) ? creep
Exception: (17) bubblesort([54, 19, 43, 707, -99, 20, 32|_G3208], _G3247) ? creep
Exception: (15) bubblesort([79, -67, 45, 54, 19, 43, 707, -99|...], _G3238) ? creep
Exception: (14) bubblesort([55, -32, 79, -67, 45, 54, 19, 43|...], _G3232) ? creep
Exception: (13) bubblesort([10101, -98, 55, -32, 79, -67, 45, 54|...], _G3226) ? creep
Exception: (10) bubblesort([11, 6, 8, 61, 10101, -98, 55, -32|...], _G3214) ? creep
Exception: (9) bubblesort([2, -4, 11, 6, 8, 61, 10101, -98|...], _G3309) ? creep
이 호출이 될 때 보여줍니다. 나는이 목록이 어떻게 이루어 졌는지 이해할 수 없다. 왜냐하면 내 목록에 근거가없는 가치가 있기로되어 있지 않기 때문이다. 내가 도대체 뭘 잘못하고있는 겁니까?
편집 : 나는 약간의 코드를 수정 :
bubblesort([],[]).
bubblesort([X,Y|List], [Y,X|SortedList]) :- X>Y, bubblesort(List, SortedList).
bubblesort([N|List], [N|SortedList]) :- bubblesort(List, SortedList).
bubble(List, SortedList) :- bubblesort(List, Sorted),
(check(Sorted)
-> SortedList=Sorted
; bubble(Sorted, SortedList)).
check([]).
check([X,Y|SortedList]) :- X<Y, check(SortedList).
I가 다음과 같은 출력 : 목록 = [-4, 2, -98, 6, -32, 8, -67, 11, 45 | ...]
이 출력은 잘못된 것이며, 이것이 왜 출력인지 알 수 있습니다. 수표는 처음 두 개를 테스트합니다. 다음 두 개로 이동하는 것 등 바람직하지 않습니다. 나는 수 표를 위해 고치고있다.
편집 2 : 이제 의도 한대로
check([]).
check([_]).
check([X,Y|SortedList]) :- X<Y, check([Y|SortedList]).
코드가 작동합니다
나는 체크 방법을 수정.