SHARE

TWEET

# recursive types exercises

a guest
May 1st, 2019
94
Never

**Not a member of Pastebin yet?**

**, it unlocks many cool features!**

__Sign Up__- (*In this exercise, we implement a queue with a pair of two lists (front, back) such that front @ List.rev back represents the sequence of elements in the queue.
- Write a function is_empty : queue -> bool such that is_empty q is true if and only if q has no element. *)
- let is_empty (q : queue) : bool =
- match q with
- | ([], []) -> true
- | _ -> false
- ;;
- (*Write a function enqueue : int -> queue -> queue such that enqueue x q is the queue as q except that x is at the end of the queue. *)
- let rec append i l =
- match l with
- | [] -> [i]
- | hd :: tl -> hd :: (append i tl);;
- let enqueue (x: int) (q: queue) : queue =
- match (x, q) with
- | (_, (a, b)) -> (a, (append x b));;
- (* Write a function split : int list -> int list * int list such that split l = (front, back) where l = back @ List.rev front and the length of back and front is List.length l / 2 or List.length l / 2 + 1 *)
- let rec take (x: int) (l: 'a list) =
- match x with
- | 0 -> []
- | x -> match l with
- | [] -> failwith "take"
- | hd :: tl -> hd :: (take (x - 1) tl);;
- let rec drop (x: int) (l: 'a list) =
- if x = 0 then l
- else
- match l with
- | [] -> failwith "drop"
- | hd :: tl -> drop (x - 1) tl;;
- let split (x: int list) : int list * int list =
- let left = (drop (List.length x / 2) x) in
- let right = (take (List.length x / 2) x) in
- ((List.rev left), right);;

RAW Paste Data

We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.