Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SML Specific Instructions Your code should be written in hw1.sml. Create a hw1 f

ID: 3794476 • Letter: S

Question

SML Specific Instructions
Your code should be written in hw1.sml. Create a hw1 function that takes in two strings as
arguments: the first for the name of input file and second for the name of output file.
Skeleton Code:
fun hw1(inFile : string, outFile : string) =
(* Open input and output le streams, read rst input line from the input le stream *)
(* To read subsequent lines, use of a helper function { see resources on SML File Stream *)
(* Convert the string type of the lines read and alphabet string to list type*)
(* Perform alphabet checks on these lists *)
(* Write the result, true or false, to the output le *)
(* Remember to
ush the outStream, refer resources on TestIO. *)

Write a program in SML

Your program will consist of a function that accepts two strings. Each string is the name of
a file. The first is the name of an input file and the second is the name of an output file. Name
the function hw1. (Note that your program can also make use of other helper functions. Just
make sure function hw1 takes as arguments the input file and output file that are specified in the
program)
A pangram is a sentence that contains all the letters of the English alphabet at least once. For
example, the quick brown fox jumps over the lazy dog is a pangram. The program you are to write
must read in an input file (input.txt - a plain text le which contains 5 sentences), line by line and
check if the line read is a pangram or not. If the sentence read is a pangram, it writes true to the
output file. If it is not, it writes false to the output file.
For example, if input.txt contains:
we promptly judged antique ivory buckles for the next prize.
how quickly daft jumping zebras vex.
pottery is an art.
crazy fredrick bought many very exquisite opal jewels.
mr. dumbledore is a funny name for a dog.
Then your program must output the following to the output.txt:
true
true
false
true
false
NOTE: Output is case sensitive { please use all lower case in the output file. The example
provided here is formatted for human readability. Please look at the sample input
output files to see the precise formatting.

Explanation / Answer

structure CharSet = RedBlackSetFn(struct
type ord_key = char
val compare = Char.compare
end)

val alphabet = CharSet.fromList (explode "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

fun isPanagram s =
let val chars = CharSet.fromList (map Char.toUpper (explode s))
val letters = CharSet.intersection (chars,alphabet)
in CharSet.numItems letters = 26
end;

fun hw1(inFile: string, outFile: string) = let
   val ins = TextIO.openIn inFile
   val outs = TextIO.openOut outFile
   fun loop ins =
       case TextIO.inputLine ins of
           SOME(line) => (TextIO.output1(outs,isPanagram(line)) :: loop ins
           | NONE => []
in
   loop ins before TextIO.closeIn ins
end