prolog

How do I rewrite the following so it uses if_?

半城伤御伤魂 提交于 2020-04-03 04:31:34
问题 I am doing some easy exercises to get a feel for the language. is_list([]). is_list([_|_]). my_flatten([],[]). my_flatten([X|Xs],RR) :- my_flatten(Xs,R), (is_list(X), !, append(X,R,RR); RR = [X | R]). Here is a version using cut, for a predicate that flattens a list one level. my_flatten([],[]). my_flatten([X|Xs],RR) :- my_flatten(Xs,R), if_(is_list(X), append(X,R,RR), RR = [X | R]). Here is how I want to write it, but it does not work. Neither does is_list(X) = true as the if_ condition. How

Prolog - should this happen? [closed]

两盒软妹~` 提交于 2020-03-25 18:19:39
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 17 hours ago . Should this happen, in any program? This is the result of a tracing in SWISH. If it should, then what could be the reason? 来源: https://stackoverflow.com/questions/60833674/prolog-should-this-happen

Find mutual element in different facts in swi-prolog

邮差的信 提交于 2020-03-25 18:03:24
问题 i am trying to have an input of a list of movies and find the actors who play at the same movies. Question : Given a list of movies, display their link by a set of stars using recursion. these are an example of facts : fact(movie,actor). starsin(a,bob). starsin(b,bob). starsin(c,bob). starsin(a,maria). starsin(b,maria). starsin(c,maria). starsin(a,george). starsin(b,george). starsin(c,george). Example of input and out put : ?- sameActors([a,b,c],Y). Y = bob,maria,george. Rule written so far :

setof/3 inside setof/3 not working, but why?

泪湿孤枕 提交于 2020-03-22 09:00:26
问题 Inspired by Find mutual element in different facts in swi-prolog I wanted to try my hand at "RDBMS operations in Prolog" (actually, this is more or less Datalog) Problem statement Given a database of "actors starring in movies": starsin(a,bob). starsin(c,bob). starsin(a,maria). starsin(b,maria). starsin(c,maria). starsin(a,george). starsin(b,george). starsin(c,george). starsin(d,george). And given set of movies, find those actors that starred in all the movies of said set. I first had an ugly

七周七语言之Prolog学习笔记

眉间皱痕 提交于 2020-03-20 21:42:30
3 月,跳不动了?>>> ##有关于Prolog Prolog是一门逻辑编程语言,它于1972年由Alain Colmerauer和Phillipe Roussel开发完成,在 自然语言处理领域颇受欢迎。在Prolog中,数据以逻辑规则的形式存在,下面是基本 构建单元。 事实。事实是关于真实世界的基本断言。(Babe是一头猪,猪喜欢泥巴。) 规则。规则是关于真实世界中一些事实的推论。(如果一个动物是猪,那么它喜欢泥巴。) 查询。查询是关于真实世界的一个问题。(Babe喜欢泥巴吗?) 事实和规则被放入一个知识库(knowledge base)。Prolog编译器将这个知识库编译成一种适 于高效查询的形式。 在Prolog中,第一个字母的大小写是有着重要意义的,如果一个词以小写字母开头,它就是一个原子(atom)——一个类似Ruby符号(symbol)的固定值,如果一个词以大写字母或下划线开头,那么它就是一个变量。变量的值可以改变,原子则不能。 likes(wallace,cheese). //事实 likes(grommit,cheese). likes(wendolene,sheep). friend(X,Y) :- \+(X = Y), likes(X, Z), likes(Y, Z). //规则,\+执行逻辑取反操作,\+(X=Y)表示X不等于Y | ?- friend

Prolog- Mappings (Associative Arrays)

北慕城南 提交于 2020-03-19 05:32:12
问题 I am studying prolog and was wondering if anybody give me guidance on how to go about doing this question, It's the first of many in this area and knowing how to do this question will really help me progress. Thank-you in advance. Using Prolog define a predicate mapof(K, M, V) such that, when invoked with K instantiated to a key, and M instantiated to a mapping, mapof will instantiate the variable V to the value (or one of the values) associated with K in mapping M. The predicate should fail

【源码分析】Strawberry Prolog WarCraft.pro 源码分析

可紊 提交于 2020-03-16 21:24:42
某厂面试归来,发现自己落伍了!>>> calculate_pos:- start_random_from(_), for(P,0,G_CC-1), (status(P) =:= 4 -> move_object(P,tar_x(P),tar_y(P),2) % 单位的下达普通移动命令时的移动速度 else ( status(P) mod 2 =:= 1 -> move_shell(P,cc_x(attack(P)),cc_y(attack(P)),4) else ( status(P) =:= 2 -> ( cc_t(attack(P)) mod 4 =:= 2 -> status(P):=0 else ( (cc_x(P)-cc_x(attack(P)))**2 + (cc_y(P)-cc_y(attack(P)))**2 > 10000 -> move_object(P,cc_x(attack(P)),cc_y(attack(P)),3) % 单位下达攻击命令时前往攻击目标的移动速度 else status(P):=3, tar_x(P):=cc_x(P), tar_y(P):=cc_y(P) )) else ( status(P) =:= 0 -> (find_enemy(P,near,Enemy) -> status(P):=1, attack(P):=Enemy, tar

【源码分析】Strawberry Prolog 5.0 源码分析

百般思念 提交于 2020-03-16 20:25:08
某厂面试归来,发现自己落伍了!>>> 源码下载 前言 介绍 Prolog IDEs 的很多,但是专门介绍 Strawberry Prolog 的文章几乎没有。不要看它的官方网站还是上世纪的风格,也没有什么移动版视图,但是其到2020年1月23日仍有更新,推出了5.0版本的 二进制包 ,相应地提供了其 源码包 。自 3.0 Beta 4 推出的示例小游戏 WarCraft.pro(位于 安装目录/Games/ 下)至今仍吸引着我。希望能转化为其他编程语言实现,研究 IDE 如何实现了 Prolog 脚本是其中一项重要内容,在此写下自己的分析进展。 Assert.cpp - 编译入口 文件内只定义了一个函数: void assert(DWORD iii, DWORD where) 在 Linux 下编译会提示 assert 函数只需要一个参数却给了两个,这是因为与系统定义的 assert 函数冲突了,虽然 stdafx.h 中似乎取消了 assert 原来的定义: #undef assert //???plam 重命名这个函数为 prologAssert 之类的别的名称可以避免错误,在 HeadFunc.h 中的下述声明也需要修改: extern void assert(DWORD, DWORD); assert 函数体内的 Program 在 Intelig.cpp 文件中声明:

Prolog findall/3 rule convert to recursive rule

拥有回忆 提交于 2020-03-16 06:38:12
问题 score(Movies,Total) :- findall(Money,(member(Movie,Movies),takings(Movie,Money)),Profit), sum_list(Profit,Total) I want to convert this rule using recursion but I am not sure how. Help! Link to the previous question that answers this one : Sum up data from facts 回答1: % base case score([],0). % recursive case score([Movie|Movies],Total) :- takings(Movie,Profit), score(Movies,Total0), Total is Total0 + Profit. Example run ?- score([robots,hulk,bad_boys_ii],Y). Y = 749200000. Explanation of code

Prolog, permutation code understanding

為{幸葍}努か 提交于 2020-03-05 07:53:12
问题 I'm trying to understand how this program works. Code from Daniel Lyons' solution(from the link above) takeout(X,[X|R],R). takeout(X,[F |R],[F|S]) :- takeout(X,R,S). perm([X|Y],Z) :- perm(Y,W), takeout(X,Z,W). perm([],[]). I'm trying ti understand how it works with this list [1,2,3] So, I have perm([1,2,3],X). It's easy to understand at first, Y = [2,3] then Y = [3] and then Y = [] After that perm([],[]). is called and it gives us W = [] Now, takeout is called for the first time - takeout(3,