我正在尝试制作一种算法,该算法接收列表并根据以下操作步骤重新组织列表。
[1 , 2, 3, 4 , 5, 6, 7 , 8, 9 ]
[1 , 3, 5 , 7, 9 ] ++ [2 , 4, 6, 8 ]
[1 , 3, 5 , 7, 9 ] ++ [2 , 6] ++ [ 4, 8 ]
[1 , 3, 5 , 7, 9 , 2, 6 , 4, 8 ]
我已经走到这一步了,然后我不知道下一步该做什么。有什么帮助吗?
ever xs = if xs == [] then [ ] else head xs: ever (drop 1 residue)
Where residue 1 drop = xs
最佳答案
这是 CPS 版本:
shuffle [] = []
shuffle xs = shuffle' xs shuffle where
shuffle' (x:y:xs) cont = x : shuffle' xs (cont . (y:))
shuffle' xs cont = xs ++ cont []
关于haskell - simpel Haskell 列表按模式排序/洗牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26759867/