Questions tagged [prolog]

Prolog is the most commonly used logic programming language. It supports non-deterministic programming through backtracking and pattern matching through unification.

Filter by
Sorted by
Tagged with
3 votes
0 answers
39 views

Topological sort in Prolog

I wanted to calculate topological sort given a few predicates asserting that one element should be before another in a sequence. order(a, b) means that ...
Jakub Nowak's user avatar
3 votes
1 answer
51 views

maxlist(L, M) that succeeds if M is the max number in the list L

I'm working my way through "Prolog For Artificial Intelligence" by Ivan Bratko, and exercise 3.17 is to write a maxlist(L, M) that succeeds when ...
Avrohom Yisroel's user avatar
2 votes
0 answers
261 views

Temperature Scale Converter in Prolog

Problem statement: Write a program that converts all given temperatures from a given input temperature scale to a given output temperature scale. The temperature scales to be supported are Kelvin, ...
Christian Hujer's user avatar
1 vote
0 answers
185 views

Pocket cube solver in Prolog

Hello I have writtern this Prolog code to solve a scrambled 2x2 rubiks cube. Also known as a a pocket cube. ...
Teodor Dyakov's user avatar
1 vote
1 answer
88 views

Prolog refactor if/else

How do I refactor this mess of if/else conditions so that it's more readable? ...
pfft's user avatar
  • 19
2 votes
0 answers
31 views

Simple task with recursion on Prolog

I'm new in Prolog. So, I'm not sure if my code is ok and optimized. I'm also not sure if I use recursion correctly. Task: find students that has at least 1 bad grade (1 or 2) and print them in table ...
Dmitry Gashko's user avatar
6 votes
1 answer
265 views

Constraint solving CrossCells puzzle game solution

There's a puzzle game called CrossCells (not affiliated; there's also multiple other ones with the same kind of idea, but slightly different layout and rule set) that I've been playing on and off. ...
ferada's user avatar
  • 11.1k
3 votes
0 answers
77 views

Basic logic problem in Prolog

I thought it would be a good idea to take some easy to solve (by hand) problems and model them in Prolog for practice. Here is a problem I modeled in Prolog: A messy kid wrote a multiplication ...
Dair's user avatar
  • 5,864
4 votes
0 answers
257 views

Writing a tokenizer in SWI-Prolog

To teach myself how to do meta-programming using Prolog, I've been playing around with translating ABNF syntax into DCG syntax. I'm still at the lexing and tokenising stage, but hope to advance to the ...
joeblog's user avatar
  • 243
1 vote
2 answers
120 views

Find CISC prerequisites

Below I have some code written in prolog as a brief example. The goal of the program is to take a Computer and Information Science (CISC) course number and return any CISC prerequisites (prereqs). I ...
DanSchneiderNA's user avatar
2 votes
1 answer
513 views

Echo in prolog with single print

In order to learn a bit more of Prolog, I implemented the echo command for SWI-Prolog. I want a correct implementation, that is, it satisfies the following ...
Christian Hujer's user avatar
5 votes
1 answer
145 views

Counting number of seen states

I have some code which executes a simple task, counting the number of states reachable from a particular state, would just like some advice on how to improve it. So far it stores all the states it ...
user3667111's user avatar
4 votes
2 answers
1k views

Zebra puzzle in Prolog

I have attempted to a zebra puzzle in prolog and I was seeking some feedback as to the way I went about solving the puzzle. Here is the puzzle: Two weeks ago, four enthusiasts made sightings of ...
Thomas C's user avatar
2 votes
0 answers
147 views

All outcomes for subtraction on each value in list individually

As you can see from my predicate iterate, I am repeating Z is X-3 and Z is X-2, the scenarios are different if the value is 0 or ...
Small Legend's user avatar
4 votes
1 answer
17k views

Family tree in Prolog

I tried to write a program in Prolog and describe a family tree: ...
Brian Brown's user avatar
10 votes
1 answer
784 views

Harry Potter's Family Tree Database

As an assignment for a class, I have made a small Harry Potter family tree in Prolog. It compiles into a self-contained executable that can be run straight from the command line on Linux machines. ...
Michael Brandon Morris's user avatar
3 votes
0 answers
348 views

Movie Knowledgebase

I have had an assignment to construct a movie knowledgebase and ten specified queries in Prolog. This is my first ever Prolog program, so I would appreciate any pointers toward how I could improve. ...
Michael Brandon Morris's user avatar
3 votes
2 answers
186 views

CLP(FD) labeling on possibly infinite domains

As a follow-up to this SO question, I implemented, for my Prolog-based language Brachylog, my own labeling predicate brachylog_equals/2 which is not guaranteed to ...
Fatalize's user avatar
  • 133
4 votes
1 answer
588 views

Aggregate cost for a directed graph in Prolog

I've come up with a simple graph traversal algorithm to calculate the aggregated cost of a directed graph or its subgraphs. Given a directed graph, the code visits all reachable nodes and sums up the ...
Jacek's user avatar
  • 41
6 votes
1 answer
239 views

Logic Prolog compiler

I'm actually doing my own Prolog compiler just like SWISH one with Java. Prolog is a logic language that is particularly suited to programs that involve symbolic or non-numeric computation. Prolog ...
Revolucion for Monica's user avatar
7 votes
3 answers
990 views

