Exercises #3

  1. Write a procedure to check the spelling of a word:
    # spell :: filedescriptor X string -> boolean
    proc spell {fd word} {...}
    It should take a file descriptor, which refers to an opened dictionary file, and a string, which is the word to be checked, and it should return 1 if the word is spelled correctly, or 0 if not.

    We define spelled correctly to mean that the word is listed in the dictionary file. We ignore the issues of plurals, etc: if the exact word is not found in the dictionary, it's misspelled.

    We define a dictionary file to be any file that contains words, one per line, which is sorted in ASCII collating order. You may use the file /home/keith/web/tcl-course/words for testing.

    Hint: this proc is extremely easy to write. If you disagree, you're approaching it wrong.

  2. There is a standard dictionary file included with Unix systems: /usr/dict/words. Why doesn't this file work if you use it with your spell proc? What type of changes would you have to make to your code to be able to use this file as is?
  3. Write a spelling checker. This should be an actual complete program, using the spell proc you coded above (and my dictionary). Your program should read a file to be spell checked on standard input, and write to standard output any mispelled words, one per line.

    You can use any rule you like for deciding what makes a word in the input file. I recommend choosing a rule that exploits existing Tcl commands.

    To run your program (assuming it's called spell.tcl), you can say, from the Unix shell:

    megatcl -f spell.tcl
    and then you can type lines of text to be spelled checked; type C-d alone on a line to terminate. You can also spell check a file by saying:
    megatcl -f spell.tcl < filename

    Don't worry about the problems of uppercase vs lowercase, or punctuation marks. We'll learn how to handle these when we cover string processing.

    Hint: this program, not counting comments, whitespace or lines containing closing braces, and not counting the code for your spell proc, should be about six lines long.

  4. Modify your spelling checker to output an identifying line number with each mispelled word. Your output for each line should be the line number, a tab, and the mispelled word.

    Hint: this should add about two lines to your program.


Keith Waclena
The University of Chicago Library
This page last updated: Wed Aug 17 13:19:52 CDT 1994
This page was generated from Extended HTML by xhtml.