
CS 415: Programming Languages
Homework 2: Ocaml
Due Friday, 23 September by 10 a.m.
For this homework, you will need to write three Ocaml functions that deal with DFAs and NFAs:
• dfasimulate: given a DFA and input, will return the state transition of the progress through the
DFA, and true or false if the DFA ended in a final state after reading in all the input
• nfasimulate: same as dfasimulate, but for an NFA (this function can’t use nfa2dfa, however)
• nfa2dfa: given an NFA, this function will convert it to a DFA
The only requirements are:
• The types of the functions must be as specified below, to standardize the specification for the
FAs, and thus enable us to test your code
• You may NOT use any iterative programming, objects, or functions that have side-effects (this
includes print statements) even though Ocaml allows this – the homework must use “pure”
functions and recursion only (you are welcome to have these aspects in there while you are
developing – just not in the final submission)
• You may only use the functions in the List and String modules as well as the standard OCaml
functions
FA specification
All states and transitions in the FAs will have string names. You can assume that the states we
use in our test FAs will only have state names made of digits and/or letters (although the transitions may
include punctuation). The input for both types of FAs will be a string list, where the various
elements of the list are the transitions the FA should take.
The type for both FAs must be string * (string * char * string) list * string list.
The first part of the tuple, the string, is the initial (or current) state of the FA. The second part of the
tuple, the (string * string * string) list, contains a list of all the transitions of the FA. A
transition consists of an initial state, a symbol, and the destination state. The final part of the tuple, the
string list, is a list of the final states. Note that the FA format is the same for both DFAs and NFAs.
Also note that both state names and transitions can be strings of length greater than 1. You can assume
there will be no common names between the states and transitions.
Sample OCaml code for a DFA and an NFA are given below, and more will be available on the
website.
Function requirements
The two simulate functions must take in two parameters each: the FA (described above), and a
list of transitions to take (as a string list). You are welcome to have as many other functions in your
submission file as needed. Both simulate functions return a tuple of type string list * bool. The
string list is the list of all the states visited. The bool is whether the FA is in a final state after
reading the input. Thus, both simulate functions must have the following type: string * (string *