Prolog parser written in Haskell

I am writing a very simple Prolog intepreter in Haskell. It's a class assignment and I really want to do it right. I was able to (quite quickly) write a parser for the language. Today I borrowed a ...
David's user avatar
  • 203
3 votes
1 answer
94 views

Prolog predicate for adjacency of integers with all signatures and no spurious choice points

I want to create a predicate nearby(X,Y) which represents "X and Y are integers 1 apart from each other" and fully satisfies: ...
Mark Green's user avatar
4 votes
0 answers
21k views

Monkey-banana problem in Prolog

The monkey-banana problem: There many variations to this problem, but the basic premise is that a monkey is in a room with a banana and a chair, and the monkey cannot reach the banana until he moves ...
goelakash's user avatar
  • 167
10 votes
1 answer
704 views

cat program in Prolog

In order to learn Prolog, I have implemented the cat command in Prolog. I want to know if the code is idiomatic and what could be improved. File ...
Christian Hujer's user avatar
3 votes
0 answers
30 views

meta predicates fixedpoint/3 and fixedpointlist/3

What about the following meta-predicate "fixed point"? ...
repeat's user avatar
  • 275
6 votes
1 answer
273 views

map + foldl = mapfoldl

I was wondering... In the standard library of some Prolog systems, there are meta-predicates like maplist and foldl. In general,...
repeat's user avatar
  • 275
8 votes
2 answers
586 views

Higher-order Prolog predicates combine/3 and reduce/3

I'm trying to find useful versatile meta-predicates that are different from the vanilla ones offered currently by library(apply). How about the following two: <...
repeat's user avatar
  • 275
4 votes
1 answer
156 views

SICP Exercise 1.3: Sum of squares of two largest numbers out of three, Prolog Version

The exercise 1.3 of the book Structure and Interpretation of Computer Programs asks the following: Exercise 1.3. Define a procedure that takes three numbers as arguments and returns the sum of the ...
Marcus Vinícius Monteiro's user avatar
2 votes
1 answer
226 views

Repeated string concatenation

I've been looking for a way to have a predicate that establishes that all elements of a list are substrings delimited by a given string. Analogues in other languages include: ...
wvxvw's user avatar
  • 961
2 votes
1 answer
717 views

Adding elements in two lists as numbers

The problem is adding two lists as numbers L1 = [1,9,9] and L2 = [0,9,9]: ...
YonCho's user avatar
  • 41
5 votes
1 answer
187 views

Binary Digits to ASCII Characters

I wrote this code, as a novice a to Prolog, for a challenge posted on /r/DailyProgrammer. The formal description of this challenge, as presented in the prompt reads: Input description On ...
ankh-morpork's user avatar
2 votes
1 answer
131 views

English phrases, prime numbers, pseudo random data and encryption source code check

This code uses a DCG and pairs phrases into near-English sentences. If anyone is familiar with Prolog, I'm wondering how I can use the code as an encryption method by hiding data and replacing it ...
user avatar
2 votes
2 answers
86 views

Iterating towards a goal

I'm pretty proud of the following, but would welcome any comments from experts: ...
Simon H's user avatar
  • 203
2 votes
1 answer
337 views

Implementing an open/close function in Prolog

I'm going through the Adventure in Prolog tutorial and I'm trying to implement an open/close rule for each of the doors I have. Add an open/closed status for each of the doors. Write open and close ...
Mohammad's user avatar
  • 1,212
9 votes
1 answer
10k views

"Hello world!" in Prolog

I have just written the following "hello world" program in Prolog. Although it is as simple as it can be, I would like your feedback as Prolog is very new to me. ...
Martin Thoma's user avatar
  • 1,235
6 votes
1 answer
169 views

Sample n items without replacement in Prolog

I have the following Prolog code which samples n items from a list without replacement: ...
S0rin's user avatar
  • 205
4 votes
1 answer
91 views

Implementing a zipwithindex predicate in prolog

I want to implement a predicate of the form: zipwithindex(?List1, ?List2) which is true when the elements of List2 are the ...
S0rin's user avatar
  • 205
4 votes
2 answers
4k views

Prolog Sudoku solver taking too long

I was wondering if there's any way I can improve this code's execution. I want to use it on a 9*9 grid, but it takes too long to solve this as 4*4. This program takes as input a 4*4 matrix ...
user1730250's user avatar
5 votes
1 answer
1k views

Complete graph traversal algorithm in Prolog

Given a table, I want to explore all possible transition between the elements in the table. ex: for a table with size 3 [0,1,2], the output of the algorithm should be 0->1, 1->0, 0->2, 2->1, 1->2, 2->...
MortenM's user avatar
  • 153
1 vote
2 answers
3k views

Range of all variables in List in Prolog

I have a list in Prolog like this: Puzzle = [ A1, A2, A3, B1, B2, B3, C1, C2, C3 ]. And I want every variable in the list to be a number ...
Aerus's user avatar
  • 113
1 vote
1 answer
340 views

Bibtex reader using Prolog

I am trying to learn Prolog, and wrote a bibtex reader using GNU Prolog. I would like some feedback on my code in terms of: the way I write Prolog, and how it can be improved. Gist ...
Rahul Gopinath's user avatar