Monday, March 16, 2015

HASKELL LOOKUP TABLE DICTIONARY

HASKELL LOOKUP TABLE DICTIONARY

REVISED: Wednesday, January 24, 2024





1. HASKELL LOOKUP TABLE DICTIONARY

Shown below is a simple easy to understand Haskell lookup table dictionary program:

module Rhymes where

import Prelude hiding (Maybe(..), lookup)

{-
Representing failure using Maybe monad.
Using monads for error handling, also known as exception handling.
Data keyword introduces type Maybe a.
a is a place holder a generic variable.
Just and Nothing are constructors of type Maybe.
-}

data Maybe a = Just a | Nothing
deriving (Eq, Ord, Show)

{-
Lookup Table Dictionary.
A lookup table dictionary relates keys k to values v.
First tuple String is the key k.
Second tuple String is the value v.
-}

rhyme :: [(String, String)]
rhyme = [("fleece", "fleas")
           ,("snow", "go")
          ,("walk", "stalk")
          ,("jaws", "pause")
          ,("fair", "there")
          ,("there", "underwear")
          ,("nose", "toes")
  ,("nice", "lice")
  ,("toes", "blows")
          ,("lice", "ice")]

{-
Function for looking up keys k in Lookup Table Dictionary.
Found value v is returned wrapped in a Just.
You could unwrap Maybe with case statements.

rest is rest of Table.

-}

lookup key [] = Nothing   -- Empty String
lookup key ((k, v) : rest) = if key == k then Just v else lookup key rest

{-
No key match default value.
-}

fromMaybe def (Just a) = a
fromMaybe def Nothing = def

{-
getLine can be thought of as a monadic function of type () -> IO String a monadic action which reads a line of text from the terminal, returning the line of text read as a string. However, its type is IO String. 

putStrLn is a function which happens to be in the IO monad and takes a string as input and displays it on the terminal, also outputting a newline character at the end.

-}

main = do
  putStrLn "Type word to rhyme or type exit then press Enter."
  word <- getLine
  if word == "exit"
      then do 
             return ()
      else do  
              let poetry = lookup word rhyme
             print (fromMaybe "Not found! Update Poetry Lookup Table Dictionary!" poetry)
             main    

2. EXAMPLE PROGRAM OUTPUT

Prelude>  :load Rhymes
[1 of 1] Compiling Rhymes            ( Rhymes.hs, interpreted )
Ok, modules loaded: Rhymes.
Prelude>

Prelude>  main
Type word to rhyme or type exit then press Enter.
fleece
"fleas"
Type word to rhyme or type exit then press Enter.
snow
"go"
Type word to rhyme or type exit then press Enter.
exit
Prelude>

3. EXAMPLE POEM

Mary had a little lamb,
its fleas were white as snow.
And everywhere that Mary went,
its fleas were sure to go.

4. lookup AND Maybe a

As you know a lookup table dictionary is a table which relates keys to values. As shown above the lookup function and the Maybe a features work very well with a lookup table dictionary. I was very impressed with how easy it was to create and use a lookup table dictionary.

Please share with us your comments concerning your successful and unsuccessful lookup table dictionary experiences.

5. CONCLUSION

In this tutorial you have been introduced to the Haskell lookup table dictionary.

6. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.