ocaml

Embedding Lua in Objective Caml (OCaml)

Could you, please, give a code snippet showing how to use Lua embedded in OCaml? A simple example could be a "Hello, World" variant. Have OCaml prompt the user for a name. Then pass that name to a Lua function. Have Lua print a greeting and return the length of the name. Then have OCaml print a message about the length of the name. ...

Ocaml List: Implement append and map functions

Hi! I'm currently trying to extend a friend's OCaml program. It's a huge collection of functions needed for some data analysis.. Since I'm not really an OCaml crack I'm currently stuck on a (for me) strange List implementation: type 'a cell = Nil | Cons of ('a * 'a llist) and 'a llist = (unit -> 'a cell);; I've figured out that t...

Why isn't OCaml more popular?

I've always heard that C is the language of choice to use for embedded systems, or anything that needs to run at maximum speed. I never developed a fondness for C, mostly because I don't like pointer arithmetic and the language is barely a rung above assembler. On the other hand, ML languages are functional, garbage collected languages...

Basic OCaml OOP question

Hello, I am attempting to learn oCaml and am having an issue as to why the below program is invalid. class myClass2 = object method doSomething = Printf.printf "%s\n" "Doing something" end;; class myClass foo = object val dMember = foo method doIt = dMember#doSomething end;; let mc2 = new myClass2;; let mc = new myClass mc2;; mc#do...

OCaml syntax for passing in custom objects as parameters

I'm trying to get a grip on the OCaml language syntax and am having some trouble with applying some OOP structures. My goal with the below code is to have a class bar that inherits from a virtual class foo. The virtual class contains three virtual methods that I hope to take an instance of a "player" object as a parameter. When I compile...

OCaml lists

I'm having trouble with lists in OCaml. I've read conflicting statements saying whether or not the lists can be modified at runtime. Can the cons operator be used at runtime? Additionally, why is a doberman (see below) allowed to be in a list of chihuahuas? How would one go about adding another chihuahua onto the list (as attempted with...

Creating objects on-the-fly in OCaml

I am attempting to learn OCaml by using compiled code instead of the top-level; however, much of the sample code online seems to appeal to the latter. I would like to create a new Foo within a method of an object per below. This code does not compile, citing a syntax error with the doFooProc definition. class bar = object (self) metho...

Lookup tables in OCaml

I would like to create a lookup table in OCaml. The table will have 7000+ entries that, upon lookup (by int), return a string. What is an appropriate data structure to use for this task? Should the table be externalized from the base code and if so, how does one go about "including" the lookup table to be accessible from his/her program?...

Ocaml noobie Q -- how to use accumulating parameters?

I'm trying to learn Ocaml by working on Problem 18 from Project Euler. I know what I want to do, I just can't figure out how to do it. I've got three lists: let list1 = [1;2;3;4;5];; let list2 = [ 6;7;8;9];; let line = [9999];; I want to add the numbers list2 to the max adjacent number in list1, IOW I would add 6+2, 7+3, 8+4 and 9+5...

Representing optional syntax and repetition with OcamlYacc / FsYacc

I'm trying to build up some skills in lexing/parsing grammars. I'm looking back on a simple parser I wrote for SQL, and I'm not altogether happy with it -- it seems like there should have been an easier way to write the parser. SQL tripped me up because it has a lot of optional tokens and repitition. For example: SELECT * FROM t1 INNER...

Classic Singleton implementation in OCaml

I am attempting to conceptualize the Singleton design pattern (qua Java) in OCaml and have seen ever instance allude to functors or modules, neither of which I am using in a proof of concept of GoF's work. Basically, I would like to recreate the following functionality using OCaml: public class Singleton { private static Singleton Uniq...

Any documentation out there about OCAML Sdl?

hi everyone, am developing a small game (worms), and am using OCAML Sdl for that, actually i didn't find any documentation out there about it, so i have three questions: I want to use ocaml sdl under windows, so anyone can help how can I compile it, (I already gave up and installed linux on a virtual pc) Is there any documentation on h...

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 this possible? Thanks. class bar = object end;; class foo = object (self) val myDataMe...

How to read a bitmap in OCAML?

I want to read a bitmap file (from the file system) using OCAML and store the pixels (the colors) inside an array which have th dimension of the bitmap, each pixel will take one cell in the array. I found the function *Graphics.dump_image image -> color array array* but it doesn't read from a file. ...

how to declare a record inside an OCAML class

hello, I wanna declare a record inside a class as follows: class player (x, y)= object(self) type gun = {x:int; y:int; active:bool} val guns = Array.create 5 {x=0; y=0; active=false} .... but the compiler claim that this line is syntax error : type gun = {x:in .... when declared outside the class like this type : gun...

Recursive functions within OCaml objects

I am trying to figure out recursion for OCaml in the context of an object's method. I have tried the following code but can't seem to get it to compile. class foo = object (self) method loopTest = let rec doIt x = Printf.printf "%d\n" x; if x>1 then doIt (x+1) end;; How do I create a recursive function of this sort within a m...

Return character to stdin in Ocaml

is there an equivalent to (C++) istream::putback(char) for ocaml? How could I add a character to the beginning of stdin? ...

Determining type on the fly in OCaml's OOP construct

I am learning about OCaml's OOP constructs and partially implemented this today until I realized I have no idea how to represent a polymorphic match statement without using the type keyword outside of the object. class bar (param:string) = object (code) end;; class foo param = object (code) initializer match param with string -...

Manipulating lists in OCaml

I am having issues manipulating deeply nested lists in OCaml in the below context. class foo (pIn:int)= object (self) val p = pIn val even = if (pIn mod 2) = 0 then true else (false) method doIt = "doIt" method isEven = even method getP = p end;; let rec createListOfElements howMany = ( Random.self_init (); ...

Functors in Ocaml

I am having a bit of a problem with a functor (and it's resultant type). Below, I have a Set functor that uses an Ordered type. I actually used the set.ml that comes with ocaml for some guidance, but I seem to be doing everything ahhem right. I created an Ordered module with integers and applied it to the Set functor to get the last modu...