Word Transformation Puzzle – Part 2: Implementation in Common Lisp

Written by on January 4, 2025 in LISP, Programming with 0 Comments

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:

Getting Word Neighbors

Getting Word Neighbors

We then use this function to find the chain:

Finding Word Chain

Finding Word 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:

Program Output

Program Output

As you can see, the implementation is quite straightforward. You can download the code here.

Have a wonderful 2025!

Tags: ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top