【发布时间】:2018-01-14 06:12:55
【问题描述】:
我不知道如何更改列表中的给定单词
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
【问题讨论】:
-
subst st nd [] = [] subst st nd (x:xs) = if st == x then nd : xs else x : xs
-
本人只是初学者,对Haskell中的递归函数不熟悉,不知道哪里做错了~
-
你的类型签名应该是
change :: Eq t => t -> t -> [t] -> [t] -
是的,抱歉,我刚才在那部分输入错误。应该改变 :: Eq t => t -> t -> [t] -> [t]
标签: string list haskell replace