2011-03-15 61 views
1

我試圖糾正一個簡單的函數來進行排序使用選擇排序列表類型的錯誤,即時通訊代碼如下:關於無限類型

fun slctsrt [] = [] 
| slctsrt (x::xs) = slctsrt2 (xs, x, []); 
fun slctsrt2 ([], min, []) = min 
| slctsrt2 ([], min, y::ys) = min :: slctsrt2 (ys, y, []) 
| slctsrt2 (x::xs, min, ys) = if x<min then slctsrt2 (xs, x, min::ys) 
          else slctsrt2 (xs,min,x::ys); 

...並返回以下錯誤:

"lctsrt.txt", line 7, characters 32-35: 
! | slctsrt2 ([], min, y::ys) = min :: slctsrt2 (ys, y, []) 
!         ^^^ 
! Type clash: expression of type 
! 'a list 
! cannot have type 
! 'a 
! because of circularity 

回答

2

函數slctsrt2應返回一個列表。但是,第一個條款:

fun slctsrt2 ([], min, []) = min 

返回a'a。

它應該是:

fun slctsrt2 ([], min, []) = min :: [] 
+1

或'樂趣slctsrt2([],分鐘,[])= [分鐘]' – sepp2k 2011-03-16 00:27:07