程序不起作用。序言

时间:2015-12-27 19:05:07

标签: prolog

我有这个代码的问题。永远不会调用func3:

technology(board, saw, table).
technology(wood, sanded, board).
technology(water, grow, tree).

material(table, board, 20).
material(table, tree, 5).
material(wood, water, 100).

equipment(table,saw, cut, 10).
equipment(board, plane, polish, 7).
equipment(tree, watering, growing, 100).

specialization(saw, wood).
specialization(plane, wood).
specialization(watering, forestry).

plan_vypusku(table,10).

potreba_u_zahotovkah1(M, V):-
    write(M + V),
    nl,
    technology(F, _, M),
    material(M, F, C),
    Z is V * C,
    write(F - Z),
    nl.

func3([A, B], C):-
    write("InF3"),
    nl,
    potreba_u_zahotovkah1(A, C),
    func3(B, C).

func2([A, B], C):-
    write("InF2"),
    nl,
    findall(M, equipment(M, A, _, _), ML),
    write(ML),
    nl,
    func3(ML, C),
    func2(B, C).

potreba_u_zahotovkah(C, G):-
    findall(X, specialization(X, C), XL),
    write(XL),
    nl,
    plan_vypusku(G, S),
    func2(XL, S).

结果:

?- potreba_u_zahotovkah(wood,table).
[saw,plane]
InF2
[table]
false.

帮助PLS!

1 个答案:

答案 0 :(得分:0)

我不知道你在做什么,但我对你观察到的意外失败有一个解释。

您所做的查询按副作用(write/1nl/0)写了以下几行,然后失败了:

?- potreba_u_zahotovkah(wood,table).
[saw,plane]
InF2
[table]
false.

突出显示的行由以下突出显示的write/1nl/0输出:

func2([A, B], C):-
    write("InF2"),
    nl,
    findall(M, equipment(M, A, _, _), ML),
    write(ML),
    nl,
    func3(ML, C),
    func2(B, C).

因此,当调用目标ML时,变量[table]上方已绑定到 func3(ML, C)

看看你对func3/2的定义,失败的原因显而易见:

func3([A, B], C):-
    write("InF3"),
    nl,
    potreba_u_zahotovkah1(A, C),
    func3(B, C).

func3/2的子句头要求第一个参数是一个只有两个元素的列表。但是,列表[table]具有完全一个元素,而不是两个!

由于没有更多的选择点打开,目标potreba_u_zahotovkah(wood,table)失败。