Write to certain byte / position in the Ocaml file

Is there any equivalent to seekp() in OCaml ? I need to write chars in a file at certain offsets.An utop example, to write a character ('A') at a certain position inside a file (Test.data): #use "topfind";; #require "unix";; open Unix;

How can I turn this function into a recursive queue

let rec hf0 k n = if n <= k then n else let rec loop i = if i > k then 0 else hf0 k (n - i) + loop (i + 1) in loop 1 I have no idea how to do it since there is a sum of recursive functions there. (Thanks @Jeffrey Scofield for the code)Your code is n

What is the problem with my synonym of Haskell type?

I have two functions for controlling loops, continue and break: type Control a = (a -> a) -> a -> a continue :: Control a continue = id break :: Control a break = const id Then, I wanted to simplify the Control type synonym. Hence, I wrote: type

Can I pass a value to a functor in Ocaml?

I have a module signature for a Ring. IntRing (Z) was easy to define, but I want to create IntRingModP (Z_p). How can I pass P to the functor to set it when creating a module? module IntRing : Ring = struct type t = int let zero = 0 let one = 1 let a

OCaml: Functor application in the type declaration

I am trying to parametrise a functor using a type parameter in a declaration. As an example, suppose I have the following GADT declaration: type _ basic = | String : string -> string basic | Integer : int -> int basic I want to use Map.Make to const

Mutually recursive types in OCaml

In Haskell you can do the following: Prelude> data Foo = Foo Bar; data Bar = Bar Foo How can you do the same thing in OCaml? I tried: ___ # type foo = Foo of bar;; type bar = Bar of foo;; Error: Unbound type constructor bar Is it even possible to def

Defining a type alias for Hash Table in OCaml

I am trying to use atdgen and it requires that you define the types of the OCaml objects you're trying to convert to JSON in what it calls an "atd file" So for Hashtbl.t atdgen generates code which looks like: type ('a, 'b) bucketlist = | Empty

What is the grammar rule for this OCaml code?

I'm not familiar with OCaml, but recently have been analysing the syntax of OCaml. I came across this include construct which I can't see which grammar rule it relates to: include Warning(Loc).S The syntax rule for include is defined as include modul

Shortcut for the constructor function

Is there any way to pass a constructor as a function? type foo = | Foo of int | Bar of int let foo x = Foo x let bar = fun x -> Bar x Is there any shorthand for the functions foo and bar? I want to pass a constructor as a function, but it seems unwie

binary-search in OCaml?

could someone tell me where is the mistake here? let a = [|2;4;6;9;12|];; a.(0);; a.(4);; a.(5);; let binary_search array size x = let n = size-1 in let p = ref 0 in let r = ref n in while (!p <= !r) do let q = (!p + !r)/2; if array.(q) = x then rais

OCaml: pattern matching on variable length strings

I'm trying to write a program that accepts user input (one line at a time) and evaluates this user input in some ways. I have a function getline : unit -> string that reads a string from stdin and returns it. The commands have the following format: #

OCaml - If the card is in standard OCaml?

I have a question. If Module Map is in standard OCaml ? I cannot find answer in the internet. Additionaly, what is difference between Map and Set?It depends on what you mean by "standard" OCaml. The compiler is distributed with a small library,

OCaml Where did I make a mistake?

How to correct this error ? type 'a drzewo = | Puste | Wezel of 'a * 'a drzewo * 'a drzewo let rec inorder t = match t with | Puste -> print_int (-1) | Wezel (v, l, r) -> (inorder l; print_int v; inorder r) let tree = (1, (Wezel (2, (Wezel (3, Puste

Set a timer for a function

I have defined a list of values: data : int list and a function f: int -> unit, and a piece of code: for i = 0 to (List.length data) - 1 do let d = List.nth data i in f d done Now, I would like to set a maximal running time for f. For instance, if f

Counter party in OCaml

data Seq a = Nil | Cons a (Seq (a,a)) Ok, so this is a piece of haskell declaring a nested datatype of a binary tree, yet is there a counter part in OCaml? Show us in OCaml code if yes. I have tried, but I wanna make sure if this is the same idea wit

Is there a flip function in the standard OCaml library?

In Haskell, we have a flip function: flip f x y = f y x, which essentially takes a function and returns the same function except that the two arguments are swapped. I wonder if there is a counterpart in OCaml, since I could not find one and don't wan

How to define an int64 in OCaml?

let i = 32 will give me a int32. What if I want to define a int64? let i = 32L (Postfix L for int64 literals introduced in Objective Caml 3.07) and use module Int64: http://caml.inria.fr/pub/docs/manual-ocaml/libref/Int64.html You should really use g

Binary tree Width-first search

I'm using OCaml. I have type: type 'a bt = Empty | Node of 'a * 'a bt * 'a bt;; Also I have example BST: let tree = Node(1,Node(2,Node(4,Empty,Empty),Empty),Node(3,Node(5,Empty,Node(6,Empty,Empty)),Empty)); I need to write function: breadthBT : 'a bt

OCaml: type constraints in signatures

In my code, I have a database access context that provides elementary read/write operations, called CouchDB.ctx. Various modules in my application then extend that class with additional functionality, such as Async.ctx. I am implementing a Cache modu

OCaml + Menhir Compilation / Writing

I'm a complete newbie when it comes to OCaml. I've only recently started using the language (about 2 weeks ago), but unfortunately, I've been tasked with making a syntax analyzer (parser + lexer, whose function is to either accept or not a sentence)

Ocaml error with the if statement

I have a list of lists, eg [[1;2;3];[2];[3;4;5;6]; [7;8;9;10] I want to place these in a Hashtbl where the key is the length of the list and the value is list of lists, which contains all sublists of the given length. So for the example above the has

Equality in the hashtables of Ocaml

Are there hashtables in Ocaml that use == instead of = when testing for equality of keys? For example: # type foo = A of int;; # let a = A(1);; # let b = A(1);; # a == b;; - : bool = false # a = b;; - : bool = true # let h = Hashtbl.create 8;; # Hash

What is the best way to structure and build OCaml projects?

It is unclear to newcomers to the ecosystem what is the canonically preferred way to structure and manage building small to medium sized OCaml projects. I understand the basics of ocamlc, &c.--they mirror conventional UNIX C compilers enough to seem

How can I represent a null value in the float type for Ocaml

I know this may seem very basic but basically, I want to say in pattern matching match value with Null-> failwith "Empty" |value-> #do something I've tried any variation of null or none, and have also tried unit which cannot be used becaus

Ocaml Int and negative values

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: - :

OCaml data members without initialization

I would like to create an object with a data member using OCaml but do not necessarily want to specify an initial value for the data member. Many examples seem to use a list but I would like to use a single instance of an object type I've created. Is