prolog

understand the running trace of quicksort in prolog

荒凉一梦 提交于 2019-12-24 03:52:24
问题 I have the following prolog code for quicksort: gt(X,Y):- X @> Y. conc([], List, List). conc([Head|Tail], List1, [Head|List2]):- conc(Tail, List1, List2). quicksort([], []). quicksort([X|Tail], Sorted):- split(X,Tail,Small,Big), quicksort(Small,SortedSmall), quicksort(Big, SortedBig), conc(SortedSmall, [X|SortedBig], Sorted). split(X,[],[],[]). split(X,[Y|Tail],[Y|Small],Big):- gt(X,Y), !, split(X,Tail,Small, Big). split(X,[Y|Tail],Small,[Y|Big]):- split(X,Tail,Small,Big). The array for

Find out if first letter is a vowel prolog

女生的网名这么多〃 提交于 2019-12-24 03:34:33
问题 I'm used to procedural programming languages, and I'm kind of struggling with prolog - the lack of resources online is also a bummer. What would be the most 'prolog'-y way to get the first character of a given variable and check if it is a vowel? Something like this is what I'm after, I think? This is all pseudocode - but is that how you'd solve it? isVowel(Word) :- vowels = [a, e, i, o, u], firstLetter(Word[0]), ( firstLetter in vowels -> Vowel! ; Not a vowel! ). Thanks so much, Ollie 回答1:

Size Procedure in Prolog Language

微笑、不失礼 提交于 2019-12-24 03:27:25
问题 I am new to Prolog and am not that great when it comes to recursive algorithms as is, thus I am confused with the following two clauses: size([], 0). size([H|T], N) :- size(T, N1), N is N1+1. I am having trouble tracing this problem for: ?- size([a,b,c,d], N). This will unify with the second clause to form: size([a,b,c,d], N) :- size([b,c,d], N1), N is N1+1. But I am confused with the N is N1+1 as these variables are never unified. What values do these variables take? Any help regarding this

Prolog list not printing all the elements on console

陌路散爱 提交于 2019-12-24 03:07:39
问题 I am using SWI-PROLOG version 6.6.6 I want to print all the attributes of a particular predicate type. I have a predicate called law with arity 2. Some of the facts are law(borrow,'To borrow Money on the credit of the United States'). law(commerce,'To regulate Commerce with foreign Nations, and among the several States, and with the Indian Tribes'). law(unifomity,'To establish an uniform Rule of Naturalization, and uniform Laws on the subject of Bankruptcies throughout the United States').

Eagerly evaluating all predicate calls in Prolog

﹥>﹥吖頭↗ 提交于 2019-12-24 02:55:13
问题 Reading the SWI-Prolog documentation on meta-predicates, I initially assumed that call(f, ...) is equivalent to f(...) , where f is some predicate. But I observe that the behavior of the two actually diverge in certain cases. For instance, suppose a knowledge base includes the clause f(g(x)) . Then the query call(f, g(x)) succeeds, whereas f(call(g, x)) does not. This is problematic, because I sometimes need to use clauses whose bodies include nested predicate calls. I'd like Prolog to

Is there a way to sum only the integers in a list of pairs that contain a letter and an integer in Prolog?

丶灬走出姿态 提交于 2019-12-24 02:24:03
问题 I'm having trouble figuring out how to find the sum of the integers that are in a list of pairs like so: [[a, 1], [b, 2], [c, 3], [d, 4]] I tried something like this, since it is reminiscent of a regular sum function: sum([], 0). sum([[_,Head]|[_,Tail]], Sum) :- sum([_,Tail], Sum2), Sum is Head+Sum2. With the call being: sum([[a, 1], [b, 2], [c, 3], [d, 4]], Total), write('Sum = '), write(Total). But that doesn't work. It prints out false, when it should print out the sum, which would be 10

Prolog and Logic Puzzles

瘦欲@ 提交于 2019-12-24 02:23:16
问题 I seem to be having an issue with the unification of facts in Prolog, but can't confirm it. Everything looks like it should work, and I've looked up examples of solved logic puzzles using Prolog to no real effect, given the relative rarity of Prolog. This is an extra credit assignment, so I'm unsure if it's valid, but I'm genuinely stumped on how to proceed from here % Names name(teo). name(mira). name(bruno). name(igor). %Food food(sandwich). food(pie). food(hamburger). food(pizza). %Hobby

Swapping consecutive items of a list in Prolog

一世执手 提交于 2019-12-24 02:21:26
问题 I'm trying to write Prolog code that can swap two elements of a list, but only if they are consecutive to each other . That is, conseq_swap(d, e, [a, g, d, e, f], X). should give: X = [a, g, e, d, f]. (d and e are consecutive.) However, conseq_swap(a, e, [a, g, d, e, f], X). should always fail (a and e are not consecutive.) I can assume that an item appears in the list only once. I have the following code, which is actually working fine: swap_conseq(X, Y, MainList, SwappedList) :- indexOf

Gprolog Knight's Tour using Warnsdorff's Rule

余生颓废 提交于 2019-12-24 02:14:15
问题 I'm trying to implement Warnsdorff's Rule in Gprolog to generate tours on an arbitrary chessboard. I found an SO post providing a good solution in B-prolog, and I simply needed to translate the Warnsdorff step (knight's tour efficient solution). Below is my implementation of the Warnsdorff step: warnsdorffSelect(X, Y, Row, Col, Past, NewX_, NewY_) :- setof((Count, NewX, NewY), ( possibleMovesFromPosWithBoard(X, Y, Row, Col, Past, NewX, NewY), countMoves(NewX, NewY, Row, Col, [(NewX, NewY) |

Sort a list by the second atom in functor

陌路散爱 提交于 2019-12-24 02:13:51
问题 I have this list in prolog: List = [functor(a,1),functor(n,7),functor(l,9),functor(k,0)] and i want to sort it by the number in the functor instead of the letter. I mean if i do sort(L,L1) i will get L1 = [functor(a,1),functor(k,0),functor(l,9),functor(n,7)] but i want it to be like L1 = [functor(k,0),functor(a,1),functor(n,7),functor(l,9)] is there any way of doing that (and not just recreate it and put the number first) 回答1: To "recreate" the list (kind of): ?- List = [functor(a,1),functor