我正在嘗試在SML中編寫一個函數,該函數需要一個ints列表,並且會輸出一個有序的ints對列表。有序對第一個int是輸入列表中發生的int,有序對中的第二個int是它在輸入列表中發生的次數。此外,返回的列表應該按照有序對中的第一個int升序排列。SML - 在列表中查找出現以形成有序對
例如輸入列表[1, 1, 1, 2, 3, 3, 5]
將輸出爲[(1,3), (2, 1), (3, 2), (5, 1)]
。
到目前爲止,我有一個使用foldl
修訂,因爲原來的職位代碼的功能。
fun turnIntoPairs l = foldl (fn (e, a) => if List.exists (fn (x, _) => x = e) a then x + 1 else a @ [(e, 1)]) [] l;
我無法更新在那裏我找到了有序對已在列表中的列表 - 我想補充一個在有序對第二個INT被發現,而它仍然在列表中。
任何幫助將不勝感激!
C:\Program Files (x86)\SMLNJ\\bin\.run\run.x86-win32.exe: Fatal error -- Uncaught exception Error with 0
raised at ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
[autoloading done]
C:\Users\Localadmin\Desktop\CS 671\Program 3\commonFactors.sml:1.87 Error: unbound variable or constructor: x
C:\Users\Localadmin\Desktop\CS 671\Program 3\commonFactors.sml:1.44-1.110 Error: types of if branches do not agree [literal]
then branch: int
else branch: (''Z * int) list
in expression:
if (List.exists (fn <pat> => <exp>)) a
then <errorvar> + 1
else a @ (e,1) :: nil
[Finished in 0.5s with exit code 1]
看看你的錯誤。它抱怨'find'和'x'不存在。通過'find'我認爲你的意思是'List.find',儘管那個類型的簽名不適合你試圖給它的參數。 – Tayacan
此外,同樣的問題正在回答[here](http:// stackoverflow。com/questions/17372083/turn-a-list-with-common-items-in-to-a-list-of-ordered-pairs) – Tayacan
我看到那篇文章,當我發佈這篇文章時,我正在寫這個問題。我用這些信息開始寫這個功能,我現在遇到了麻煩。 –