如何从Prolog中的列表返回重复项

时间:2017-09-17 00:35:33

标签: recursion prolog duplicates

我正在尝试编写一个谓词,删除列表中的所有唯一元素(并返回重复项),如下所示:

delete_unique([],[]).
delete_unique([H|T],[H|Solution]):-
   member(H,Solution),!,
   delete_unique(T,Solution).
delete_unique([H|T],[H|Solution]):-
   member(H,T),
   delete_unique(T,Solution).
delete_unique([H|T],Solution):-
   %%not(member(H,Solution)),
   %%not(member(H,T)),
   delete_unique(T,Solution).

虽然我的代码不起作用。

{{1}}

1 个答案:

答案 0 :(得分:1)

要删除重复项,如果订单无关紧要,最简单的方法是使用class RecipeController extends Controller { /** * @Route("/admin/recipes", name="recipe_index") * @Method("GET") */ public function indexAction(Request $request) : Response { // code to fetch paginated list of recipes // and render it } /** * @Route("/admin/recipes/new", name="recipe_new") * @Method("POST") */ public function newAction(Request $request) { $recipe = new Recipe(); $form = $this->createForm(RecipeType::class, $recipe); if ($form->isSubmitted() && $form->handleRequest($request)->isValid()) { $entityManager = $this->getDoctrine()->getManager(); $entityManager->persist($recipe); $entityManager->flush(); $this->addFlash( 'success', 'Recipe successfully added!'); return $this->redirectToRoute('recipe_index'); } return $this->render('Admin/recipe_form.html.twig', [ 'form' => $form->createView(), ]); } /** * @Route("/admin/recipes/{id}", name="recipe_detail") * @Method({"GET"}) */ public function editAction(Request $request, Recipe $recipe) : Response { $form = $this->createForm(RecipeType::class,$recipe); if ($form->isSubmitted() && $form->handleRequest($request)->isValid()) { $entityManager = $this->getDoctrine()->getManager(); // No need to persist, the object is already persisted, just flush $entityManager->flush(); $this->addFlash( 'success', 'Recipe successfully updated!'); return $this->redirectToRoute('recipe_index'); } // You don't need to use the recipe_form_edit, you can use the one you created above return $this->render('Admin/recipe_form.html.twig', [ // Form already has its values filled-in no need to add the entity 'form' => $form->createView(), ]); } }

(
    select SUM(Amount_income_table) as 'Actual Income'
    from bacci.income_table
    where year(Date_income_table)='2017'
)
union
(
    select SUM(estimated_amount) as 'Estimated Income'
    from bacci.estimated_income_table
    where estimated_year='2017'
)
union
(
    select SUM(Amount_expenses_table) as 'Actual Expenses'
    from bacci.expenses_table
    where year(Date_expenses_table)='2017'
)
union
(
    select SUM(estimated_amount) as 'Estimated Expenses'
    from bacci.estimated_expenses_table
    where estimated_year='2017'
);

当然,您会看到元素的原始顺序丢失。更重要的是,如果您正在排序的列表已经基础(其中没有自由变量),这只能保证正确。