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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:10:00+00:00 2026-05-30T12:10:00+00:00

I have written a sudoku solver in Haskell. It goes through a list and

  • 0

I have written a sudoku solver in Haskell. It goes through a list and when it finds ‘0’ (an empty cell) it will get the numbers that could fit and try them:

import Data.List (group, (\\), sort)
import Data.Maybe (fromMaybe)

row :: Int -> [Int] -> [Int]
row y grid = foldl (\acc x -> (grid !! x):acc) [] [y*9 .. y*9+8]
    where y' = y*9
column :: Int -> [Int] -> [Int]
column x grid = foldl (\acc n -> (grid !! n):acc) [] [x,x+9..80]
box :: Int -> Int -> [Int] -> [Int]
box x y grid = foldl (\acc n -> (grid !! n):acc) [] [x+y*9*3+y' | y' <- [0,9,18], x <- [x'..x'+2]]
    where x' = x*3

isValid :: [Int] -> Bool
isValid grid = and [isValidRow, isValidCol, isValidBox]
    where isValidRow = isValidDiv row
          isValidCol = isValidDiv column
          isValidBox = and $ foldl (\acc (x,y) -> isValidList (box x y grid):acc) [] [(x,y) | x <- [0..2], y <- [0..2]]
          isValidDiv f = and $ foldl (\acc x -> isValidList (f x grid):acc) [] [0..8]
          isValidList = all (\x -> length x <= 1) . tail . group . sort -- tail removes entries that are '0'

isComplete :: [Int] -> Bool        
isComplete grid = length (filter (== 0) grid) == 0

solve :: Maybe [Int] -> Maybe [Int]
solve grid' = foldl f Nothing [0..80]
    where grid = fromMaybe [] grid' 
          f acc x
            | isValid grid = if isComplete grid then grid' else f' acc x
            | otherwise    = acc
          f' acc x 
            | (grid !! x) == 0 = case guess x grid of 
                Nothing -> acc
                Just x -> Just x
            | otherwise        = acc

guess :: Int -> [Int] -> Maybe [Int]
guess x grid
    | length valid /= 0 = foldl f Nothing valid
    | otherwise         = Nothing
    where valid = [1..9] \\ (row rowN grid ++ column colN grid ++ box (fst boxN) (snd boxN) grid) -- remove numbers already used in row/collumn/box
          rowN = x `div` 9 -- e.g. 0/9=0 75/9=8
          colN = x - (rowN * 9) -- e.g. 0-0=0 75-72=3
          boxN = (colN `div` 3, rowN `div` 3)
          before x = take x grid
          after x = drop (x+1) grid
          f acc y = case solve $ Just $ before x ++ [y] ++ after x of
            Nothing -> acc
            Just x -> Just x

For some puzzles this works, for example this one:

sudoku :: [Int]
sudoku = [5,3,0,6,7,8,0,1,2,
          6,7,0,0,0,0,3,4,8,
          0,0,8,0,0,0,5,0,7,
          8,0,0,0,0,1,0,0,3,
          4,2,6,0,0,3,7,9,0,
          7,0,0,9,0,0,0,5,0,
          9,0,0,5,0,7,0,0,0,
          2,8,7,4,1,9,6,0,5,
          3,0,0,2,8,0,1,0,0]

Took under a second, however this one:

sudoku :: [Int]
sudoku = [5,3,0,0,7,0,0,1,2,
          6,7,0,0,0,0,3,4,8,
          0,0,0,0,0,0,5,0,7,
          8,0,0,0,0,1,0,0,3,
          4,2,6,0,0,3,7,9,0,
          7,0,0,9,0,0,0,5,0,
          9,0,0,5,0,7,0,0,0,
          2,8,7,4,1,9,6,0,5,
          3,0,0,2,8,0,1,0,0]

I have not seen finish. I don’t think this is a problem with the method, as it does return correct results.

Profiling showed that most of the time was spent in the “isValid” function. Is there something obviously inefficient/slow about that function?

  • 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-05-30T12:10:01+00:00Added an answer on May 30, 2026 at 12:10 pm

    The implementation is of course improvable, but that’s not the problem. The problem is that for the second grid, the simple guess-and-check algorithm needs a lot of backtracking. Even if you speed up each of your functions 1000-fold, there will be grids where it still needs several times the age of the universe to find the (first, if the grid is not unique) solution.

    You need a better algorithm to avoid that. A fairly efficient method to avoid such cases is to guess the square with the least number of possibilities first. That doesn’t avoid all bad cases, but reduces them much.

    One thing that you should also do is replace the length thing == 0 check with null thing. With the relatively short lists occurring here, the effect is limited, but in general it can be dramatic (and in general you should also not use length list <= 1, use null $ drop 1 list instead).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a homework assignment to write a multi-threaded sudoku solver, which finds all
I have written an AIR Application that downloads videos and documents from a server.
I have written an AppleScript which when supplied with a Windows network link, will
I have written a DLL that uses MS Word to spell check the content
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
I have written a watir script that downloads files. One of the files it
I have written a xml/php document that is pulling from a Magento Commerce database,
I have written the following code. I know that a higher order function is
I have written a WCF service with the REST template that has the defaultOutgoingResponseFormat
Have written a method; when I try to run it, I get the error:

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.