Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7860963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:30:28+00:00 2026-06-02T22:30:28+00:00

I have programmed many things in many programming languages, and Prolog is not one

  • 0

I have programmed many things in many programming languages, and Prolog is not one of those languages. I am supposed to write a program that will solve a sudoku puzzle when given a puzzle representation as input.

My idea:
Store all supplied numbers in a list of lists (list of rows), and brute force the possible options until one fits (I know, it’s not the most elegant, but I don’t have a lot of time to spend writing out the logic to solve the puzzle conventionally). But, being new to Prolog, I am having some difficulty understanding the way of going about things. The input is given in a text file in the form of rows looking like

1-2---34-
-34--1-5-

and so on and so forth. My code to read and print the file is:

readPuzzle(File) :-
  see(File),
  repeat,
  get_char(X),
  (X = end_of_file, !
   ;
   write(X),
   fail
  ),
  seen.

This works all well and good. So let’s say I try to build this list by adding a W = []
just above see(File) and replace write(X) with W = [W|X]. From my experience, this should create a long list of all of the chars supplied from the text file.
It doesn’t.
Someone please either

  • tell me a better more prological way of accomplishing the task ahead of me
  • explain how I can work around this.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:30:29+00:00Added an answer on June 2, 2026 at 10:30 pm

    There are several ways of accomplish the specification reading. If you are willing to reuse your code, I would suggest to use assertz:

    :- dynamic onechar/1.
    
    readPuzzle(File) :-
      see(File),
      repeat,
      get_char(X),
      (X = end_of_file, !
       ;
       asssertz(onechar(X)), % write(X),
       fail
      ),
      seen.
    

    and use

     ...,  findall(L, retract(onechar(C)), Cs), ...
    

    to get the list of chars read, then you will need to split the list in lines, discarding lines’ separators.

    Otherwise, instead of asserting/retracting, we can use service predicate that collects more structured input, and constraints length while reading:

    readPuzzle(File, Puzzle) :-
      see(File),
      length(Puzzle, 9),  % this allocates a list of unbound variables
      maplist(read_a_line, Puzzle),
      seen.
    
    read_a_line(Line) :-
      length(Line, 9),
      maplist(get_char, Line),
      get_char(_). % get rid of nl
    

    For an example of how this works in SWI-Prolog:

    ?- length(L,9),see(user),maplist(get,L),seen.
    |: 987654321
    
    L = [57, 56, 55, 54, 53, 52, 51, 50, 49].
    

    update

    Here is another way, using accumulators to collect structured input and size..

    readPuzzle(File, Puzzle, Length) :-
      see(File),
      readLines([], 0, Puzzle, Length),
      seen.
    
    readLines(SoFar, CountSoFar, Lines, CountLines) :-
      get_char(C), % lookahead
      C \= end_of_file,
      readLine(C, [], Line),
      M is SoFar + 1,
      readLines([Line|SoFar], M, Lines, CountLines).
    readLines(Lines, TotLines, Lines, TotLines).
    
    readLine(C, Line, Line) :-
     C == 10.
    readLine(C, SoFar, Line) :-
     get_char(S),
     readLine(S, [C|SoFar], Line).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a program that will communicate with other .NET programs ...
I am one of those accidental programmer so I don't have that much knowledge
I have 10 dice that are all programmed the same as D1. D1 =
I have a directory that continually fills up with artefact files. Many different programs
I have programmed an emulator, but I have some doubts about how to organizate
I have programmed in HTML, Java, CSS, C++, VB, an Python. I'm looking to
Once I have programmed GUI with Java and have used Form Layouts. Form layout
I have never programmed a gadget for Vista or Seven, but I would like
I have never programmed in visual basic before and my boss just told me
I have a webservice programmed in coldfusion which I'm attempting to consume using c#.net.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.