繁体   English   中英

如何从haskell中的列表中剪切?

[英]How can I cut from a list in haskell?

实现列表功能,将列表的一部分切掉! 第一个参数是子列表开始的索引,第二个参数是切割的长度。

list :: Int -> Int -> [b] -> [b]
list a b (x: xs) = x: sublist (a: a + b) xs

我尝试了这个递归解决方案,但它不起作用。 我不知道如何制作此代码。 我能得到一些帮助吗?

利用前奏。 先下降,然后采取:

cut :: Int -> Int -> [a] -> [a]
cut m n = take n . drop m

例如:

> cut 2 3 [0, 1, 1, 2, 3, 5, 8, 13]
[1,2,3]

或者,如果您设置滚动自己的递归实现:

cut' :: Int -> Int -> [a] -> [a]
cut' 0 0 xs       = []                    -- done
cut' _ _ []       = []                    -- done
cut' 0 n (x : xs) = x : cut' 0 (n - 1) xs -- take
cut' m n (x : xs) = cut' (m - 1) n xs     -- drop

的确:

> cut' 2 3 [0, 1, 1, 2, 3, 5, 8, 13]
[1,2,3]

暂无
暂无

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

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