【问题标题】:why prolog isn't printing this list为什么 prolog 不打印此列表
【发布时间】:2011-08-07 15:16:35
【问题描述】:

我在下面有一个序言规则

schedule(mary,[ma424,ma387,eng301]).

我有一个谓词

taking(X,Y):- schedule(X, [Y | L]). 

当我试图通过打字来弄清楚她正在上什么课时

taking(mary,Y).

我得到 y=ma424

为什么不打印出她的所有课程

我也尝试过这个和其他变体

taking(X,Y):- schedule(X,[X|L]),schedule(Y, [Y | L]),schedule(Y,L),X\=Y,X\=L.

但它不起作用

我如何让它打印所有的类给我的规则定义的方式

【问题讨论】:

    标签: list search printing prolog items


    【解决方案1】:

    这是由于您定义谓词的方式。

    taking(X,Y) :-        % X takes class Y if...
        schedule(X,       % in the schedule for X,
                 [Y|L]).  % Y is the first element.
    

    如果你不告诉它,你的程序不会神奇地决定搜索列表L。为此,请使用 member/2 谓词:

    taking(Student, Class) :-
        schedule(Student, Classes),
        member(Class, Classes).
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-23
      • 2019-03-01
      • 1970-01-01
      • 2022-11-22
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多