我有以下问题,我想编写一个生成嵌套列表的函数,然后将嵌套列表解压缩到一个列表中。
根据参数生成动态程度嵌套列表的函数显然不进行类型检查,因为返回类型会因参数而异。但是,是否可以递归解压缩列表以始终返回平面列表?
假设不可能的函数将为参数2返回此值:
[ [ [a], [b] ], [ [c], [d] ] ]
这是3的参数:
[[[[a], [b], [c]], [[d], [e], [f]], [[g], [h], [i]]], [[[j], [k], [l]], [[m], [n], [o]], [[p], [q], [r]]], [[[s], [t], [u]], [[v], [w], [x]], [[y], [z], [a1]]]]
现在我希望有一些函数可以放入递归调用中,这会产生类似这样的结果:
[ unpack [ unpack [a], unpack [b] ], unpack [ unpack [c], unpack [d] ] ]
反过来会评估为[a, b, c, d]
我能够写出这样的东西只列出一个元素:
unpack [x] = x
f 1 = [0]
f n = [unpack $ f x | x <- [1..n-1]]
*Main> f 3
[0,0]
但显然它失败了4:
*Main> f 4
[0,0,*** Exception: Non-exhaustive patterns in function unpack
手动&#34;追踪&#34;:
f 3 = [unpack $ f x | x <- [1..3-1]]
= [ unpack $ f 1, unpack $ f 2]
= [ unpack [0], unpack [f x | x <- [1..2-1]]]
= [ unpack [0], unpack [f 1] ]
= [ unpack [0], unpack [0] ]
= [0, 0]
理论上这样的功能是否可行?我有一种强烈的感觉,不是......但也许这种感觉是错误的。
如果还没有弄清楚我的想法:[unpack [1,2,3]]
会导致列表[1,2,3]
。
答案 0 :(得分:5)
正如评论中所写,在Haskell中无法实现所需的程序。
这是一个可能的替代方案:创建数据类型
data Unpack a = Unpack [Unpack a] | Elem a
deriving (Eq, Ord)
此外,您可以编写一个评估解包的函数:
unpacked :: [Unpack a] -> [a]
unpacked [] = []
unpacked (Unpack x : xr) = unpacked x ++ unpacked xr
unpacked (Elem x : xr) = x : unpacked xr
让我们让输出变得更漂亮:
instance Show a => Show (Unpack a) where
show (Unpack xs) = show xs
show (Elem x) = show x
ghci中的用法示例:
> list = [Unpack [Elem 1, Unpack [Elem 3, Elem 4]], Unpack [Elem 5, Elem 6, Elem 7]]
[[1,[3,4]],[5,6,7]]
> unpacked list
[1,3,4,5,6,7]