使用自己的数据类型解析地图输入上的错误

问题描述

因此,我正在尝试创建一个可以连接字符串或整数的函数,但出现问题。

我拥有的自定义数据类型是

data AnEither  = AString String | AnInt Int
                deriving (Show,Read,Eq)

我为字符串编写了此concat函数,并且可以正常工作

concatAll :: [[AnEither]] -> AnEither
concatAll [[]] = ""
concatAll x = foldr (++) "" (map concat x)

现在我对如何使用多种数据类型感到困惑。

我创建了此辅助功能

concatHelper :: AnEither -> AnEither -> AnEither
concatHelper nothing x = x
concatHelper x nothing = x
concatHelper x y = x ++ (show y)

然后是另一个concat函数

concat2Either:: [[AnEither]] -> AnEither
concat2Either x = foldr concatHelper '' (map (foldr concatAll "") x)

所以基本上,如果我输入concat2E [[AString“”,AnInt 0],[]] 它会输出

“ AString” 0“”

我先是遇到了map的解析错误,然后又是如何完全实现这些功能的麻烦

我在地图上遇到此错误

错误:解析输入“地图”上的错误 | 56 | concat2Either x =文件夹concatHelper''(地图(folder concatAll“”)x)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)