【问题标题】:How can I call a function random inside other function in prolog?如何在 prolog 中的其他函数中随机调用函数?
【发布时间】:2015-06-15 18:29:11
【问题描述】:

我正在尝试在另一个函数中调用随机函数。例如我想这样做assert(fact(random()). 但它不起作用。如何以这种方式插入随机数?谢谢。

【问题讨论】:

  • Prolog 不能以这种方式工作。你实际上要获得什么?你的目标是什么?
  • 你用的是什么prolog解释器? random() 是如何定义的? random() 不是普通的 Prolog 语法。如果你使用random/1,你可以这样做,random(X), assertz(fact(X)).
  • 只是猜测:你想设置种子吗?见the SICStus manual
  • 我正在使用 SWI-Prolog,不过还是谢谢大家

标签: prolog prolog-assert


【解决方案1】:

在 prolog 中,没有像您在代码中尝试执行的功能概念。你应该这样做:

random(N), assert(fact(N))

我建议至少阅读前两章Learn Prolog Now! 以更好地理解搜索和统一。

【讨论】:

  • 谢谢兄弟,我以为我能做到
猜你喜欢
  • 2018-06-09
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多