Assuming l is a List and elem is an element, how can I return the last occurence of the element elem in the list l? Also return -1 if the element does not exisit in l. I dont quite understand how to use recursion for iterating through the list...
let rec getLastOccurence l elem =
;;
...
Coming from a Java and C background, grasping some concepts of Ocaml has been quite interesting. One such is, getting a/multiple statement to run inside a for loop.
let test_method (x:vector list)(vec:vector) =
if List.length x != 0 then
{a=0.;b=0.} (* Return a vector of 0,0 *)
else
for i = 0 to List.l...
Did you hear something about any corporate investments or enlargement of OCaml community? (not F# please)
...
For an assignment, i have written the following code in recursion. It takes a list of a vector data type, and a vector and calcuates to closeness of the two vectors. This method works fine, but i dont know how to do the recursive version.
let romulus_iter (x:vector list ) (vec:vector) =
let vector_close_hash = Hashtbl.create 10 in...
Hi,
I have a task to write a (toy) parser for a (toy) grammar using OCaml and not sure how to start (and proceed with) this problem.
Here's a sample Awk grammar:
type ('nonterm, 'term) symbol = N of 'nonterm | T of 'term;;
type awksub_nonterminals = Expr | Term | Lvalue | Incrop | Binop | Num;;
let awksub_grammar =
(Expr,
funct...
To learn the basics of OCaml, I'm solving one of the easy facebook engineering puzzles using it. Essentially, I'd like to do something like the following Python code:
some_str = some_str.strip()
That is, I'd like to strip all of the whitespace from the beginning and the end. I don't see anything obvious to do this in the OCaml Str l...
I wrote a basic Hippity Hop program in C, Python, and OCaml. Granted, this is probably not a very good benchmark of these three languages. But the results I got were something like this:
Python: .350 seconds
C: .050 seconds
interpreted OCaml: .040 seconds
compiled OCaml: .010
The python performance doesn't really surprise me, bu...
ive set up a custom data type
type vector = {a:float;b:float};
and i want to Initialize an array of type vector but containing nothing, just an empty array of length x.
the following
let vecarr = Array.create !max_seq_length {a=0.0;b=0.0}
makes the array init to {a=0;b=0} , and leaving that as blank gives me errors. Is what im t...
Sometimes I see code like
let (alt : recognizer -> recognizer -> recognizer) =
fun a b p -> union (a p) (b p)
Or like:
let hd = function
Cons(x,xf) -> x
| Nil -> raise Empty
What is the difference between fun and function?
...
How could I convert binary to decimal in ocaml in the following,
If I am representing binary numbers like the following 01 is [False; True] which equals 2? I want to write a method to return the double of the number in decimal form. For example in this case the method will take in [False; True] and return [False; False, True] which is 4...
How could I convert binary to decimal and decimal to binary in the following, If I am representing binary numbers like the following 01 is [False; True] which equals 2? I want to write a method to take in [False; True] and return 2. Also a method for doing it the other way around taking in an integer 2 and returning [False; True].
...
Given this snippet of OCaml code:
let rec range a b =
if a > b then []
else a :: range (a+1) b
;;
The Repl tells me that it's type is:
val range : int -> int -> int list = <fun>
Giving it inputs such as:
range 0 4;;
gives back the list:
- : int list = [0; 1; 2; 3; 4]
However providing the input
range -4 2;;
Gives the...
I've created a mutable data structure in OCaml, however when I go to access it, it gives a weird error,
Here is my code
type vector = {a:float;b:float};;
type vec_store = {mutable seq:vector array;mutable size:int};;
let max_seq_length = ref 200;;
exception Out_of_bounds;;
exception Vec_store_full;;
let vec_mag {a=c;b=d} = sqrt( c*...
How can I make a lazy list representing a sequence of doubling numbers? Example:
1 2 4 8 16 32
...
I'm using an implementation of lazy lists where the type can be either Nil or Cons (value, thunk), where thunk is a function from unit to the rest of the list.
I'm trying to write a function cross, which would function as List.combine does. Unfortunately, I'm having syntax errors.
open Sequence;;
let rec (cross : 'a Sequence.t -> '...
I wrote a short bit of code to simply skip num_lines lines in an input file (printing the lines out for debugging purposes. Here's two things I tried that didn't work:
for i = 0 to num_lines do
print_endline (fscanf infile "%s" (fun p -> p));
done;;
for i = 0 to num_lines do
print_endline (fscanf infile "%S\n" (fun p -> p));
done;...
The documentation on the Bigarray module is somewhat vague. It states that the purpose of arrays in that module is to hold "large arrays", but it doesn't really define what it means by "large array". When should I use a Bigarray over a regular array? Is there a certain number of elements beyond which I should just use a Bigarray? Is ...
All of these start from a simple idea: How to write python-style formatted string in ocaml.
pythoners could init a string as:
str = "this var: %s" % this_var
str2 = "this: %s; that: %s" % (this_var, that_var)
but ocaml's formatted string code as :
let str = Printf.sprintf "this var: %s" this_var
let str2 = Printf.sprintf "this: %s; ...
I wanted to have a tail-recursive version of List.map, so I wrote my own. Here it is:
let rec list_map f l ?(accum=[])=
match l with
head :: tail -> list_map f tail ~accum:(head :: accum)
| [] -> accum;;
Whenever I compile this function, I get:
File "main.ml", line 69, characters 29-31:
Warning X: this optional argument ...
I have a program that solves the weighted interval scheduling problem using dynamic programming (and believe it or not, it isn't for homework). I've profiled it, and I seem to be spending most of my time filling M with p(...). Here are the functions:
let rec get_highest_nonconflicting prev count start =
match prev with
head :...