prolog

Combinations of multiple lists - Prolog

扶醉桌前 提交于 2020-04-13 06:45:23
问题 I need to find the combinations in a list of lists. For example, give the following list, List = [[1, 2], [1, 2, 3]] These should be the output, Comb = [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3]] Another example: List = [[1,2],[1,2],[1,2,3]] Comb = [[1,1,1],[1,1,2],[1,1,3],[1,2,1],[1,2,2],[1,2,3]....etc] I know how to do it for a list with two sublists but it needs to work for any number of sublists. I'm new to Prolog, please help. 回答1: This answer hunts the bounty offered " for a pure solution

How to solve 15-puzzle paradigm in Prolog with Manhattan & Hamming Heuristics

半腔热情 提交于 2020-04-11 05:32:29
问题 I have this implementation of the 15-puzzle game, using Prolog (Swipl). I have already implemented the A* search using Manhattan heuristic, but now I need to add hamming heuristic. Do yo know how to implement it? :- op(400,yfx,'@'). resolver(Estado,MovimientosSolucion) :- evaluar(Estado,0,F), buscarSolucion([Estado@0@F@[]],S), reverse(S,MovimientosSolucion). evaluar(Estado,Profundidad,F) :- evaluarCoste(Estado,Coste), F is Profundidad + Coste. buscarSolucion([Estado@_@_@MovimientosSolucion|_]

How to solve 15-puzzle paradigm in Prolog with Manhattan & Hamming Heuristics

六月ゝ 毕业季﹏ 提交于 2020-04-11 05:32:08
问题 I have this implementation of the 15-puzzle game, using Prolog (Swipl). I have already implemented the A* search using Manhattan heuristic, but now I need to add hamming heuristic. Do yo know how to implement it? :- op(400,yfx,'@'). resolver(Estado,MovimientosSolucion) :- evaluar(Estado,0,F), buscarSolucion([Estado@0@F@[]],S), reverse(S,MovimientosSolucion). evaluar(Estado,Profundidad,F) :- evaluarCoste(Estado,Coste), F is Profundidad + Coste. buscarSolucion([Estado@_@_@MovimientosSolucion|_]

Generate all expressions from list of numbers equal to a number [PROLOG]

本小妞迷上赌 提交于 2020-04-11 04:45:31
问题 I am given a list of numbers, for example [22,45,2,6,7,...] . Now I have to insert binary operators: + , - , / , * and parentheses ( , ) between numbers so that expression is equal to given number k . List all possible expressions created by insertions of operators and parentheses that will give sum of k . Position of numbers in resulting expression have to be fixed, i.e. only insertion of operators and parentheses between or around numbers For example : given number k=9 and list [1,2,3] ,

Generate all expressions from list of numbers equal to a number [PROLOG]

陌路散爱 提交于 2020-04-11 04:43:41
问题 I am given a list of numbers, for example [22,45,2,6,7,...] . Now I have to insert binary operators: + , - , / , * and parentheses ( , ) between numbers so that expression is equal to given number k . List all possible expressions created by insertions of operators and parentheses that will give sum of k . Position of numbers in resulting expression have to be fixed, i.e. only insertion of operators and parentheses between or around numbers For example : given number k=9 and list [1,2,3] ,

Prolog: Splitting a number into a sequence of increasing integers

南楼画角 提交于 2020-04-07 07:08:08
问题 After doing some Prolog in uni and doing some exercises I decided to go along somewhat further although I got to admit I don't understand recursion that well, I get the concept and idea but how to code it, is still a question for me. So that's why I was curious if anyone knows how to help tackle this problem. The idea is given a number e.g. 45, check whether it is possible to make a list starting with 1 going n+1 into the list and if the sum of the list is the same as the given number. So for

Prolog: Splitting a number into a sequence of increasing integers

你离开我真会死。 提交于 2020-04-07 07:07:30
问题 After doing some Prolog in uni and doing some exercises I decided to go along somewhat further although I got to admit I don't understand recursion that well, I get the concept and idea but how to code it, is still a question for me. So that's why I was curious if anyone knows how to help tackle this problem. The idea is given a number e.g. 45, check whether it is possible to make a list starting with 1 going n+1 into the list and if the sum of the list is the same as the given number. So for

How do I rewrite the following so it uses if_?

情到浓时终转凉″ 提交于 2020-04-03 04:36:15
问题 I am doing some easy exercises to get a feel for the language. is_list([]). is_list([_|_]). my_flatten([],[]). my_flatten([X|Xs],RR) :- my_flatten(Xs,R), (is_list(X), !, append(X,R,RR); RR = [X | R]). Here is a version using cut, for a predicate that flattens a list one level. my_flatten([],[]). my_flatten([X|Xs],RR) :- my_flatten(Xs,R), if_(is_list(X), append(X,R,RR), RR = [X | R]). Here is how I want to write it, but it does not work. Neither does is_list(X) = true as the if_ condition. How

How do I rewrite the following so it uses if_?

谁都会走 提交于 2020-04-03 04:32:10
问题 I am doing some easy exercises to get a feel for the language. is_list([]). is_list([_|_]). my_flatten([],[]). my_flatten([X|Xs],RR) :- my_flatten(Xs,R), (is_list(X), !, append(X,R,RR); RR = [X | R]). Here is a version using cut, for a predicate that flattens a list one level. my_flatten([],[]). my_flatten([X|Xs],RR) :- my_flatten(Xs,R), if_(is_list(X), append(X,R,RR), RR = [X | R]). Here is how I want to write it, but it does not work. Neither does is_list(X) = true as the if_ condition. How

How do I rewrite the following so it uses if_?

随声附和 提交于 2020-04-03 04:31:48
问题 I am doing some easy exercises to get a feel for the language. is_list([]). is_list([_|_]). my_flatten([],[]). my_flatten([X|Xs],RR) :- my_flatten(Xs,R), (is_list(X), !, append(X,R,RR); RR = [X | R]). Here is a version using cut, for a predicate that flattens a list one level. my_flatten([],[]). my_flatten([X|Xs],RR) :- my_flatten(Xs,R), if_(is_list(X), append(X,R,RR), RR = [X | R]). Here is how I want to write it, but it does not work. Neither does is_list(X) = true as the if_ condition. How