O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using the Search

OK, let’s plug the word-chain code into the preceding and see what happens; for example, let’s define the following and run word_chain "lead" "gold".

 word_chain a b = breadth_first_search (\w -> w == b) one_step a

You should see the word “gold” popping out every few seconds, which corresponds to each successful path being found. (There might be a short delay while the dictionary is loaded into memory on the first run.)

Oh. This confirms that paths exist, but we really want to know the intermediate steps. The way to get those is to change the state type from a String to something that keeps track of where it’s been—in other words, a list of previous words. The following code lightly wraps up the basic code to maintain this ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required