[英]Haskell trying to mutate string in [String]
movex [] a s = []
movex (x:xs) a s
| elem a x = moveNow x a s
| otherwise = x : (movex xs a s)
where
moveNow x a s
| s == 'l' = moveNow2 x a
where
moveNow2 [] _ = []
moveNow2 (x:y:xs) a
| x == ' ' && y == a = a : x : moveNow2 (y:xs) a
| otherwise = x : moveNow2 (y:xs) a
<- This is what I got right now < - 这就是我现在所得到的
I am trying to make a function that iterates through [string], finds the right string and then mutates it. 我正在尝试创建一个遍历[string]的函数,找到正确的字符串,然后将其变异。
given input 给定输入
func ["abc", "dfg"] f l -- move f in this list 1 space left --
expected output 预期产出
["abc", "fdg"]
Right now I am stuck at movex function that gives me error 现在我被困在movex函数,给我错误
Couldn't match expected type `Char' with actual type `[Char]'
In the first argument of `(:)', namely `x'
In the expression: x : (movex xs a s)
Direct solution to the error is to replace the line 直接解决错误就是更换线路
| elem a x = moveNow x a s
With 同
| elem a x = moveNow x a s : movex xs a s
Or, probably 或者,可能
| elem a x = moveNow x a s : xs
Depending on what you want to do after the first match: continue looking for certain character, or leave other strings untouched. 取决于您在第一场比赛后想要做的事情:继续寻找某个角色,或保持其他角色不变。
Your moveNow
function has return type String
, or [Char]
, while movex
has [String]
, or [[Char]]
, that's why compiler complains. 你的
moveNow
函数有返回类型String
或[Char]
,而movex
有[String]
或[[Char]]
,这就是编译器抱怨的原因。
To avoid such problems(or fix them easier) consider writing explicit type signatures, like so: 为避免此类问题(或更容易修复),请考虑编写显式类型签名,如下所示:
movex :: [String]->String->String->[String]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.