ocaml

Using dynamic programming in Haskell? [Warning: ProjectEuler 31 solution inside]

老子叫甜甜 提交于 2019-12-06 02:08:05
问题 In solving projecteuler.net's problem #31 [ SPOILERS AHEAD ] (counting the number of ways to make 2£ with the British coins), I wanted to use dynamic programming. I started with OCaml, and wrote the short and very efficient following programming: open Num let make_dyn_table amount coins = let t = Array.make_matrix (Array.length coins) (amount+1) (Int 1) in for i = 1 to (Array.length t) - 1 do for j = 0 to amount do if j < coins.(i) then t.(i).(j) <- t.(i-1).(j) else t.(i).(j) <- t.(i-1).(j) +

How to use OCaml's [@tailcall] annotation to assert tail-recursiveness?

老子叫甜甜 提交于 2019-12-06 00:35:38
In OCaml, the [@tailcall] annotation lets you assert that a particular function-call is a tail call (and so hopefully your whole function is tail recursive). The question is: Where do I place the annotation exactly? Obvious, easy example: let rec f = function | 0 -> 0 | x -> (f [@tailcall]) (x - 1) (* works like a charm *) But I don't see how I can do it in "less obvious" places: let rec f = function | 0 -> 0 | x -> (|>) (x - 1) f (* uh? *) I can see from the assembly code that the latter example is recognized as tail recursive by the compiler. So until someone implements [@tailrec] : Where

Re-implementing List.map in OCaml/F# with correct side effect order?

假如想象 提交于 2019-12-06 00:29:09
问题 According to this previous answer You could implement List.map like this: let rec map project = function | [] -> [] | head :: tail -> project head :: map project tail ;; but instead, it is implemented like this: let rec map project = function | [] -> [] | head :: tail -> let result = project head in result :: map project tail ;; They say that it is done this way to make sure the projection function is called in the expected order in case it has side effects, e.g. map print_int [1;2;3] ;;

opam upgrade wants to downgrade a bunch of packages

会有一股神秘感。 提交于 2019-12-06 00:25:22
I just tried an 'opam upgrade' and got (the final summary): 1 to install | 59 to reinstall | 3 to upgrade | 34 to downgrade | 0 to remove Do you want to continue ? [Y/n] Downgrading 34 packages makes me nervous. Why would it want to do this? Some examples of packages it wanted to downgrade: downgrade llvm.3.2 to 3.1 downgrade ocamlfind.1.4.0 to 1.3.3 [required by bitstring, camltc, ezjsonm, fat-filesystem, google-drive-ocamlfuse, merlin, mirage-block-xen, mirage-console-xen, mirari, utop] downgrade SPOC.130624 to 121217 downgrade bitstring.2.0.4 to 2.0.3 downgrade camlzip.1.05 to 1.04

How to make library installed from OPAM available to OCaml?

 ̄綄美尐妖づ 提交于 2019-12-05 22:41:30
I followed this tutorial on OCaml FFI and installed Ctypes through OPAM: opam install ctypes However, OCaml does not find the module: open Ctypes (* ... *) I receive the error: Unbound module Ctypes It looks like I need to let OCaml know where my Ctypes installation is? Do I need to update some path variable to let OCaml look for my libraries installed through OPAM? This is Ubuntu 15.04, OCaml 4.01.0, OPAM 1.2.0. Installing something on your system doesn't make it automatically visible for the compiler, this is true not only for OCaml, but for most conventional systems, like C or C++ to name a

Limitations of let rec in OCaml

。_饼干妹妹 提交于 2019-12-05 20:36:07
问题 I'm studying OCaml these days and came across this: OCaml has limits on what it can put on the righthand side of a let rec. Like this one let memo_rec f_norec = let rec f = memoize (fun x -> f_norec f x) in f;; Error: This kind of expression is not allowed as right-hand side of `let rec' in which, the memoize is a function that take a function and turns it into a memorized version with Hashtable. It's apparent that OCaml has some restriction on the use of constructs at the right-hand side of

Is INRIA going to add concurrency primitives to OCaml?

旧城冷巷雨未停 提交于 2019-12-05 19:09:29
问题 By "concurrency" I mean lightweight processes like Erlang's actors and concurrent GC aimed to make such a processes work smoothly. It would be very cool if INRIA got rid of those drawbacks of the current OCaml implementation to make OCaml more prepared for the multicore future. P.S. F# isn't what I'm looking for. 回答1: no I cannot be more concise without reproducing his explanation. It speaks for itself. Yes, this is from 2002, but I haven't heard him sway on the issue, and from the text, it

OCaml compiler check for vector lengths

╄→гoц情女王★ 提交于 2019-12-05 18:56:22
I was wondering if it is possible to have compile-time check in OCaml to make sure arrays are the correct length. For my problem, I want to verify that two GPU 1-dim vectors are of the same length before doing piecewise vector subtraction. let init_value = 1 let length = 10_000_000 let x = GpuVector.create length init_value and y = GpuVector.create 9 init_value in let z = GpuVector.sub v1 v2 In this example I would like it to throw a compile error as x and y are not the same length. As I am a OCaml noob I would like to know how I can achieve this? I am guessing that I will have to use functors

How to make a covariant observable in OCaml

霸气de小男生 提交于 2019-12-05 18:23:38
I'm trying to make a wrapper for values that allows callers to register themselves for notifications about it. Here's some (working) code: module Thing : sig type +'a t val make : 'a -> 'a t val watch : ('a -> unit) -> 'a t -> unit val notify : 'a t -> unit end = struct type 'a t = { obj : 'a; watchers : (unit -> unit) Queue.t } let make x = { obj = x; watchers = Queue.create () } let watch fn x = x.watchers |> Queue.add (fun () -> fn x.obj) let notify x = x.watchers |> Queue.iter (fun fn -> fn ()) end let () = let x = Thing.make (`Int 4) in Thing.watch (fun (`Int d) -> Printf.printf "Observed

Remove the comments generated by cpp

笑着哭i 提交于 2019-12-05 18:17:42
I use #include ".../frontend/tokens.mll" in lexer.mll , and then cpp -C -P frontend/lexer.mll -o frontend/lexer_new.mll to generate lexer_new.mll . That worked until I upgraded my ubuntu from 12.04 to 14.04 yesterday. The compilation gives an error: ocamllex frontend/lexer_new.mll File "frontend/lexer_new.mll", line 1, character 1: illegal character /. make: *** [frontend/lexer_new.ml] Error 3 That is because in lexer_new.mll several lines of C comments have been inserted in the beginning: /* Copyright (C) 1991-2014 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU