Question: OCaml Write a function tokenize : string -> token list such that a call (tokenize string) returns a list of tokens, where the type of

OCaml

Write a function tokenize : string -> token list such that a call (tokenize string) returns a list of tokens, where the type of tokens is defined as:

type token = And | Or | If

Your function should convert the string "&&" to the token And; the string "||" to the token Or; and the string "if" to the token If. For example

(tokenize " || if &&")

should return the list [Or; If; And]. Note that white space in the form of the space character _ should be ignored. You can assume that the only space characters in your input are the ones that occur in &&, if, and ||, and you don't have to handle incomplete or partial tokens. For example, your code is allowed to return any list you like on the input &|. Hint: Use the Lib.explode function.

Problems 6 and 7 relate to binary trees with interior nodes Arrow and with leaf nodes that are either the constant C or a variable Var 0, Var 1 etc.

type t = C | Var of int | Arrow of { from : t ; too : t }

Note that the 'too' field should probably be called 'to' (that is the names would be from and to), but 'to' is already a reserved keyword in OCaml, so we cannot use it for a field name.

With this definition, we can express trees as follows.

# C;; - : t = C # Var (2 + 3);; - : t = Var 5 # Arrow { from = C; too = C};; - : t = Arrow {from = C; too = C}

We'll use the following examples.

let t0 = Arrow { from = Var 0; too = Arrow { from = C; too = C }} let t1 = Arrow { from = C; too = Var 1 } let t2 = Arrow { from = t1; too = Arrow { from = C; too = C }} let t3 = Arrow { from = C; too = t0 } t0 = -> t1 = -> t2 = -> t3 = -> / \ / \ / \ / \ v0 -> C v1 t1 -> C t0 / \ / \ C C C C

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!