【问题标题】:Why do I get parse error after my if then else statement in Haskell?为什么我在 Haskell 中的 if then else 语句之后会出现解析错误?
【发布时间】:2021-11-13 00:40:09
【问题描述】:

我的 if then else 语句出现解析错误,我不明白为什么。我已经检查了函数,据我所知,他们都得到了他们需要的参数,所以我不明白为什么会这样。我对 haskell 非常陌生,因此我们将不胜感激。

replace :: [(Int,Int)] -> String -> String
replace xs ys = if translate (manipulator (fst (convert xs)) (snd (convert xs)) ys) /= ""
then take (fst (convert xs)) ys ++ (manipulator (fst (convert xs)) (snd (convert xs)) ys) ++ drop (snd (convert xs)) ys
else take (fst (convert xs)) ys ++ stjerner (snd (convert xs) - fst (convert xs)) ++ drop (snd (convert (xs)) ys

--get parse error here, see picture.
stjerner :: Int -> String
stjerner 0 = ""
stjerner int | int > 0 =  "*" ++ stjerner (int -1)
manipulator:: Int -> Int -> String -> String
manipulator low high xs = take high (drop low xs)
convert :: [a] -> a
convert [a] = a

picture of parse error here

【问题讨论】:

  • 看起来您在上面的行中有一个未闭合的括号。这真的不是非常惯用的 Haskell,使用这么多嵌套括号应该很少见。考虑改用$ 运算符。
  • 您使用convert xs 的次数很多。您应该使用let (f,s) = convert xs in ... 之类的东西,然后直接使用f,s。注意convert crashs是xs的长度不是1。这看起来很可能是错误的。
  • this 是您的代码应该如何编写的。看看那里是否更容易发现错误。

标签: haskell parse-error


【解决方案1】:

else 子句中的最后一个表达式 --- drop (snd (convert (xs)) ys -- 括号错误。您有 3 个打开的括号,但只有 2 个关闭的括号。我想你的意思是drop (snd (convert xs)) ys

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 2013-05-26
    • 2015-11-18
    • 1970-01-01
    • 2019-02-09
    相关资源
    最近更新 更多