Arrays 阵列模式匹配
我创建了一个:Arrays 阵列模式匹配,arrays,pattern-matching,Arrays,Pattern Matching,我创建了一个: newType Board = Board (Array (Int, Int) Life) ,在哪里 data Life = Alive|Dead 我正在尝试创建一个函数,通过模式匹配将电路板保存为字符串: showBoard:: Config -> String showBoard Board(array ((0, 0), (w, h)) [(a:as)]) = *code* 但这只给了我“pattern:array中的解析错误”。我看不出有什么问题?您只能在数
newType Board = Board (Array (Int, Int) Life)
,在哪里
data Life = Alive|Dead
我正在尝试创建一个函数,通过模式匹配将电路板保存为字符串:
showBoard:: Config -> String
showBoard Board(array ((0, 0), (w, h)) [(a:as)]) = *code*
但这只给了我“pattern:array中的解析错误”。我看不出有什么问题?您只能在数据构造函数上进行模式匹配<代码>数组不是数据构造函数;它是一个常规函数,内部使用
数组
数据构造函数创建并返回数组
值。内部细节
一个数组的
未公开,这会阻止您在其上进行模式匹配
相反,您需要使用为查看数组
值而提供的函数。这些可以由一个函数组成,该函数接受可以进行模式匹配的参数
-- bounds :: Array i e -> (i, i)
-- elems :: Array i e -> [e]
showConfig :: Board -> String
showConfig (Board arr) = showConfig' (bounds arr) (elems arr)
where showConfig' :: ((Int,Int),(Int,Int)) -> [Life] -> String
showConfig' ((0,0),(w,h)) (a:as) = ...
如果您将板
类型修改为
newtype Board = Board { getArray :: Array (Int, Int) Life }
您可以使用应用程序样式重写showConfig
:
showConfig = (showConfig' <$> boards <*> elems) . getArray
where showConfig' ((0,0),(w,h)) (a:as) = ...
showConfig=(showConfig'板元素)。getArray
其中showConfig'((0,0),(w,h))(a:as)=。。。
只能在数据构造函数上进行模式匹配<代码>数组不是数据构造函数;它是一个常规函数,内部使用数组
数据构造函数创建并返回数组
值。内部细节
一个数组的
未公开,这会阻止您在其上进行模式匹配
相反,您需要使用为查看数组
值而提供的函数。这些可以由一个函数组成,该函数接受可以进行模式匹配的参数
-- bounds :: Array i e -> (i, i)
-- elems :: Array i e -> [e]
showConfig :: Board -> String
showConfig (Board arr) = showConfig' (bounds arr) (elems arr)
where showConfig' :: ((Int,Int),(Int,Int)) -> [Life] -> String
showConfig' ((0,0),(w,h)) (a:as) = ...
如果您将板
类型修改为
newtype Board = Board { getArray :: Array (Int, Int) Life }
您可以使用应用程序样式重写showConfig
:
showConfig = (showConfig' <$> boards <*> elems) . getArray
where showConfig' ((0,0),(w,h)) (a:as) = ...
showConfig=(showConfig'板元素)。getArray
其中showConfig'((0,0),(w,h))(a:as)=。。。
数组
是一个类型构造函数;它的数据构造函数是什么样子的?我正在使用数据。数组库Array
是一个返回Array
值的函数,但它不是数据构造函数<代码>数据。数组似乎不会导出任何数据构造函数,因此您无法对这些值进行模式匹配。数组
是类型构造函数;它的数据构造函数是什么样子的?我正在使用数据。数组库Array
是一个返回Array
值的函数,但它不是数据构造函数Data.Array
似乎不会导出任何数据构造函数,因此无法对这些值进行模式匹配。