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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:34:16+00:00 2026-06-03T20:34:16+00:00

Ok, so I am trying to write a backtracking algorithm that can take input

  • 0

Ok, so I am trying to write a backtracking algorithm that can take input like:

0 2 3 1 (top-right location, length, horizontal or vertical)
1 0 4 0
2 2 4 0
1 3 3 1
top (the actual words)
that
toga
cat

And spit out a crossword, like:

**c***
that**
**toga
***p**

The code I have thus far is:

//prints the puzzle
let printPuzzle (puzzle : char[,]) =
    printfn "%s" ""
    printfn "%A" puzzle
    printfn "%s" ""

//checks if the words fits
let rec doesItFit place (puzzle : char[,]) (word : seq<char>) =
    let (row, col, length, isVertical) = place
    if length <> (Seq.length word) then
        (puzzle, false)
    else
        match (Seq.toList word) with
        | [] -> (puzzle, true)
        | letter::rest ->
            printfn "%c" letter
            printPuzzle puzzle
            if isVertical = 0 then
                if puzzle.[row, col] = '*' || puzzle.[row, col] = letter then
                    puzzle.[row, col] <- letter
                    doesItFit (row, col+1, length-1, isVertical) puzzle rest 
                else
                    (puzzle, false)
            else
                if puzzle.[row, col] = '*' || puzzle.[row, col] = letter then
                    puzzle.[row, col] <- letter
                    doesItFit (row+1, col, length-1, isVertical) puzzle rest   
                else
                    (puzzle, false)

//the actual backtracking algorithm... goes through all places and all words
//trying to make stuff fit
let rec sort words places (puzzle : char[,]) =
    match places with
    | [] -> (puzzle, true)
    | place::rest ->
        let rec checkWords words place puzzle =
            match words with
            | [] ->
                printfn "%s" "failure, backtracking" 
                puzzle, false
            | word::otherWords ->
                let attempt = doesItFit place puzzle word
                if snd attempt then
                    printfn "%s" "success, entering if block"
                    let nextLevel = sort words rest (fst attempt)
                    if (snd nextLevel) then
                        nextLevel
                    else
                        checkWords otherWords place puzzle
                else
                    checkWords otherWords place puzzle
        checkWords words place puzzle

//line for testing
printPuzzle (fst (sort ["cat"; "that"; "toga"; "top"] [(0, 2, 3, 1); (1, 0, 4, 0); (2, 2, 4, 0); (1, 3, 3, 1)] (Array2D.create 6 6 '*')));;

Here is the output from running the test line:

c

[['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

a

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

t

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['*'; '*'; 'a'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

success, entering if block
t

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['*'; '*'; 'a'; '*'; '*'; '*']
 ['*'; '*'; 't'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

h

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; '*'; 'a'; '*'; '*'; '*']
 ['*'; '*'; 't'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

a

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; '*'; '*'; '*']
 ['*'; '*'; 't'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

t

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; '*'; '*'; '*']
 ['*'; '*'; 't'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

success, entering if block
t

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

h

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

a

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; 'h'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

t

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; 'h'; 'a'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

success, entering if block
t

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; 'h'; 'a'; 't']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

o

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; 'h'; 'a'; 't']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

failure, backtracking
t

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; 'h'; 'a'; 't']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

o

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; 'h'; 'a'; 't']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

failure, backtracking
t

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; 'h'; 'a'; 't']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

o

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; 'h'; 'a'; 't']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

failure, backtracking
t

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; 'h'; 'a'; 't']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

failure, backtracking

[['*'; '*'; 'c'; '*'; '*'; '*']
 ['t'; 'h'; 'a'; 't'; '*'; '*']
 ['*'; '*'; 't'; 'h'; 'a'; 't']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']
 ['*'; '*'; '*'; '*'; '*'; '*']]

I think my problem is that I’m not sure how immutability works in F#. It seems from what my program does that when the puzzle is passed in after a failed attempt, the puzzle has been modified. That does not make much sense to me, as I thought F# would not let it be modified. What I would like is an explanation of why this code uses the modified puzzle when testing a word after backtracking out, rather than the original, unmodified puzzle.

  • 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-03T20:34:17+00:00Added an answer on June 3, 2026 at 8:34 pm

    Your code uses mutable array char[,] to represent puzzle, so when you mutate it in the doesItFit function (using the <- operator), you are actually changing the state of the object. When the code returns from the doesItFit function, the array has changed.

    There are essentially three ways to solve the problem:

    • Use immutable types to represent puzzles – if you use F# list (list of lists) to represent puzzles, then you cannot mutate it (because lists are immutable) and so you don’t get into this situation

    • Clone objects as needed – you don’t have to use immutable types to write code in a functional style. Just clone the array before you pass it to a function that may mutate it. You can use Array.copy for that.

    • Restore the state before backtracking – when the function fails, it should undo all modifications
      (This is not a purely functional approach, but it might be more efficient – think of it as optimization)

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

Sidebar

Related Questions

Trying to write a chat, like on facebook, I wondered if two clients can
Trying to write a code that searches hash values for specific string's (input by
Trying to write a regex that can parse a full name and split it
Trying to write a short method so that I can parse a string and
I'm trying write a code that can the set property value through the lambda
I am trying write a function that generates simulated data but if the simulated
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write a code at the moment that basically tests to see if
Im trying to write a single byte at a certain location in a file.
im trying to write an app that will display a list off lines from

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.