In the last article I discussed an interesting word puzzle and showed how to solve it using Prolog. Here is the problem statement:
“You are given two words of the same length. You have to transform the first word into the second word, by changing only one letter at a time. The additional constraint is that each intermediate step must be a valid word.”
As I had mentioned in that article, the solution can be implemented in any programming language. In today’s article, I want to share the Lisp implementation.
Let us start with a simple dictionary. We need a helper function that returns all possible “next” words, given one word. Here is the code:
We then use this function to find the chain:
The function returns “Nil” if a valid chain cannot be found, else it returns the successive words, starting from the initial word, as a list.
Here is the output for a few start and end words:
As you can see, the implementation is quite straightforward. You can download the code here.
Have a wonderful 2025!
Recent Comments