[英]Reverse list of lists using foldr Haskell
我正在尝试通过使用文件夹反转Haskell中的列表列表。 有一个我想做的例子:
> reverse'' [[1,2,3],[3,4,5]]
[[5,4,3],[3,2,1]]
和我的代码(它不起作用):
reverse'':: [[a]]->[[a]]
reverse'' x = foldr (\n acum -> acum:(foldr(\m acum1 -> acum1++[m])) [] n) [[]] x
我的IDE在第二个文件夹的开头报告了一个错误。
为了分析您当前的解决方案,我将您的一线代码分解为一些辅助函数,并根据其组成推导了它们的类型:
reverse'':: [[a]] -> [[a]]
reverse'' x = foldr f [[]] x
where
f :: [a] -> a -> [a]
f n acum = acum : reverse n
reverse :: [a] -> [a]
reverse n = foldr append [] n
append :: a -> [a] -> [a]
append m acum = acum ++ [m]
当我尝试编译上面的代码时,ghc抱怨reverse''
类型签名出现以下错误:
Expected type: [[a]] -> [[a]] -> [[a]]
Actual type: [[a]] -> [a] -> [[a]]
我做了一些挖掘,然后为了使“ reverse''
具有类型[[a]] -> [[a]]
,函数f
需要具有类型[a] -> [[a]] -> [[a]]
。 但是,当前f
类型为[a] -> a -> [a]
,或者在这种情况下为[[a]] -> [a] -> [[a]]
。
以下是正确的类型,但是由于累加器的起始值,所以将一个额外的[]
值插入到数组的开头:
reverse'':: [[a]] -> [[a]]
reverse'' x = foldr f [[]] x
where
f :: [a] -> [[a]] -> [[a]]
f n acum = acum ++ [reverse n]
reverse :: [a] -> [a]
reverse n = foldr append [] n
append :: a -> [a] -> [a]
append m acum = acum ++ [m]
通过将初始累加器值更改为空列表[]
而不是空列表[[]]
列表,我们得到了一个可行的解决方案:
reverse'':: [[a]] -> [[a]]
reverse'' x = foldr f [] x
where
f :: a -> [a] -> [a]
f n acum = acum ++ [reverse n]
reverse :: [a] -> [a]
reverse n = foldr append [] n
append :: a -> [a] -> [a]
append m acum = acum ++ [m]
如果您真的希望将此作为单线工作,则为:
reverse'' :: [[a]] -> [[a]]
reverse'' = foldr (\n acum -> acum ++ [foldr (\m acum1 -> acum1 ++ [m]) [] n]) []
与工作:
append m acum = acum ++ [m]
append = \m acum1 -> acum1 ++ [m]
reverse n = foldr append [] n
reverse = \n -> foldr append [] n
reverse = foldr append []
reverse = foldr (\m acum1 -> acum1 ++ [m]) []
f n acum = acum ++ [reverse n]
f = \n acum -> acum ++ [reverse n]
f = \n acum -> acum ++ [foldr (\m acum1 -> acum1 ++ [m]) [] n]
reverse'':: [[a]] -> [[a]]
reverse'' x = foldr f [] x
reverse'' x = foldr (\n acum -> acum ++ [foldr (\m acum1 -> acum1 ++ [m]) [] n]) [] x
reverse'' = foldr (\n acum -> acum ++ [foldr (\m acum1 -> acum1 ++ [m]) [] n]) []
这是一个显式递归解决方案(即不使用fold
),提供了map
和reverse
。
reverse :: [a] -> [a]
reverse (x:xs) = reverse xs ++ [x]
reverse [] = []
map :: (a -> b) -> [a] -> [b]
map f (x:xs) = f x : map f xs
map _ [] = []
reverse'' :: [[a]] -> [[a]]
reverse'' ls = reverse $ map reverse ls
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.