繁体   English   中英

调用助手 function 来反转列表,然后在 Haskell 中压缩列表

[英]Calling helper function to reverse lists, then zipping lists in Haskell

我是 Haskell 的新手,我已经尝试了几个小时让这两个函数一起工作,以使用递归和没有内置函数来生成压缩反向列表。

这是用于反转列表的助手 function:

reverseList :: [x] -> [x]
reverseList  [] = []
reverseList  xs = last xs : reverseList (init xs)

然后是拉链 function

ZipRevLists2 :: [x] -> [y] -> [(x, y)]
ZipRevLists2 _ [] = []
ZipRevLists2 [] _ = []
ZipRevLists2 (x:xs) = reverseList xs
ZipRevLists2 (y:ys) = reverseList ys
ZipRevLists2 (x:xs) (y:ys) = (x,y) : ZipRev2Lists xs ys

我正在尝试向 learnyouahaskell.com 学习,但唯一的例子是“ghci>”,而不是真正的 function 形式。

谢谢,我终于明白了!

Zip function

zip' :: [x] -> [y] -> [(x,y)]
zip' _ [] = []
zip' [] _ = []
zip' (x:xs) (y:ys) = (x,y):zip' xs ys

和主function

zipRevLists2 :: [Int] -> [Char] -> [(Int,Char)]
zipRevLists2 _ [] = []
zipRevLists2 [] _ = []
zipRevLists2 (x:xs) (y:ys) = zip' (reverseList (x:xs))  (reverseList (y:ys))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM