English 中文(简体)
将论点转化为清单
原标题:Prolog transformation of arguments into a list
  • 时间:2011-11-20 16:31:31
  •  标签:
  • prolog

If I had the predicate cat(dog(cow(a,b,c))), how I can transform this into cat(dog(cow([a,b,c])))? I was thinking about the system s predicate =.. but it doesn t end properly, because I obtain cat([dog([cow([a,b,c])])]), and that s not that I want :( The patch needs to fix also other similar case as dog(cat(a,b,c)) into dog(cat([a,b,c])).

任何人都有想法? Or maybe I m与运营商做过错......

我的想法是:

animal(X) :- atomic(X).
animal(X) :- atomic([]).

cow(X) :-
   animal(X),
   cow([X|List]).

dog(X) :-
   animal(X),
   dog([X|List]).

cat(X) :-
   animal(X),
   cat([X|List]).

感谢

问题回答

使用<条码>=.,对tern子进行检查。

ternary_takes_list(T0, T) :-
    (T0 =.. [F, X, Y, Z] ->
        T =.. [F, [X, Y, Z]]
    ; T0 =.. [F, X0] -> 
        ternary_takes_list(X0, X),
        T =.. [F, X]
    ;
        T = T0
    ).

您的例子如下:

?- ternary_takes_list(cat(dog(cow(a,b,c))), X).
X = cat(dog(cow([a, b, c]))).

?- ternary_takes_list(dog(cat(a,b,c)), X).
X = dog(cat([a, b, c])).




相关问题
Prolog : Learning by example

I am trying to learn a little bit about swi-prolog (beyond the basic, useless programs). Can anyone explain (perhaps in pseudocode) what this sudoku solver and the related functions are doing? If ...

Working with lists in Prolog

First off let me state that this is part of a class exercise given as homework. But, the entire assignment is much more involved than the subject of this question. So.. I am searching through two ...

SWI-Prolog conditional statements

I m trying to write a function that will test to see if the word hello is contained in a list. If it is contained, i don t want it to say "true", i want it to say : "yes, the word hello is contained ...

prolog cut off in method

I have a question I would like to ask you something about a code snippet: insert_pq(State, [], [State]) :- !. insert_pq(State, [H|Tail], [State, H|Tail]) :- precedes(State, H). insert_pq(State, [...

Max out of values defined by prolog clauses

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 would I find the oldest kitty? ...

热门标签