Max out of values defined by prolog clauses

前端 未结 5 881
名媛妹妹
名媛妹妹 2021-01-01 07:41

I know how to iterate over lists in Prolog to find the maximum, but what if each thing is a separate clause? For example if I had a bunch of felines and their ages, how woul

5条回答
  •  無奈伤痛
    2021-01-01 08:25

    A cat is the oldest if it's a cat and there is not a cat older than it. Let's write that in Prolog:

    oldest(X):- cat(X, _), not( thereAreOlders(X)), !.
    thereAreOlders(X):- cat(X, N), cat(C, M), C\=X, M > N.
    

    If you consult:

    ?- oldest(X).
    X = sassy.
    

提交回复
热议问题