实现list函数,该函数可以删除列表的一部分!第一个参数是子列表开始的索引,第二个参数是切割的长度。
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]