English 中文(简体)
曼 谷 具有可变性但具有可变性
原标题:Can I subtitude functor with variable in a predicate

我是新来的,是利用博客。

I have been reading some example program to execute query on group of related data. But in order to infer from facts with similar structure, they wrote many predicates like search_by_name,search_by_point, which are partly duplicated.

% working search in example
search_by_name(Key,Value) :-
    Key == name,
    sname(ID,Value),
    point(ID,Point),
    write(Value),write(Point),nl.

当我试图用更笼统的措辞来取代它们时:

% a more general search I want to write
% but not accepted by BProlog
search_by_attr(Key,Value) :-
    Key(ID,Value),
    sname(ID,Name),
    point(ID,Point),
    write(Name),write(Point),nl.

error arised:

| ?- consult( students.pl )
consulting::students.pl
** Syntax error (students.pl, 17-21)
search_by_attr(Key,Value) :-
        Key<<HERE>>(ID,Value),
        sname(ID,Name),
        point(ID,Point),
        write(Name),write(Point),nl.

1 error(s)

我这样做是错误的,还是不可能在道歉中采取这种态度?

守则和实例数据可在https://gist.github.com/2426119查询。

最佳回答

I don t know any Prolog that accept variables funct或s. There is call/N, 或 univ+call/1.

search_by_attr(Key,Value) :-
    call(Key, ID, Value), % Key(ID,Value)
    ...

search_by_attr(Key,Value) :-
    C =.. [Key, ID, Value], % univ
    call(C),                % Key(ID,Value)
    ...
问题回答

暂无回答




相关问题
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? ...

热门标签