【问题标题】:How to create a function that returns the result of applying a function to a value in Prolog?如何创建一个函数,该函数返回将函数应用于 Prolog 中的值的结果?
【发布时间】:2018-03-27 02:05:24
【问题描述】:

我是 Prolog 的新手,它创建函数的方式有点让我头疼。

在 Python 中,我会这样做

def get_result(func, inpt):
    return func(inpt)

现在我正在尝试在 Prolog 中做同样的事情。我从教程中发现,在 Prolog 中,编写函数时通常将返回值作为最后一个参数。然后你通过function(..., X)得到一个函数的结果,然后它会吐出X = ...作为你的答案。

所以,我尝试这样做:

get_result(Action, State, Result) :- Action(State, Result).

但是 Prolog 给了我错误Syntax error: operator_expected

如何编写一个函数,将第一个参数的结果应用于 Prolog 中的第二个参数?

【问题讨论】:

  • Prolog 没有函数。它有谓词。它们不是一回事。

标签: prolog


【解决方案1】:

只需致电: 例如:

double(X,Y) :-
    Y is 2 * X.

get_result(Action, State, Result) :- call(Action, State, Result).

结果:

?- get_result(double, 3, R).
R = 6.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多