How to create a list from facts in Prolog?

前端 未结 2 751
自闭症患者
自闭症患者 2021-01-18 11:59

There are these facts:

man(john).
man(carl).
woman(mary).
woman(rose).

I need to create the predicate people(List), which returns a list wi

相关标签:
2条回答
  • 2021-01-18 12:31

    Using findall/3:

    people(L) :- findall(X, (man(X) ; woman(X)), L).
    
    ?- people(X).
    X = [john, carl, mary, rose].
    
    0 讨论(0)
  • 2021-01-18 12:45

    Here we go:

    person(anne).
    person(nick).
    
    add(B, L):-
        person(P),
        not(member(P, B)),
        add([P|B], L),!.
    
    add(B, L):-
        L = B,!.
    
    persons(L):-
        add([], L).
    
    0 讨论(0)
提交回复
热议问题