好的,大家好。我一直在尝试这样做,而现在没有成功。我想要做的是获取列表的中间数字将它们移动到列表的起始位置。例如:
[5,6,8,9,0]
=> [8,5,6,9,0]
// 8
已移至起始位置。
[5,6,7,8]
=> [6,7,5,8]
//两个中间数字被移动到起始位置。
到目前为止,我已经能够获得列表中的两个中间数字(下面的代码),但是我被卡住了,并且不知道如何移动数字。< / p>
我的代码:
middle :: [a] -> [a]
middle xs = take (signum ((l + 1) `mod` 2) + 1) $ drop ((l - 1) `div ` 2) xs
where l = length xs
答案 0 :(得分:1)
我建议这样做:
splitMiddle :: [a] -> ([a], [a], [a])
它将返回的地方
(beginning, middle, end)
然后你可以做
middleToFront :: [a] -> [a]
middleToFront xs
let (beginning, middle, end) = splitMiddle xs
in middle ++ beginning ++ end
要实施splitMiddle
,我建议将其分解为几个步骤
-- This is actually available in Data.List more efficiently,
-- but you can implement it yourself for practice
splitAt :: Int -> [a] -> ([a], [a])
splitAt n xs = (take n xs, drop n xs)
splitMiddle :: [a] -> ([a], [a], [a])
splitMiddle xs =
let l = length xs
beginningLength = ???
(beginning, rest) = splitAt beginningLength xs
middleLength = ???
(middle, end) = ???
in (beginning, middle, end)
您需要填写???
,我不会为您解决所有问题;)
答案 1 :(得分:0)
您可以使用$scope.selectItem = function(item) {
angular.forEach($scope.list,function(value){
if(value.name==item.name){
value.selected = true;
} else {
value.selected = false;
}
});
};
将该项目添加到列表的前面。对于奇数长度的情况,您可以按如下方式执行此操作:
(:)
您必须对偶数长度的情况进行修改,您可能需要单独识别和处理(即拉出并添加两个项目)。我会留下你的补充。