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

Dictionary Filtering

We’re not quite finished with the program that has been the motivator of this chapter. Now for the dictionary filtering. We’ll do this bit by loading a list of words into a hash-like structure and then testing whether a word is present. Here’s the code. I’m sure you could write this code in several brilliant ways, and it won’t advance our understanding of Haskell or functional programming much to examine it in detail, so I’ll be brief.

 -- import lines should go at the top
 import Data.Set(Set, fromDistinctAscList, member)
 import System.IO.Unsafe(unsafePerformIO)
 
 dict = fromDistinctAscList
  $ lines
  $ unsafePerformIO (readFile "fours.txt")
 
 is_valid_word w = member w dict

The previous code loads a list of words ...

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