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

  • Home
  • SEARCH
  • 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 7711127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:17:51+00:00 2026-06-01T01:17:51+00:00

The question is as described here: Knight's Shortest Path Chess Question . I tried

  • 0

The question is as described here: Knight's Shortest Path Chess Question. I tried to solve it using an intuitive recursive method, as follows:

let is_in_list x s = List.exists (fun y -> if y = x  then true else false) s;; (* find out if an element belongs to a list *)

let next_step n = [n+10;n-6;n+6;n-10;n+17;n-17;n+15;n-15];;

let rec legal_next_step = function
  |[]->[]
  |x::s-> 
    if x<0 or x>63 then legal_next_step s
    else x::legal_next_step s;;

let find_next_step n = legal_next_step (next_step n);; (* here is to find all the valid next moves and put them into a list*)

let rec min s = 
  let rec loop result = function
    |[]->result;
    |x::s-> 
      if x < result then loop x s
      else loop result s in
  loop (List.hd s) s;;

let rec find_shortest_path n m =
  if is_in_list m (next_step n) then 1
  else
    1 + min (List.map (fun x -> find_shortest_path x m) (find_next_step n)) ;;

This causes a “stack overflow” issue. Why is that? Is my program creating too many layers of recursive call in the stack? Or something is terribly wrong with my code.

  • 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-01T01:17:52+00:00Added an answer on June 1, 2026 at 1:17 am

    You have to understand that when you write

    List.map (fun x -> find_shortest_path x m) (find_next_step n))
    

    You will literally compute all “shortest path from here” from all possible neighbors — then compute the minimum of all those results.

    So there is an infinite loop in your code: if you start from position A and try to compute the shortest path from one of its neighbors B, then find_shortest_path called from B will inevitably try to see how long the path would be if his first move was to go back to A. So, among all other possible moves that will also be tried, you will compute the “length” of the loop A-B-A-B-A-B..., that is, loop endlessly.

    There are several ways to avoid that issue (that is not related to OCaml programming per se, it’s an error in your program logic that would be manifest in any language):

    • use a breadth-first search instead of a depth-first-search, so that you incrementally explore all paths of a given length, stopping at the smallest winning path you find; if you want, this corresponds to explore all paths in parallel, so it’s not an issue to have infinite paths as long as you stop (because you have found another solution) before trying to search the whole infinite path

    • mark the places you’ve already visited, so as to not “go back” (this will never be the shortest way to reach your destination)

      • if you use depth-first search this is delicate because those marks must be local to a search (you cannot simply mutate a matrix of booleans); for example, you could add an int list parameter to your find_shortest_path functions, that would be the list of places that are part of the currently explored path; before trying to compute the shortest path from a possible neighbors, check that it is not in this list. For something more efficient, you can use a set (module IntSet = Set.Make(struct type t = int let compare = Pervasives.compare)) (logarithmic, instead of linear, membership test), or use a mutable boolean matrix where you are careful to backtrack state changes when you choose a different path.

      • if you use breadth-first search, you can use a global boolean matrix of “places you’ve already visited”, because you simultaneously explore all paths upto a given length; so if you encounter a place that is already marked as visited, you know that another path visited it in an earlier time, so it is already ahead of you and there is no point trying to get a shortest path from there.

    So the short answer is: you should learn about breadth-first search.

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

Sidebar

Related Questions

I'm using the method described by Josh in this question to add a toolbar
I've written an application which allows audio fingerprinting using the method described here .
I have the same question problem as described here How to purge a cached
Here is my current question: I'm guessing that my problem (described below) is being
I want to do what's described in question 724043 , namely encode the path
I believe this is a beginner's CSS question. I am utilizing the method described
I have organised my junit tests using one inner class per method as described:
This question is another problem im having with my application which is described here
Using the tiny Diggit/Blog feature of StackOverflow described here : I would like to
I'd like to do the same thing as is described here , but using

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.