--DICTIONARY-- --NEED TO HAVE A FILE CALLED "dict.txt" DEFINED IN THE LOCAL DIRECTORY. --NOVEMBER 21, 1999 --dict.hs------------------------------------------------------------- module Dictionary where import Prelude import IO import List import "words" import "fixwords" dictFile = "dict.txt" --This is the file name of the dictionary file. main = getFileName >>= \file -> loadDict dictFile >>= \dict -> sortDict dict >>= \dict -> sortDict dict >>= \dict -> checkFile file dict >>= \dict -> saveDict dict >> putStr"\nThank you for using this dictionary." --Loads a text file f and checks if its words are in the dictionary d. checkFile :: String->[Word]->IO [Word] checkFile f d = do{ ck<-loadDict f; d<-checkAll ck d; return d; } --Checks if words in the given list are in the dictionary d. --When a word is not in d, the user is asked if the word should be added. --The dictionary is returned with any new words added. checkAll :: [Word]->[Word]->IO [Word] checkAll [] d = return (d) --If the wordlist is empty, then return. checkAll (w:ws) d |null w = checkAll ws d --fixWord can make given word [] |w `elem` d = checkAll ws d --if w in d, check the rest |otherwise = do{ d<-questionWord w d; checkAll ws d; --Gotta love recursion!!! } --Asks the user if the word w is to be added to the dictionary d. --If the user answers 'y', w is added to d. --Takes a single word as the first parameter, and the dictionary --as the second. The dictionary is returned with any new words --added, and sorted. questionWord :: Word->[Word]->IO [Word] questionWord w d = do{ putStr ("\n '"++w++"' is not in the dictionary! Add it? (y/n) ->"); getChar>>= (\y_n->return(addWord y_n w d)) } --If c is the character 'y' or 'Y', adds word w to dictionary d and returns --d after sorting. Otherwise, just returns d. addWord ::Ord a => Char -> a -> [a] -> [a] addWord c w d |c=='Y'||c=='y' = sort(d++[w]) |otherwise = d --Sorts the dictionary sortDict ::(Monad a, Ord b) => [b] -> a [b] sortDict d = return (sort d) --Saves the dictionary saveDict :: [Word]->IO() saveDict d = openFile dictFile WriteMode>>= (\handle->hPutStr handle (unwords d)) --Reads the dictionary file. --loadDict ::String->IO [Word] loadDict file = openFile file ReadMode>>= (\handle->hGetContents handle >>= (\line->return(splitWords line))) --This long line uses lamba abtractions to open a file, read its contents, --and split the contents into words. There are basically three parts to --this line: --"openFile file ReadMode" -- This opens a file for read mode, and creates a handle. --"\handle->hGetContents handle" -- The variable handle is defined by the previous function call -- (openFile). hGetContents takes a file handle as a parameter -- and returns a string containing all of the characters in the file. --"\line->return(splitWords line)" -- The variable line is what is returned from hGetContents. -- The rest of this part just returns the [word] list by -- making a call to splitWords with the whole file. --Returns a file name entered by the user. getFileName ::IO String getFileName = do { putStr "Check what file: "; line<-getLine; return line; }