prolog

Prolog, find minimum in a list

痴心易碎 提交于 2019-12-17 16:55:41
问题 in short: How to find min value in a list? (thanks for the advise kaarel) long story: I have created a weighted graph in amzi prolog and given 2 nodes, I am able to retrieve a list of paths. However, I need to find the minimum value in this path but am unable to traverse the list to do this. May I please seek your advise on how to determine the minimum value in the list? my code currently looks like this: arc(1,2). arc(2,3). arc(3,4). arc(3,5). arc(3,6). arc(2,5). arc(5,6). arc(2,6). path(X,Z

How to transpose a matrix in prolog

情到浓时终转凉″ 提交于 2019-12-17 16:48:40
问题 How can I transpose a list like [[1,2,3][4,5,6][6,7,8]] to [[1,4,6],[2,7,8],[3,6,9]] ? To depict it: I'd like to flip the matrix 90 degree to the left. How can I do that? 回答1: Not sure your example is correct, but I get the idea. If using SWI-PROLOG, you can use the CLPFD module, like so: :- use_module(library(clpfd)). Allowing you to use the transpose/2 predicate, like this: 1 ?- transpose([[1,2,3],[4,5,6],[6,7,8]], X). X = [[1, 4, 6], [2, 5, 7], [3, 6, 8]]. Otherwise (if no SWI-PROLOG), you

SWI-Prolog how to show entire answer (list)?

时光怂恿深爱的人放手 提交于 2019-12-17 16:47:26
问题 I'm trying to convert a string to a list of ascii-codes like so: 7 ?- string_to_list("I'm a big blue banana in space!", C). C = [73, 39, 109, 32, 97, 32, 98, 105, 103|...]. 8 ?- This doesn't give me the entire list as you can see, but I need it. This solution does not work: I can't press w since it gives me the answer and does a full stop. Neither does this: I can call the function alright, and it returns true, but the list still isn't fully displayed. 11 ?- set_prolog_flag(toplevel_print

Ordering lists with constraint logic programming

自古美人都是妖i 提交于 2019-12-17 16:35:24
问题 I was wondering if anyone could help me with this problem: I have to order a list using Prolog with Constraing Logic Programming and I must do it with the more efficient way I can. So the main predicate I have defined is the next one: order(Xs,Ys) :- same_length(Xs,Ys), /* To determine the list Ys with the Xs' length */ perm(Xs,Ys), /* Permutation */ ordered(Ys), /* Is Ys ordered? */ ! . The implementation of each of the previous auxiliary predicates is as follows: same_length(Xs,Ys) :-

Recursive Prolog predicate for reverse / palindrome

我的未来我决定 提交于 2019-12-17 13:55:44
问题 Can I get a recursive Prolog predicate having two arguments, called reverse, which returns the inverse of a list: Sample query and expected result: ?- reverse([a,b,c], L). L = [c,b,a]. A recursive Prolog predicate of two arguments called palindrome which returns true if the given list is palindrome. Sample query with expected result: ?- palindrome([a,b,c]). false. ?- palindrome([b,a,c,a,b]). true. 回答1: Ad 1: It is impossible to define reverse/2 as a ( directly edit thx to @repeat: tail)

Subsets in Prolog

99封情书 提交于 2019-12-17 12:17:26
问题 I'm looking for a predicate that works as this: ?- subset([1,2,3], X). X = [] ; X = [1] ; X = [2] ; X = [3] ; X = [1, 2] ; X = [1, 2, 3] ; X = [2, 3] ; ... I've seen some subset implementations, but they all work when you want to check if one list is a subset of the another, not when you want to generate the subsets. Any ideas? 回答1: Here goes an implementation: subset([], []). subset([E|Tail], [E|NTail]):- subset(Tail, NTail). subset([_|Tail], NTail):- subset(Tail, NTail). It will generate

Prolog findall Implementation

﹥>﹥吖頭↗ 提交于 2019-12-17 09:59:58
问题 I've been tasked to implement a version of findall in Prolog without using any Prolog built-ins except for not and cut - so basically in pure Prolog. I'm trying to search a tree for all direct descendants and return the results in a list parent(a, b). parent(b, c). parent(b, d). parent(e, d). What I have so far is: find(X, L) :- find2(X, [], L). find2(X, Acc, L) :- parent(Y, X), find2(Y, [Y|Acc], L). find2(_, Acc, Acc). What I want to be getting when I enter for example: find(a,X). would be:

Prolog append with cut operator

你离开我真会死。 提交于 2019-12-17 09:52:11
问题 What problem can occur when we use append with cut operator? append2([],L,L):-!. append2([H|T],L,[H|TL]):-append2(T,L,TL). I have tried several different inputs, but it always succeeds. ?- append2([1,2],[5],L). L = [1, 2, 5]. ?- append2([1,2],[1,2],L). L = [1, 2, 1, 2]. ?- append2([],[1,2],L). L = [1, 2]. ?- append2([1,2],[],L). L = [1, 2]. 回答1: There are two kinds of cuts; green cuts and red cuts. Green cuts are inserted just to improve efficiency and don't change the semantics of the

Shuffle in prolog

删除回忆录丶 提交于 2019-12-17 06:53:57
问题 I'm trying to write a procedure in prolog where if L1 = [1,2,3] and L2 = [4,5,6] then L3 = [1,4,2,5,3,6] so shuffle([1,2,3],[4,5,6],[1,4,2,5,3,6]) I have this so far: shuffle([X],[Y],[X,Y]). shuffle([X|Xs],[Y|Ys],_) :- shuffle(Xs,Ys,Z), shuffle(X,Y,Z). This is my first attempt at writing prolog code so I'm still trying to wrap my head around the syntax, rules and everything. I understand the logic, I'm just not sure how to implement it so any help would be greatly appreciated! Thanks! Edit: I

SWI-Prolog - show long list

安稳与你 提交于 2019-12-17 06:49:47
问题 I'm using SWI-Prolog and I'm trying to print a list but if the list has more than 9 items - it look like that - [1, 15, 8, 22, 5, 19, 12, 25, 3|...] is there a way to show the whole list? 回答1: Have a look at: http://www.swi-prolog.org/FAQ/AllOutput.html The simple solution is to type w after the answer is given, i.e.: ?- n_queens_problem(10,X). X = [1, 3, 6, 8, 10, 5, 9, 2, 4|...] [write] X = [1, 3, 6, 8, 10, 5, 9, 2, 4, 7] After you have pressed the "w"-key "[write]" is displayed at the end