[英]Haskell, replace every occurrence of the given word in the list
我被困在如何更改列表中的給定單詞
change :: Eq t => t -> t -> [t] -> [t]
change x y z
該函數應像給單詞x
並在z
列表中將其替換為y
一樣工作。
例如change 'a' 'c' "banana"
,結果將是"bcncnc"
我應該如何遞歸檢查列表並更改單詞的每次出現?
我自己的嘗試:
change st nd [] = []
change st nd (x:xs) = if st == x then nd : xs else x : xs
使用當前代碼,您僅更改此列表的第一個元素(如果它與st
匹配),並且將其余列表xs
保持不變。 您應該將change
功能應用於列表的末尾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.