我怎樣才能在列表範例中得到最頻繁的值:哈斯克爾 - 最頻值
[1,3,4,5,6,6] -> output 6
[1,3,1,5] -> output 1
我試着通過我自己的函數來得到它,但我不能做到這你們能幫助我嗎?
我的代碼:
del x [] = []
del x (y:ys) = if x /= y
then y:del x y
else del x ys
obj x []= []
obj x (y:ys) = if x== y then y:obj x y else(obj x ys)
tam [] = 0
tam (x:y) = 1+tam y
fun (n1:[]) (n:[]) [] =n1
fun (n1:[]) (n:[]) (x:s) =if (tam(obj x (x:s)))>n then fun (x:[]) ((tam(obj x (x:s))):[]) (del x (x:s)) else(fun (n1:[]) (n:[]) (del x (x:s)))
rep (x:s) = fun (x:[]) ((tam(obj x (x:s))):[]) (del x (x:s))
請發表您已經嘗試了什麼。 – luqui
這是爲什麼標記遞歸? –
我想用遞歸來實現 – Urah