ocaml

Mutually recursive module and functor in OCaml

你说的曾经没有我的故事 提交于 2019-12-13 01:34:35
问题 I have defined an interface A to be used by several functors, and notably by MyFunctor : module type A = sig val basic_func: ... val complex_func: ... end module MyFunctor : functor (SomeA : A) -> struct ... let complex_impl params = ... (* Here I call 'basic_func' from SomeA *) SomeA.basic_func ... ... end Now I want to define a module B with implements the interface A . In particular, the implementation of complex_func should use basic_func through complex_impl in MyFunctor : module B =

How do the OCaml operators < and > work with non-integer types?

痞子三分冷 提交于 2019-12-12 22:06:32
问题 I'm curious how the greater than (>) and less than (<) operators work with types that are not int, float, or double in OCaml. For instance, I was able to discover that string "a" > "b" but is there some reference that lists the conventions for all non-numerical data types. Furthermore, how do these operators work across types? e.g. Is "a" > true or is "a" < true? Finally, how would these work across a user-defined data type? Thanks! 回答1: The OCaml < , > , <= , >= operators only work with two

Use OCaml warning atttribute to disable warning 8: unexhaustive match

给你一囗甜甜゛ 提交于 2019-12-12 17:05:18
问题 I'm trying to write code similar to the following: let [a; b] = (* body *) [1; 2] And I want to disable warning # 8 just for the pattern [a; b] and not for the body or for anything outside of the let. I've tried to put the warning attribute to disable the warning, but none of the below work: let[@warning "-8"] [a[@warning "-8"];b[@warning "-8"]] [@warning "-8"] = [1;2][@warning "-8"] [@@ocaml.warning "-8"] P.S. I'm not really writing this code, but am experimenting with a custom PPX

OCaml: Embedding openGL in GTK

点点圈 提交于 2019-12-12 16:08:46
问题 I would like to embed openGL graphics in a GTK program in OCaml. Do you have any ideas on how to do this? (EDIT) Tk seems to have the problem solved with Togl. 回答1: In my ubuntu I have the package "liblablgtk2-gl-ocaml": $ apt-cache show liblablgtk2-gl-ocaml-dev .... This package contains the development files of lablgtk for libraries using GtkGL. Never used it, but I guess that the GtkGL bindings is exactly what you need. 来源: https://stackoverflow.com/questions/4933975/ocaml-embedding-opengl

Why it takes a long time to compute Fibonacci number?

吃可爱长大的小学妹 提交于 2019-12-12 15:54:24
问题 I started learning Ocaml a few days ago. I tried to make a program of fibonaaci numbers: let rec fib a= if a=1||a=2 then 1 else fib(a-1)+fib(a-2);; This code is not optimal as I do not know how to deal with exception cases. But for now, if I try to compute fib 50 or fib 100, then the computer takes a long time to evaluate. I am wondering why, because Ocaml is supposed to be very fast, and adding numbers up is obviously a linear time task. If I paste this code in "Try Ocaml" (http://try

structural comparison of variants

百般思念 提交于 2019-12-12 15:24:13
问题 I want to deal with limits on the integer number line. I would like to have Pervasives.compare treat RightInfinity > Point x for all x , and the inverse for LeftInfinity . In the ocaml REPL: # type open_pt = LeftInfinity | Point of int | RightInfinity ;; # List.sort Pervasives.compare [LeftInfinity; Point 0; Point 1; RightInfinity] ;; - : open_pt list = [LeftInfinity; RightInfinity; Point 0; Point 1] but # type open_pt = LeftInfinity | Point of int | RightInfinity of unit ;; # List.sort

Write an efficient string replacement function?

巧了我就是萌 提交于 2019-12-12 14:53:39
问题 For the purpose of learning I've tried to come up with a string replacement function, here's what I have now: let string_replace where what replacement = let wlength = (String.length what - 1) in let length = (String.length where - 1) in let rec loop i shift = let [shift, i] = if wlength == shift then (String.blit replacement 0 where (i - wlength) (wlength + 1); [0, i]) else if (String.get where i == String.get what shift) then [shift + 1, i] else [0, i - shift] in if i < length then loop (i

simple ocaml graphics progam that close before its window is displayed

て烟熏妆下的殇ゞ 提交于 2019-12-12 14:44:05
问题 My system is ArchLinux I have this simple ocaml program that should create a window and use drawing primitives. open Graphics let _ = open_graph ""; set_window_title "Graphics example"; draw_rect 50 50 300 200; set_color red; fill_rect 50 50 300 200; set_color blue; draw_rect 100 100 200 100; fill_rect 100 100 200 100 I can compile it: ocamlc graphics.cma -o graphics_exple graphics_exple.ml And launch it with: ./graphics_exple I see in my taskbar that a new window take focus then disapear

What OCaml libraries are there for lazy list handling?

…衆ロ難τιáo~ 提交于 2019-12-12 13:39:59
问题 What OCaml libraries are out there that provide lazy list handling? I am looking for something along these lines: type 'a lazy_list = (*'*) | Nil | Cons of 'a * 'a lazy_list lazy_t let from f = let rec gen n = lazy ( match f n with | Some x -> Cons (x, gen (n + 1)) | None -> Nil ) in gen 0 Integration with the Stream type and syntactic sugar for backtracking Camlp4 parsers would be nice. 回答1: Ocaml Batteries has a lazy list module, check out the to_stream function. As for backtracking, you

How to trace error of OCaml programs?

空扰寡人 提交于 2019-12-12 12:35:08
问题 I am writing a compiler written in OCaml. Sometimes when there is an error of execution, it shows the line of error, but it does not show the context, for instance, how the function is called, with which values... In order to help debugging, does anyone know a way to show the steps of execution till the error with real value of the relevant variables? By the way, I am using Emacs as editor. 回答1: Ocaml is compiled. You seem to be used to interpreted languages, where the run-time system has