Question
Use the Ocaml Language. Believe it or not, possibly the hardest part of writing a compiler is producing good error messages (if you've ever used
Use the Ocaml Language.
Believe it or not, possibly the hardest part of writing a compiler is producing good error messages (if you've ever used a new language or compiler and found the error messages frustratingly unhelpful, this is probably why!) An important part of this is identifying where in the code the error occurs (generally line and column number). Imagine how frustrating the following would be when you're trying to compile a large program: $ ocamlc mycode.ml
Syntax Error.
$ While you won't be finding syntax errors in a program (yet!), you will start by writing a function similar to find above, but which identifies where the found item occurs in a list. It also finds an item in a list of lists, representing a two-dimensional array of characters (like a program written as a text file). Implement the function find2D: ('a -> bool) -> 'a list list -> (int * int) option. If file contains a list of rows, where each row is a list of characters, find2D f file should return Some (x, y) where x is the row and y is the character of the first item for which f returns true, or return None if there is no such item. Both rows and columns (characters) should be indexed from 0. When determining which item is "first", you should read from left to right within each row, then move to the next row (just like reading). As an example,
if file is [[1;2;3;4;5];
[5;4;3;2;1];
[10;9;8;7;6];
[2;4;6;8;10;12]]
then find2D ((=) 5) file = Some (0, 4) because the first 5 appears in row 0 at character 4.
find2D ((=) 15) file = None because 15 does not appear in the file.
Use the base code below:
let find2D (f: 'a -> bool) (lines: 'a list list) : (int * int) option = raise ImplementMe ;;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started