dcg

How do you do a search and replace of a list with another sublist in Prolog?

帅比萌擦擦* 提交于 2019-11-28 01:59:24
I'm trying to modify a list by search and replace, was wondering how do I search through a list with the search term as a list as well? Lets say I have a list [1,2,3,4] I want to single out the 2 and 3 and replace it with 5,6 so ideally I could have a predicate: search_and_replace(Search_Term, Replace_Term, Target_List, Result_List). eg. search_and_replace([2,3], [5,6], [1,2,3,4], Result_List), write(Result_List). You can use append/2 as follows : replace(ToReplace, ToInsert, List, Result) :- once(append([Left, ToReplace, Right], List)), append([Left, ToInsert, Right], Result). With or without

How to enumerate combinations using DCGs with CLP(FD) and multiple constraints

醉酒当歌 提交于 2019-11-28 01:33:42
This question starts from Mat's answer to Algorithm improvement for enumerating binary trees which has only one input value that determines the number of all nodes for the binary tree, and the need to be able to have two input values with one being the number of unary nodes and the other being the number of binary nodes. While I was able to derive a solution by using listing/1 and threading extra state variables: e(t, B, B, U, U). e(u(E), B0, B1, [_|U0], U1) :- e(E, B0, B1, U0, U1). e(b(E0, E1), [_|B0], B2, U0, U2) :- e(E0, B0, B1, U0, U1), e(E1, B1, B2, U1, U2). e(U,B,Es) :- length(Bs, B),

gnu Prolog powerset modification

余生颓废 提交于 2019-11-27 16:10:48
So i got this for powerset: powerset([], []). powerset([H|T], P) :- powerset(T,P). powerset([H|T], [H|P]) :- powerset(T,P). This generates all sets of a list. Is it possible to generate all sets in list order. Example: List = [a,b,c] I want to get [a],[a,b],[a,b,c],[b],[b,c],[c] Note there is no [a,c] in this list of subsets since these are subsets starting from the left and going to the right. I've tried using a combination of append and recursion, but that didn't work out as i wanted it to. Little stumped at this point. Thanks. How about powerset(L, [H|T]):- append([H|T], _, L). powerset([_

Recursive Prolog predicate for reverse / palindrome

柔情痞子 提交于 2019-11-27 15:34:40
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. Ad 1: It is impossible to define reverse/2 as a ( directly edit thx to @repeat: tail) recursive predicate - unless you permit an auxiliary predicate. Ad 2: palindrome(X) :- reverse(X,X). But the

Get elements from list of lists

北慕城南 提交于 2019-11-27 09:47:44
is it possible to get all elements from list of lists in Prolog? Something like: We have getElements([[[a,b,[c]],d,e],f,g,[h,[i,j]]],S) and the result is: S = [a,b,c,d,e,f,g,h,i,j] ... Thanks for help. magus In SWI-Prolog (and maybe others), you can use flatten/2 : ?- flatten([[[a,b,[c]],d,e],f,g,[h,[i,j]]], S). S = [a, b, c, d, e, f, g, h, i|...]. Note that the SWI-Prolog manual page for flatten/2 includes the following statement: Ending up needing flatten/3 often indicates, like append/3 for appending two lists, a bad design. However, the page doesn't say whether there is another native

Building an Expression Tree in Prolog

半世苍凉 提交于 2019-11-27 09:46:45
I'm looking for a way to build an Expression Tree in Prolog. I already did some experiments and came up with the following working code (that will only handle constants and the plus expression): const(_). plus(_, _). eval(const(R), R). eval(plus(A, B), R) :- number(A), number(B), R is A+B. eval(plus(A, B), R) :- number(A), eval(B, B_R), R is A+B_R. eval(plus(A, B), R) :- eval(A, A_R), number(B), R is A_R+B. eval(plus(A, B), R) :- eval(A, A_R), eval(B, B_R), R is A_R+B_R. Is there any simpler alternative to this approach? Will I have to define these 4 cases for each one of the operators I plan

Prolog getting head and tail of string

有些话、适合烂在心里 提交于 2019-11-27 06:54:45
问题 I'm trying to wrap my brain around Prolog for the first time (SWI-Prolog) and I'm struggling with what I'm sure are the basics. I'm trying to take a string such as "pie" and print out the military NATO spelling of it to look something like this: spellWord("Pie"). Papa India Echo Currently I'm just trying to verify that I'm using the [H|T] syntax and Write function correctly. My function is: spellWord(String) :- String = [H|T], writeChar(H), spellWord(T). writeChar(String) :- H == "P", print4(

Read a file line by line in Prolog

独自空忆成欢 提交于 2019-11-27 02:04:11
I'd like to read a plain text file and apply a predicate to each line (the predicates contain write which does the output). How would I do that? In SWI-Prolog, the cleanest solution is to write a DCG that describes what a "line" is, then call a predicate for each line. Use library(pio) to apply the DCG to a file. EDIT : As requested, consider: :- use_module(library(pio)). lines([]) --> call(eos), !. lines([Line|Lines]) --> line(Line), lines(Lines). eos([], []). line([]) --> ( "\n" ; call(eos) ), !. line([L|Ls]) --> [L], line(Ls). Sample usage: ?- phrase_from_file(lines(Ls), 'your_file.txt').

How to enumerate combinations using DCGs with CLP(FD) and multiple constraints

北战南征 提交于 2019-11-26 21:58:32
问题 This question starts from Mat's answer to Algorithm improvement for enumerating binary trees which has only one input value that determines the number of all nodes for the binary tree, and the need to be able to have two input values with one being the number of unary nodes and the other being the number of binary nodes. While I was able to derive a solution by using listing/1 and threading extra state variables: e(t, B, B, U, U). e(u(E), B0, B1, [_|U0], U1) :- e(E, B0, B1, U0, U1). e(b(E0,

Flatten a list in Prolog

旧时模样 提交于 2019-11-26 20:57:57
I've only been working with Prolog for a couple days. I understand some things but this is really confusing me. I'm suppose to write a function that takes a list and flattens it. ?- flatten([a,[b,c],[[d],[],[e]]],Xs). Xs = [a,b,c,d,e]. % expected result The function takes out the inner structures of the list. This is what I have so far: flatten2([],[]). flatten2([Atom|ListTail],[Atom|RetList]) :- atom(Atom), flatten2(ListTail,RetList). flatten2([List|ListTail],RetList) :- flatten2(List,RetList). Now, this works when I call: ?- flatten2([a,[b,c],[[d],[],[e]]], R). R = [a,b,c,d,e]. % works as