You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Haskell运行报错parse error on input `showMapping'`原因排查

错误原因

这个解析错误完全是由where子句的缩进不匹配导致的,Haskell是缩进敏感的语言,where子句下的所有变量/函数绑定需要满足两个规则:

  • 所有绑定的起始字符的缩进深度完全一致
  • 缩进深度必须大于where关键字的所在位置

你给出的代码中,where后的第一个showMapping'绑定的缩进深度,比后续两个showMapping'绑定的缩进深度多2个空格,编译器无法识别后续两个绑定属于where子句的范畴,因此抛出输入解析错误。

另外你代码还存在一个隐藏的逻辑错误:最后一行递归调用时写的是showMapping rest,应该改为showMapping' rest,否则会触发无限递归,在缩进问题修复后会暴露该问题。

修正后可正常运行的代码
showMapping :: (Show a, Show b) => Map a b -> String
showMapping m = "{ " ++ showMapping' m ++ " }"
  where 
    showMapping' MapSentinel = ""
    showMapping' (MapElem a b MapSentinel) =
              concat [show a, " -> ", show b]
    showMapping' (MapElem a b rest) =
              concat [show a, " -> ", show b,
                      ", ", showMapping' rest]

内容的提问来源于stack exchange,提问作者Andrea Berger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 20:15:01