Parsing TareaHaskell.hs:36:69:error:parse-error-on-input';)';
这是我第一次尝试Haskell。我试图创建一个函数,它接受一个元素和一个列表,并删除该项的第二个外观。例如,如果元素是Parsing TareaHaskell.hs:36:69:error:parse-error-on-input';)';,parsing,haskell,input,Parsing,Haskell,Input,这是我第一次尝试Haskell。我试图创建一个函数,它接受一个元素和一个列表,并删除该项的第二个外观。例如,如果元素是2,列表是[2,3,4,2,5,2],则结果将是[2,3,4,5,2] 然而,我得到了这个错误: TareaHaskell.hs:36:69: error: parse error on input ‘)’ | 36 | | ( (a == x) && not (isItIn x newList) ) = ( (let newList = x:[])
2
,列表是[2,3,4,2,5,2]
,则结果将是[2,3,4,5,2]
然而,我得到了这个错误:
TareaHaskell.hs:36:69: error: parse error on input ‘)’
|
36 | | ( (a == x) && not (isItIn x newList) ) = ( (let newList = x:[]) && (deleteSecond a xs) )
代码:
我读到缩进可能有问题,但我已经尝试过使用空格,前后移动,但仍然不起作用。
我也在使用记事本++和升华来帮助缩进,而不使用任何东西。问题是解析器不希望代码中出现
)
:
(let newList = x:[])
因为在Haskell中,let
语句后面不跟是无效的。(除了do
块内部。)
我真的不清楚您的实际意图是什么,但是
let
的目的就是在代码块中为更复杂的表达式指定一个临时名称。中没有附带的let
语句没有任何意义,它会导致您在此处出现解析错误。您的代码有点过于复杂。如果你简化,问题在哪里会更清楚,但是
let newList = x:[]
仅当下一个符号为
中的时有效。解析器抱怨您无缘无故地试图将其插入括号。let..in语句只是为了表达式的目的临时将名称绑定到值的一种方式
let var = value in expr
至于问题本身:使用显式递归很容易解决
removeSecond :: (Eq a) -> [a] -> [a]
removeSecond = go False
where go _ _ [] = []
go True needle (x:xs) | needle == x = xs
| otherwise = x : go True needle xs
go False needle (x:xs) | needle == x = x : go True needle xs
| otherwise = x : go False needle xs
我试图创建一个新列表,然后将原始列表中的元素x
添加到我正在创建的新列表中。deletessecond a[]=newList
这里的newList
是什么?一个应该是答案的新列表,我试图用定义它(让newList=x:[])下面的let
语句在中没有相应的在do
表达式之外无效。@viga11 aaaaahh!这听起来像是你在试图管理国家!不,不,从功能上思考!
removeSecond :: (Eq a) -> [a] -> [a]
removeSecond = go False
where go _ _ [] = []
go True needle (x:xs) | needle == x = xs
| otherwise = x : go True needle xs
go False needle (x:xs) | needle == x = x : go True needle xs
| otherwise = x : go False needle xs