2

我是 prolog 的新手,并且正在使用 BProlog。

我一直在阅读一些示例程序来对一组相关数据执行查询。但是为了从结构相似的事实中推断,他们写了很多谓词,如search_by_name, search_by_point,这些谓词部分重复。

% 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.

出现错误:

| ?- 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找到

4

1 回答 1

4

我不知道任何接受变量函子的 Prolog。有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)
    ...
于 2012-04-20T06:01:07.110 回答