Homework 4
- Write an OCaml function which takes a list of elements and an element as arguments, and returns the index of the last occurrence of the element.
- Define a binary search tree using variant types and product
types. Define a
searchfunction that takes a binary search tree and a key as arguments, and returnstrueif the key is found, andfalseotherwise. Move-to-front encoding: You are given a list
tableinitially set equal to['a';'b';'c';'d';'e';'f';'g';'h';'i';'j';'k';'l';'m';'n';'o'; 'p';'q';'r';'s';'t';'u';'v';'w';'x';'y';'z']and a strings.Move-to-front encoding works as follows.
- Initially set the output list to empty.
- For i from 0 to
(len s)-1do- let
cbe the ith character of the string s - Find the index of
cin the currenttable, and add it to the output list. - Transform
tableby bringingcto the front of the list, pushing back the elements in table from the first position to the index ofcback by one position. Elements aftercintableremain unchanged.
- let
- Return the output list.
In OCaml, you can get the character at the first position of a string
s, for example, by the expression(String.get s 0).- You are given a Tic-Tac-Toe game board in the form of a list of 3
rows, where each row is a list of 3 characters. Each character may
be a
' ', a'o'or a'x'. Given such a board, output true if the game is a winning position for the player playing'x'.