prolog

How to encode causal relations in Prolog (as a linear function)

限于喜欢 提交于 2019-12-24 16:12:17
问题 Suppose that two variables X and Y are causally and linearly related, so that an increase in X produces an increase in Y (e.g. travel distance for cars and their fuel consumption). Both X and Y are vectors of N observations (N individual cars in the example). A way to represent such a relation is a simple linear equation Yi = a + bXi, which would describe the relation in the sample of N cases, where i = 1, 2, ..., N. Here a and b are constants, while Y and X are variables. Do you have any

Prolog: efficiency

拜拜、爱过 提交于 2019-12-24 16:07:31
问题 Is there a way in prolog to make the following shorter: rule(prop, [1/2,2/2]). rule(prop, [1/3,2/3,3/3]). rule(prop, [1/4,2/4,3/4,4/4]). rule(prop, [1/5,2/5,3/5,4/5,5/5]). rule(prop, [1/6,2/6,3/6,4/6,5/6,6/6]). rule(prop, [1/7,2/7,3/7,4/7,5/7,6/7,7/7]). 回答1: The following code isn't necessarily "shorter" for the case of 6 different rules, but it is more scalable, which is probably what you really mean. You can break this down as follows. First, a rule that generates one list: list_props(N, N,

up,down,left and right cannot be used when using SWI-Prolog in terminal

北城以北 提交于 2019-12-24 15:19:35
问题 I met a problem when I use prolog(swipl), after I type swipl in the terminal, code like '^[[A^[[B^[[D^[[C' shows when I press up, down, left, and right on my keyboard. Could somebody explain why? and how to fix it. Thanks in advance! 回答1: This means the readline/editline functionality isn't working. If you built it yourself, you probably have to install the development version of one of those libraries first and then rebuild. Or you can install rlwrap and do rlwrap swipl (which is helpful for

Reversing a list

China☆狼群 提交于 2019-12-24 14:19:59
问题 I need help reversing a list. fun(a, [b, d]). fun(b, [c]). fun(c, []). fun(d, [e]). fun(e, [f]). fun(f, [g]). fun(g, []). xyz(X, Y):- fun(X, Z) -> findall([A|B], (member(A, Z), xyz(A, B)), L), flatten(L, F), sort(F, Y); Y = []. The query xyz(a,X). gives me X = [b,c,d,e,f,g]. However, I would like it to give me X = [g,f,e,d,c,b] . I have tried different attempts at reversing the list, but I am not having any luck. I have tried adding an additional predicate right after this, but it didn't work

I can't get my Prolog DCG working with atom concat

一曲冷凌霜 提交于 2019-12-24 14:16:28
问题 I can't get this Prolog DCG code working: String1=" ",string_codes(String1,Codes),phrase(spaces(Output),Codes). spaces(XXs) --> [X], {X=32}, spaces(Xs), {char_code(Ch,X), atom_concat(Ch,Xs,XXs)}, !. %%Space spaces([]) --> []. 回答1: I solved this by changing [] in the base case to ''. spaces(XXs) --> [X], {X=32}, spaces(Xs), {char_code(Ch,X), atom_concat(Ch,Xs,XXs)}, !. %% Space spaces('') --> []. String1 = " ", Codes = [32, 32, 32], Output = ' '. 回答2: I feel like an improved solution would

How to print all the facts?

北城余情 提交于 2019-12-24 13:41:30
问题 I am stuck for this problem... isAt(keys, room3). isAt(book, room3). isAt(keys, room6). isAt(keys, room4). currently, room3 have keys and book. I want to print keys and book. I tried this code and apparently prints only one. (just keys) look :- isIn(Location), write('You are in '), write(Location), nl, items_inroom(Location), nl. items_inroom(Location) :- isIn(Location), isAt(Item, Location), write('Available Item(s):'), write(Item), nl. items_inroom(_) :- write('Available Item(s): None'), nl

Predicate that will swap the first two letters in an atom in Prolog

删除回忆录丶 提交于 2019-12-24 13:28:21
问题 I have to write a predicate which will swap the first two letters in an atom of length two or more. Length one atoms are unchanged. ?- interchange(cat,X). X = act; I am suppose to use name function to split the atom: ?- name(food,[X,Y|Z]). Z = "od", Y = 111, X = 102 ; This is the code that I wrote: inter(X,[]). inter(X,[Q|W]):- name(X,[H,T|R]), reverse([H,T],W), !, append([W],[R],F). I get this output: P = [] ; P = [_VCSF, 111, 102] ; How can I improve my code to get desired output. Thanks in

SWI Prolog CLP(FD) scheduling

巧了我就是萌 提交于 2019-12-24 13:26:32
问题 I am solving a scheduling task in SWI Prolog using the CLPFD library. Since it is the first time I solve something more serious than was the sendmory I probably need some good advices from more experienced users. Let me briefly describe the domain/the task. Domain I have a "calendar" for a month. Everyday there are 2 for the whole day, 2 for the whole night (long 12h service). There are also, only Mon-Fri 10 more workers for 8 hours (short service). The domain constraints are, obviously:

prolog need to compute the tree size

落花浮王杯 提交于 2019-12-24 13:04:03
问题 I need to get the size of a tree using: size(Tree,Size) What I have so far is wrong, please advise! size(empty, Size). size(tree(L, _, R), Size) :- size(L, Left_Size), size(R, Right_Size), Size is Left_Size + Right_Size + 1. Output should produce: ?- size(node(1,2),X). X = 2. ?- size(node(1,[2,3,4]),X). X = 2. ?- size(node(node(a,b),[2,3,4]),X). X = 3. 回答1: Prolog is a declarative language, you must state correctly your patterns: size(node(L,R), Size) :- ... % why you add 1 to left+right

Difference between two Implementation of even and odd in Prolog

无人久伴 提交于 2019-12-24 12:05:35
问题 i have two implementation of Prolog , the Function is to decide if the given number is odd or even the first one works correctly even1(0). even1(X) :- X>0 ,X1 is X-1, odd1(X1). odd1(1). odd1(X) :- X>1 , X1 is X-1, even1(X1). even1(2) returns true but the second one doesnt work correctly even2(0). even2(X) :- X>0 , odd2(X-1). odd2(1). odd2(X) :- X>1 , even2(X-1). even2(2) returns false can anyone explain to me whats is the difference between the two of them ? 回答1: Prolog is a relational