(* Matt Franchi // CPSC 3520 // SDE 1 // Summer 2020 *) (* Problem 1: First Duplicate in a List *) (* Prototype: first_duplicate alist returns -10000 if there are no duplicates in integer list alist otherwise first duplicate in the integer list *) (* Signature: val firstduplicate: int list -> int = <fun> *) let rec first_duplicate = function alist -> if List.mem (List.hd alist) (List.tl alist) == true then List.hd alist else if List.tl alist == [] then -10000 else first_duplicate (List.tl alist);; (* Problem 2: First Non-Repeating Element in a List *) (* Helper Function - tests to see if given list is of size 1 a.k.a. the list is about to be empty after calling List.tl *) let list_countdown = function alist -> if List.tl alist != [] then 0 else 1;; (* Prototype: first_nonrepeating alist "Find and return the first non-repeated element in the *entire* list" returns -10000 if no non-repeated element in integer alist otherwise first non-repeating element in alist *) (* Signature: val first_nonrepeating : int list -> int = <fun> *) let rec first_nonrepeating = function alist -> if List.mem (List.hd alist) (List.tl alist) == false then List.hd alist else if list_countdown(List.tl alist) == 1 then -10000 else first_nonrepeating ((List.tl alist)@[List.hd alist]);; (* Problem Three: The Sum of 2 Problem *) (* Prototype: sumofTwo(a,b,v) *) (* Signature: val sumOfTwo : int list * int list * int -> bool = <fun> *) let rec sumOfTwo = function (a,b,v) -> if a == [] || b == [] then false else if List.hd a + List.hd b == v then true else sumOfTwo(List.tl a, b, v) || sumOfTwo(a, List.tl b, v);; (* Problem Four: CYK Parsing Algorithm-Inspired Problem *) (* Helper Function - make_decreasing_sublist n *) (* n is the initial list value, continues to 1 *) let rec make_decreasing_sublist = function n -> if n>1 then (n-1)::make_decreasing_sublist(n-1) else [];; (* Helper Function - make_increasing_sublist (i,n) *) (* i is the initial list value, n is the length of list *) let rec make_increasing_sublist = function (i,n) -> if n > 1 then i::make_increasing_sublist(i+1,n-1) else [];; (* Helper Function - pairwise alista alistb *) (* concatenates two lists alista alistb -> (a, b) *) let rec pairwise alista alistb = match alista, alistb with | [], _ -> [] | _, [] -> [] | hd :: tl, hd2 :: tl2 -> (hd, hd2) :: (pairwise tl tl2) (* Prototype: cyk_sublists n *) (* Signature: val cyk_sublists : int -> (int * int) list = <fun> *) let rec cyk_sublists = function n -> (pairwise (make_increasing_sublist(1,n)) (make_decreasing_sublist(n)));;
Write, Run & Share OCaml code online using OneCompiler's OCaml online compiler for free. It's one of the robust, feature-rich online compilers for OCaml language, running on the latest version 4. Getting started with the OneCompiler's OCaml compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as OCaml
. OneCompiler also has reference programs, where you can look for the sampleprograms and start coding.
OCaml is general purpose programming language with more importance to safety and expressiveness. With it's advanced type system, it helps to catch the mistakes in an efficient way. Hence this is used to develop applications/environments where a single mistake can cost millions and speed matters. It has good community support and rich set of development tools and libraries.
Classification | Data types |
---|---|
Basic data types | integers, floating point numbers, booleans, characters, strings |
Sophisticated data types | tuples, arrays, lists, sets, hash tables, queues, stacks, data streams |
OCaml allows users to define new data types.
Variable is a name given to the storage area in order to manipulate them in our programs.
let varible-names = value
If is performed when you need to choose expression based on a boolean-condition.
if boolean-condition then (* code if condition is true *)
if boolean-condition then (* code if condition is true*) else (* code if condition is false*)
While is used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while boolean-condition do
(* code *)
done
For loop is used to iterate a set of statements for specific number of items.
for var = start-value to end-value do
(* code *)
done
for var = start-value downto end-value do
(* code *)
done