我正在嘗試創建一個替換函式,下面的代碼在它后面的第一行非注釋代碼中創建了帖子標題中描述的錯誤。我不知道為什么會這樣,所以任何幫助將不勝感激。
replace [] t r n = []
replace [] _ _ _ = []
replace xs _ _ 0 = xs
replace (x:xs) t r n
| x == t = r:(replace [xs] t r (n-1))
| otherwise x (replace [xs] t r n)
uj5u.com熱心網友回復:
這里有一些問題:定義的所有部分都應該從同一列開始,所以你應該在第一行之后取消縮進。此外,您需要=在otherwise. xs是一個串列,所以你打電話replace給replace xs,不是。對于最后一個守衛,您正在構建一個串列,所以,不是。第二個子句也沒有多大意義,因為它等同于第一個子句。replace [xs]x : (…)x (…)
因此,您可以將其實作為:
replace :: (Integral n, Eq a) => [a] -> a -> a -> n -> [a]
replace [] _ _ _ = []
replace xs _ _ 0 = xs
replace (x:xs) t r n
| x == t = r : replace xs t r (n-1)
| otherwise = x : replace xs t r n
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/427271.html
上一篇:部分型別家庭申請
下一篇:在下一步中使用結果流式傳輸管道
