Question: Lists and Recursion 4.5. The language to solve this question is OCaml language. (*>* Task 4.5 *>*) let find2D (f: 'a -> bool) (lines: 'a

Lists and Recursion

4.5. The language to solve this question is OCaml language.

Lists and Recursion 4.5. The language to solve this question is OCaml

(*>* Task 4.5 *>*)

let find2D (f: 'a -> bool) (lines: 'a list list) : (int * int) option = raise ImplementMe ;;

(* assert (find2D (fun _ -> true) [] = None);; assert (find2D (fun _ -> true) [[]; []] = None);; let file = [[1;2;3;4;5]; [5;4;3;2;1]; [10;9;8;7;6]; [2;4;6;8;10;12]] ;; assert (find2D ((=) 5) file = Some (0, 4));; assert (find2D (( 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. 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!