prolog

Convert complex term to List of lists and then back to term with modified functor

落花浮王杯 提交于 2021-02-11 12:28:50
问题 you can use =.. to convert simple terms. ?- x(a,b,c) =.. A. A = [x, a, b, c]. what about complex terms : x(a,b(c,d)) ==> [x,a,[b,c,d]] x(a,b(c,q(d))) ==> [x,a,[b,c,[q,d]]] then as separate task i want to re-generate the terms with changed functor : x(a,b(c,d)) ==> [x,a,[b,c,d]] ==> y(a,f(c,d)) 回答1: deep_univ(X, Y) :- X =.. [H|Rest], ( Rest = [] -> Y = X ; maplist(deep_univ, Rest, ExpRest), Y=[H|ExpRest] ). rev_univ([H|Rest], Y) :- maplist(rev_univ, Rest, RestT), Y =.. [H|RestT]. rev_univ(H, H

Convert complex term to List of lists and then back to term with modified functor

不羁岁月 提交于 2021-02-11 12:27:35
问题 you can use =.. to convert simple terms. ?- x(a,b,c) =.. A. A = [x, a, b, c]. what about complex terms : x(a,b(c,d)) ==> [x,a,[b,c,d]] x(a,b(c,q(d))) ==> [x,a,[b,c,[q,d]]] then as separate task i want to re-generate the terms with changed functor : x(a,b(c,d)) ==> [x,a,[b,c,d]] ==> y(a,f(c,d)) 回答1: deep_univ(X, Y) :- X =.. [H|Rest], ( Rest = [] -> Y = X ; maplist(deep_univ, Rest, ExpRest), Y=[H|ExpRest] ). rev_univ([H|Rest], Y) :- maplist(rev_univ, Rest, RestT), Y =.. [H|RestT]. rev_univ(H, H

Prolog systems with occurs check aware freeze

风流意气都作罢 提交于 2021-02-11 12:22:39
问题 David Tonhofer posed an interesting problem about occurs check. An occurs check aware coroutine can be realized as follows with a predicate with_occurs_check/2 which can be easily implemented as a refinement of with_occurs_check/1: occurs_check_aware_freeze(X, G) :- current_prolog_flag(F), freeze(X, with_occurs_check(F, G)). Namely by using an with_occurs_check/2 with a further parameter. But the main problem of a realiable with_occurs_check/[1,2] that also works with backtracking remains.

formatting the output with proper paranthesis - Prolog

若如初见. 提交于 2021-02-11 07:58:36
问题 This question is directly related to first order logic creating terms for arithmetic expressions using prolog. After implementing the logic as per the link I have issues with the formatting of the ourput for printauth/1 . It currently results as 8-2+4* -3 , how is it possible to get something like ((8-2)+(4* -3)) (notice its not the same as +(-(8,2),*(4,-3))) . I have been trying to using various options (\k,\q) in format/2 predicate but nothing works. even I tried write_canonical and other

How to restrict values of an argument in Prolog using a series of facts?

夙愿已清 提交于 2021-02-11 06:55:59
问题 I want to restrict the query property(X, use, Y) to values of Y in the list [a,b,c] . c/1 is true for only those values of Y . I thought the following would work, but it doesn't. c(a). c(b). c(c). property(X, use, Y). c(Y). The following statements yield only false . person(1). property(1, use, _). I'm using Problog, but I'm not using any Problog functions here, so I think I am misunderstanding something about unification. I thought c(Y) would generate the list and Y would be unified across

Build a list with abs() in prolog

痞子三分冷 提交于 2021-02-11 06:27:53
问题 I am attempting to write a predicate for the following task. Write a predicate distances(Bs, B, Ds) where Bs and Ds are lists of variables such that the ith element of the Ds is the absolute difference between the variable B and the ith element of Bs know this is incorrect but it what I believe I roughly should be trying to do distances([],_,[]). distances([H|T],B,A) :-abs(H - B,A),distances(T,B,A) Do I need to return the result of the abs predicate into the recusive call to distance? I can

Get the output value from a Prolog recursive function

℡╲_俬逩灬. 提交于 2021-02-10 16:56:29
问题 I am learning Prolog these days and need to combine two list into one that follows this rule: ListA: [], ListB: [] => ResultList: [] ListA: [], ListB:[a,b,c] => ResultList: [[a],[b],[c]] ListA: [1,2], ListB:[a,b,c] => ResultList: [[1,2,a],[1,2,b],[1,2,c]] I am stuck with a recursive function output value problem and here is my coding: extend_my_path([],_,_,_). extend_my_path([H|T],OriginalPath,OrignailMewPaths,NewPaths) :- append(OriginalPath,[H],NewPath), append(OrignailMewPaths,[NewPath]

Get the output value from a Prolog recursive function

和自甴很熟 提交于 2021-02-10 16:56:18
问题 I am learning Prolog these days and need to combine two list into one that follows this rule: ListA: [], ListB: [] => ResultList: [] ListA: [], ListB:[a,b,c] => ResultList: [[a],[b],[c]] ListA: [1,2], ListB:[a,b,c] => ResultList: [[1,2,a],[1,2,b],[1,2,c]] I am stuck with a recursive function output value problem and here is my coding: extend_my_path([],_,_,_). extend_my_path([H|T],OriginalPath,OrignailMewPaths,NewPaths) :- append(OriginalPath,[H],NewPath), append(OrignailMewPaths,[NewPath]

Get the output value from a Prolog recursive function

◇◆丶佛笑我妖孽 提交于 2021-02-10 16:56:10
问题 I am learning Prolog these days and need to combine two list into one that follows this rule: ListA: [], ListB: [] => ResultList: [] ListA: [], ListB:[a,b,c] => ResultList: [[a],[b],[c]] ListA: [1,2], ListB:[a,b,c] => ResultList: [[1,2,a],[1,2,b],[1,2,c]] I am stuck with a recursive function output value problem and here is my coding: extend_my_path([],_,_,_). extend_my_path([H|T],OriginalPath,OrignailMewPaths,NewPaths) :- append(OriginalPath,[H],NewPath), append(OrignailMewPaths,[NewPath]

Perform list conversion,Prolog

扶醉桌前 提交于 2021-02-10 06:57:17
问题 [a,a,a,a,b,c,c,a,a,d,e,e,e,e] => [[4,a],b,[2,c],[2,a],d,[4,e]]. please help me solve this problem I have this code, but I do not know how to bring it to the one that is required or how can it be done differently or easier: p([]):- !. p( [X] ):- !, write(X). p( [X | T] ):-!, write(X), write(", "), p(T). first_letter([H], Let, Num, Mid, Res):- ( H = Let, New_num is Num +1, G = [Let], Prom = [New_num | G], Res = [Prom | Mid], ! ; true ), ( H \= Let,New_num is 1, G = [Let], Prom = [Num | G], New