Prolog-使用列表作为条件过滤列表

时间:2019-10-31 04:38:12

标签: prolog

我有以下事实:

def selector(input_list, check_list, return_list):
     flag = True
     input_list = set(input_list)
     for i in check_list:
         if i not in input_list: 
             flag = False
     if flag:
         return random.choice(return_list)
     else:
         return None
     return output

现在,我想通过不健康的面包和不健康的酱汁清单过滤掉面包和酱汁。我想创建一个类似于此功能的“功能”:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:ignote="MergeRootFrame">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:menu="@menu/menu_private_chat"
            app:navigationIcon="@drawable/ic_arrow_back_white_24dp"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/messages"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/enter_messages"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolbar"
            tools:listitem="@layout/animal_list_item" />

        <EditText
            android:id="@+id/enter_messages"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintStart_toEndOf="@id/messages"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:text="Send"
            app:layout_constraintBottom_toBottomOf="@+id/enter_messages"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@id/enter_messages"
            app:layout_constraintTop_toTopOf="@id/enter_messages" />


    </androidx.constraintlayout.widget.ConstraintLayout>


</layout>

给出输出:

breads([italian_wheat, hearty_italian, honey_oat, parmesan_oregano, multigrain, flatbread]).
sauces([chipotle_southwest, ranch, bbq, chili_sauce, tomato_sauce, mayonnaise, mustard_sauce]).
unhealthy_breads([hearty_italian, honey_oat, parmesan_oregano, flatbread]).
unhealthy_sauces([mayonnaise, chipotle_southwest, ranch]).

是否有一种简单的方法?

1 个答案:

答案 0 :(得分:0)

类似的东西:

breads([italian_wheat, hearty_italian, honey_oat, parmesan_oregano, multigrain, flatbread]).
sauces([chipotle_southwest, ranch, bbq, chili_sauce, tomato_sauce, mayonnaise, mustard_sauce]).
unhealthy_breads([hearty_italian, honey_oat, parmesan_oregano, flatbread]).
unhealthy_sauces([mayonnaise, chipotle_southwest, ranch]).

test(Bad, X) :-
    member(X, Bad).

filter(Data, Bad, Output) :-
   call(Data, Input_1),
   call(Bad, Input_2),

   % Filter elements for which Goal fails. 
   % True if List2 contains those elements Xi of List1 for which call(Goal, Xi) fails.
   exclude(test(Input_2),Input_1, Output).

示例

?- filter(breads,unhealthy_breads,Healthy).
Healthy = [italian_wheat, multigrain].