prolog

reversible “binary to number” predicate

我只是一个虾纸丫 提交于 2019-12-28 02:06:38
问题 What is the best way to convert binary bits (it might be a list of 0/1, for example) into numbers in a reversible way. I've written a native predicate in swi, but is there better solution ? Best regards 回答1: Use CLP(FD) constraints, for example: :- use_module(library(clpfd)). binary_number(Bs0, N) :- reverse(Bs0, Bs), foldl(binary_number_, Bs, 0-0, _-N). binary_number_(B, I0-N0, I-N) :- B in 0..1, N #= N0 + B*2^I0, I #= I0 + 1. Example queries: ?- binary_number([1,0,1], N). N = 5. ?- binary

Splitting a list of integers into a list of positive integers and a list of negative integers

柔情痞子 提交于 2019-12-28 01:35:27
问题 I've been trying to create a predicate in Prolog which splits a list of integers into a list of positive integers and into a list of negative integers. Sample query with expected result: ?- split([1,-2,3,4,-8],X,Y). X = [1,3,4], Y = [-2,-8]. This is the code I got so far: split([], [], []). split([Head|Tail], List1, List2) :- split(Tail, [Head|List1], List2), Head>=0. split([Head|Tail], List1, List2) :- split(Tail, List1, [Head|List2]), Head<0. I can't seem to figure out what I'm doing wrong.

Write a Prolog DCG grammar that handle the mean of a sentence and also build its parse tree

萝らか妹 提交于 2019-12-25 18:25:27
问题 I have this DCG grammar that understand and agree phrases like: [john, paints] and [john, likes, mary] managing the semantic meaning directly into the DCG grammar by the use of parameters sentence2(VP) --> noun_phrase2(Actor), verb_phrase2(Actor, VP). noun_phrase2(Name) --> properName(Name). verb_phrase2(Actor, VP) --> intrans_verb(Actor, VP). verb_phrase2(Somebody, VP) --> trans_verb(Somebody, Something, VP), noun_phrase2(Something). properName(john) --> [john]. properName(mary) --> [mary].

ERROR: Out of global stack with append/3

二次信任 提交于 2019-12-25 09:13:50
问题 I have a problem. I want to implement a replace(E1, L1, E2, L2) predicate. This holds when L1 and L2 are the same lists,except that in one place where L1 has the value E1, L2 has E2. In addition, only one occurrence is replaced and it must work in any mode. For example: replace(2,[1,2,3,4],5,X) should have only the solution X = [1,5,3,4] . replace(2,[1,2,3,2,1],5,X) should backtrack over the solutions X = [1,5,3,2,1] and X = [1,2,3,5,1] . replace(2,X,5,[1,5,3,5,1]) should backtrack over the

How can I write rules for the following questions and facts in Prolog?

送分小仙女□ 提交于 2019-12-25 08:36:07
问题 Write a Prolog rule to find any route between two stations. route(Station1, Station2, Route) where Station1 and Station2 are station names and Route is a list of stations connecting Station1 and Station2 in order, including both Station1 and Station2. Write a Prolog rule to find the time taken to travel on a route between two stations, on the assumption that the time to travel between adjacent stations is 4 minutes. Ignore any time associated with changing lines at interchanges. route_time

Compiling Prolog-JPL on Ubuntu 11.10 64-bit?

你。 提交于 2019-12-25 08:27:26
问题 I'm trying to compile prolog-JPL as described here: https://code.google.com/p/javanaproche/wiki/HowToJPL Unfortunately, it ends with an error: ********************** * Package plunit ********************** config.status: creating Makefile ********************** * Package jpl ********************** config.status: creating Makefile config.status: creating src/java/Makefile config.status: creating config.h config.status: config.h is unchanged ********************** * Package R ******************

cut processing in prolog

瘦欲@ 提交于 2019-12-25 07:46:29
问题 can somebody please explain the processing of the following command: max(X,Y,Max) :- X >= Y, !, Max = X ; Max = Y. I can't understand, what exactly does it mean ! in the middle, thanks in advance 回答1: The ! (cut) means if you got this far (successfully in the current goal), don't do any backtracking (consider alternative ways to satisfy the current goal) that might have been open before the cut's placement. In your example the cut is being used to tersely express how to define the Max of X

How to check if element is not in a list, if it isn't, insert it

馋奶兔 提交于 2019-12-25 07:02:40
问题 I want, given a list formed by A-B elements ([a-b,b-c,c-d]), check if some given element it's already in a list (which I will return), and if it's not, add any "A" or "B" element that is not in the output list yet. I haven't really found any "easy" enough answer for me to understand yet (like, I found one with mappings and stuff I've never used nor seen). So the output list would need to only have [a,b,c,d] edit: what I'm trying to do is, I get called like this: enumerate([a-b,b-c,c-d],

Getting the head and tail of a list

☆樱花仙子☆ 提交于 2019-12-25 06:54:54
问题 I know if I do: [H|T] = [a,b,c,d]. H = a, T = [b,c,d]. What I'm trying to do is get the head and tail of a list inside a rule. Is this possible? I'm setting up base cases for a recursive call but for now I'd be satisfied if it just returned L3 as a append of the first list head and the second list head. I'm just not sure how I can get the head and list. compose([], L1, L1). compose(L2, [], L2). compose([], [], []). compose(L1, L2, L3) :- [H1|T1] = L1, [H2|T2] = L2, append(H1, H2, L3). I've

How to convert a string read from input to a list of lists in prolog

人盡茶涼 提交于 2019-12-25 06:52:06
问题 I'm trying to write a program that converts from Mayan to Arabic numerals and vice versa in prolog. Although I'm still running into some trouble I've managed to get it mostly working. I'm just wondering how if I read for example: ....| 0 .| ||| from a user input, how can I convert it to a list like this: L = [ ['.','.','.','.','|'], [0], ['.','|'], ['|','|','|'] ] I have an algorithm written getting from L to the arabic value, but I have no clue how to convert that string into a list. 回答1: