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 8899315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:49:34+00:00 2026-06-15T00:49:34+00:00

Alright so I am attempting to create a Sudoku Solver in Haskell but I’m

  • 0

Alright so I am attempting to create a Sudoku Solver in Haskell but I’m getting an error saying that I couldn’t match the expected type [[Int]] with the actual type IO (). Here is my attempt at the recursive solver, the error message, and other relevant pieces of code:

Recursive Solver Attempt:

test i j q s_board = if ((valid_row i q s_board )&&(valid_column j q s_board)&&  (valid_sub_board i j q s_board)) then (solve (set_value i j q s_board)) else s_board

foo i j s_board = if ((get_value i j s_board) == 0) then [test i j q s_board | q <- [1..9]] else s_board  

solve s_board = if (full_board s_board) then (print_board s_board) else [foo i j s_board | i <- [0..8], j <- [0..8]]

This question would be extremely long if I included all the definitions for the valid column, row, etc. functions, but I’ve checked to make sure those work. With this code I’m getting the following error message:

 Couldn't match expected type `[[Int]]' with actual type `IO ()'
 In the return type of a call of `print_board'
 In the expression: (print_board s_board)
 In the expression:
   if (full_board s_board) then
       (print_board s_board)
   else
       [foo i j s_board | i <- [0 .. 8], j <- [0 .. 8]]

Also here is the code that I’m using to print my board:

 -- showLine: this function provides formating for a single row 
showLine :: [Int] -> String
showLine = intercalate " | "
         . map unwords
         . chunksOf 3
         . map show

-- showBoad: this function provides formating for the entire board       
showBoard :: [[Int]] -> String
showBoard = intercalate "---------------------\n"
          . map unlines
          . chunksOf 3
          . map showLine


-- print_board: this function is meant to print out the entire board 
print_board :: [[Int]] -> IO ()   
print_board s_board = putStrLn $ showBoard s_board

Do you guys see what’s the problem with what I have so far. I’m totally new to Haskell and this is the first real program that I’ve attempted. Any help would be greatly appreciated.

  • 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-15T00:49:35+00:00Added an answer on June 15, 2026 at 12:49 am

    The two branches of an if must have the same types, but in

    if (full_board s_board) then
        (print_board s_board)
    else
        [foo i j s_board | i <- [0 .. 8], j <- [0 .. 8]]
    

    the True branch has type IO (), while the False branch is a list of things (boards, in this case probably), so the type is [a], if foo :: Int -> Int -> [[Int]] -> a.

    You should separate concerns, the recursive backtracking should give you a list of (full) boards, the printing should be done from another context that calls solve.

    My suggestion would be something along the lines of

    type Grid = [[Int]]
    
    solve :: Grid -> [Grid]
    solve s_board = if full_board s_board
                        then [s_board]    -- full means no branches, singleton list
                        else concat [foo i j s_board | i <- [0 .. 8], j <- [0 .. 8]]
    
    test :: Int -> Int -> Int -> Grid -> [Grid]
    test i j q s_board
        | valid_row i q s_board
          && valid_column j q s_board
          && valid_sub_board i j q s_board = solve $ set_value i j q s_board
        | otherwise = []
    
    foo :: Int -> Int -> Grid -> [Grid]
    foo i j s_board
        | get_value i j s_board == 0 = concat [test i j q s_board | q <- [1 .. 9]]
        | otherwise                  = []
    

    so each of these functions returns a list of Grids, and a dead-end is pruned by returning an empty list of solutions that can be reached from the current grid. When there is no dead-end yet diagnosed, all allowed combinations are tried.

    You could then have

    solve_and_print :: Grid -> IO ()
    solve_and_print s_board = case solve s_board of
                                [] -> putStrLn "Sorry, that board had no solution."
                                (g:_) -> print_board g
    

    But, that would produce the same solutions multiple times, and be terribly inefficient for an exhaustive search, since evry combination of guesses would be made in all possible orders.

    So, let’s take a look at how we could make it more efficient. We can prune the permutations and repetitions of solutions in the result list, if we have an algorithm to choose the next position at which we guess a value. The simplest such algorithm is to pick the first free cell. So let s write a function to find the free cells of a grid.

    free_cells :: Grid -> [(Int,Int)]
    free_cells s_board = [(i,j) | i <- [0 .. 8], j <- [0 .. 8], get_value i j s_board == 0]
    

    With that, we also have a test whether the grid is full, full_board = null . free_cells, by the way. So we can start our solving

    solve :: Grid -> [Grid]
    solve s_board
        | null frees = [s_board]
        | otherwise  = guesses s_board (head frees)
          where
            frees = free_cells s_board
    

    Next, we find the values we might place in the cell (i,j),

    possible_values :: Grid -> (Int, Int) -> [Int]
    possible_values s_board (r,c) = [q | q <- [1 .. 9], isPossible s_board q]
      where
        isPossible v = valid_row r v s_board
                       && valid_column c v s_board
                       && valid_sub_board r c v s_board
    

    and place them in the cell and proceed further

    guesses :: Grid -> (Int, Int) -> [Grid]
    guesses s_board (r,c) = [solution | v <- possible_values s_board (r,c)
                                      , solution <- solve $ set_value r c v s_board]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright y'all. I am attempting to write a bit of code that places typographic
Alright folks, I'm getting a syntax error on a line of code and I
First, the formula I'm currently using: =countifs('page1'!AF:AF,$L6,'page1'!AA:AA,=&RIGHT(M$2,3))+countifs('page1'!AF:AF,$L6,'page1'!AB:AB,=&RIGHT(M$2,3))+countifs('page1'!AF:AF,$L6,'page1'!AC:AC,=&RIGHT(M$2,3))+countifs('page1'!AF:AF,$L6,'page1'!AD:AD,=&RIGHT(M$2,3))+countifs('page1'!AF:AF,$L6,'page1'!AE:AE,=&RIGHT(M$2,3))+countifs('page1'!AF:AF,$L6,'page1'!AF:AF,=&RIGHT(M$2,3)) Alright, now, what I'm attempting to match
Alright, so I'm about 90% sure that this isn't possible, but I thought I'd
Alright I've tried to ask this before but I'm not really getting anywhere yet.
Alright, so I'm attempting my hand at some pattern programming in my effort to
Alright so i need to open a .txt file that will be created in
Alright, this is driving me nuts because my regex is working on Rubular, but
Alright so I have no idea how to even begin doing this But basically
Alright. I'm attempting to complete a school assignment and cannot for the life of

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.