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
search
function that takes a binary search tree and a key as arguments, and returnstrue
if the key is found, andfalse
otherwise. Move-to-front encoding: You are given a list
table
initially 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)-1
do- let
c
be the ith character of the string s - Find the index of
c
in the currenttable
, and add it to the output list. - Transform
table
by bringingc
to the front of the list, pushing back the elements in table from the first position to the index ofc
back by one position. Elements afterc
intable
remain 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'
.