ocaml

Type inference with mutual recursion

I've been thinking about how type inference works in the following OCaml program: let rec f x = (g x) + 5 and g x = f (x + 5);; Granted, the program is quite useless (looping forever), but what about the types? OCaml says: val f : int -> int = <fun> val g : int -> int = <fun> This would exactly be my intuition, but how does the typ...

Defining exceptions with tuple as argument

I'm trying to define an exception in OCaml that accepts a tuple pair of lists as an argument. However, this situation doesn't work? # exception Foo of string list * string list;; exception Foo of string list * string list # let bar = (["a"], ["b"; "c"; "d"]);; val bar : string list * string list = (["a"], ["b"; "c"; "d"]) # raise(Foo b...

OCaml: Is there a function with type 'a -> 'a other than the identity function?

This isn't a homework question, by the way. It got brought up in class but my teacher couldn't think of any. Thanks. ...

Conversion from decimal to binary in Ocaml

I am trying to convert a given decimal value its corresponding binary form. I am using Ocaml about which I don't know much and am quite confused. So far I have the following code let dec_to_bin_helper function 1->'T' | 0->'F' let dec_to_bin x = List.fold_left(fun a z -> z mod 2 dec_to_bin_helper a) [] a ;; I must include h...

How will this Ocaml type definition look in its C stub?

I've taken the following code from http://www.ocaml-tutorial.org/data_types_and_matching I've been trying to write a C stub for following, to invoke from our PHP codebase. I can't quite understand how (and if) I'm supposed to create a typedef for the following Ocaml type expr, and how I can access the function multiply_out from the C st...

Enumerating all pairs constructible from two lazy lists in OCaml

Hi everyone, I am attempting to enumerate the set of all pairs made of elements from two lazy lists (first element from the first list, second element from the second list) in OCaml using the usual diagonalization idea. The idea is, in strict evaluation terms, something like enum [0;1;2;...] [0;1;2;...] = [(0,0);(0,1);(1;0);(0;2);(1;1)...

Saving my running toplevel for later

Hi, When working in the ocaml or ghci toplevels I often build up a significant "context" for want of a better word, values bound, functions, modules loaded, and so on. Is there a way to save all of that and reload it later so I can continue exactly where I left off? Or better yet, dump out the entire lot as a text file that could be rel...

Lazy "n choose k" in OCaml

Hi guys, As part of a bigger problem of enumerating a set, I need to write an OCaml function 'choose' which takes a list and outputs as the list of all possible sequences of size k made up of elements of that list (without repeating sequences which can be obtained from each other by permutation). The order they are put in the end list i...

Drop first n items in a list

I'm trying to make a function which drops the first n items of a list: let rec drop n h = if n == 0 then h else (drop n-1 (match h with a::b -> b));; This is giving: Characters 43-49: if n == 0 then h else (drop n-1 (match h with a::b -> b));; ^^^^^^ Error: This expression has type 'a -> 'b but is here...

How to write a function to get the position of the smallest int in a list?

The prototype must be: listMinPos(lst) I'm able to write the same using two arguments, (list and index), but am not able to even think how it can be possible using only the list argument. The following must hold: only 1 argument (the list). no external libraries. function should be recursive (no 'let' inside the function) ...

Ocaml modules implementation

As everybody knows, Ocaml lets to use various modules: List, Map, Nativeint, etc. I know, that interfaces for these modules are provided (e.g. for List module: http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html), but I am interested in algorithms (= implementation = code) used in modules' functions. So - where can I find that? ...

ExitCode of -1 returned to C#

In my C# application, I'm firing up a program using the .NET Process class. Sometimes, after the program has finished, the Process.ExitCode is -1. It's a large program, and I'm having trouble tracking down where `exit' is called with -1. In fact, I don't see how it's possible to have a program return an exit code of -1. I've created ...

transpose of a list of lists

I'm trying to make a recursive function to get the transpose of a list of lists, n x p to p x n. But i'm unable to do so. I've been able to make a function to transpose a 3 x n list of lists to an n x 3 one: let rec drop1 list= [(match (List.nth list 0) with [] -> [] | a::b -> b); (match (List.nth list 1) with [] -> [] | a::b -...

Comparing list of floats

I wrote a code: let rec compareVs v1 v2 = if List.length v1 == 0 then true else ((match v1 with [] -> 0. | h::l -> h) == (match v2 with [] -> 0. | h::l -> h)) && (compareVs(match v1 with [] -> [] | h::l -> l) (match v2 with [] -> [] | h::l -> l)) And ran it: # compareVs [0.1;0.1] [0.1;0.1];; - : bool = false Can't seem to ...

Keeping type generic without η-expansion

What I'm doing: I'm writing a small interpreter system that can parse a file, turn it into a sequence of operations, and then feed thousands of data sets into that sequence to extract some final value from each. A compiled interpreter consists of a list of pure functions that take two arguments: a data set, and an execution context. Each...

Differences between OCaml 2 and 3

Hello, I'm interested in learning this language, but it seems that there is very few tutorials and books on the subject. I've found only one suitable book on the subject (Developing Applications With Objective Caml) which would be absolutely perfect but the problem is that it is based on version 2.04. So my only concern is if there wou...

Installing Ocaml/grammar module for SWIG

Running Swig example for ocaml gives me this error message. rm -rf swig.mli swig.ml swigp4.ml "'" ; "[" ; e2 = expr ; "]" -> Line 27: > ... Line 114: | f = expr ; "'" ; "(" ; args = LIST0 (expr LEVEL "simple") SEP "," ; ")" -> Line 115: > Line 116: ] ] ; Line 117: END ;; What might be wrong? How to install grammar m...

Copying fields in OCaml

Hi Guys, I have a very basic question regarding OCaml records. Suppose I have a record defined: type r = {a:int;b:int;c:int} let x = {a=3;b=8;c=2} Now, suppose I want to create a new record which has all fields equal to x but which has c=4. I could write: let y = {a=3;b=8;c=4} but this is annoying because there's not need to re-wr...

model n of integer type

I am working on a module which involves a set of natural numbers. As a result I need to model some n of type integer. How can I go about it? Eg. sum of i for continuously increasing sequence of i starting at 1 = n(n+1)/2 How can I model n here? ...

Compile from within the OCaml repl

I am using emacs with tuareg mode and I always have the ocaml repl opened inside emacs. I was wondering if there's a way of compiling basic OCaml test files from within the repl so I don't have to run the compilation as a system command. ...