Haskell代码编译报错:parse error on input `ins' 求助
Haskell编译报错排查:parse error on input `ins'
原代码
module HW2sol where import HW2types ins :: Eq a => a -> Bag a -> Bag a ins x [] = [(x,1)] ins x (y:ys) = if x == fst y then (x, snd y + 1) : ys else y : (ins x ys)
报错信息
[2 of 2] Compiling HW2sol ( HMW2sol.hs, HMW2sol.o )
HMW2sol.hs:5:1: parse error on input `ins'
问题根源
Haskell模块声明语法不符合规范:where关键字不能直接与import连写在同一行,必须单独换行,或者与module写在同一行。错误的模块结构导致解析器无法正确识别后续的函数定义,进而触发解析错误。
修正后的代码
module HW2sol where import HW2types ins :: Eq a => a -> Bag a -> Bag a ins x [] = [(x,1)] ins x (y:ys) = if x == fst y then (x, snd y + 1) : ys else y : (ins x ys)
也可以将module和where放在同一行:
module HW2sol where import HW2types ins :: Eq a => a -> Bag a -> Bag a ins x [] = [(x,1)] ins x (y:ys) = if x == fst y then (x, snd y + 1) : ys else y : (ins x ys)
说明
调整模块声明的语法后,解析器能正确识别模块边界与导入语句,后续的ins函数定义即可被正常编译。
内容的提问来源于stack exchange,提问作者user19260869
相关产品推荐
相关产品推荐

