ocaml

How to combine equal sequence elements (functional programming)?

岁酱吖の 提交于 2019-12-01 21:32:59
I want to write a function that takes in a sequence <1,1,2,2,3> and returns the sequence with equal elements grouped like <<1,1>, <2,2>, <3>>. I'm using sequences, not lists, but some of the functions are similar. Some of the functions I am thinking of using are map, reduce, tabulate, filter, append etc.. Reduce takes in an associative function and returns the sequence that is "reduced" by that operator. So, reduce op+ 0 <1,2,3> = 6. My first thought was to use map to raise the sequence by one level. So, <1,1,2,2,3> => <<1>,<1>,<2>,<2>,<3>>. Then, I was thinking of using reduce, in which I

Printing stack traces

谁都会走 提交于 2019-12-01 21:06:50
I have a very short test file: let print_backtrace () = try raise Not_found with Not_found -> Printexc.print_backtrace stdout;; let f () = print_backtrace (); Printf.printf "this is to make f non-tail-recursive\n";; f (); I compile and run: % ocamlc -g test.ml % OCAMLRUNPARAM=b ./a.out Raised at file "test.ml", line 1, characters 35-44 this is to make f non-tail-recursive Why isn't f listed in the stack trace? How can I write a function that will print a stack trace of the location it's called from? Here is the code to do what I suggested. I recommend using ocamldebug if at all possible, this

Hashtable indexed on several fields

一个人想着一个人 提交于 2019-12-01 21:04:21
问题 I'm currently programming an OCaml module defining a type corresponding to a CPU register. The interface of this module is the following : (* * Defines a type which represents a R3000 register. *) type t = | R0 (* Always 0 *) | AT (* Assembler temporary *) | V0 | V1 (* Subroutine return values *) | A0 | A1 | A2 | A3 (* Subroutine arguments *) | T0 | T1 | T2 | T3 | T4 | T5 | T6 | T7 (* Temporary registers *) | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 (* Register variables *) | T8 | T9 (*

Disable or enable code by preprocessor

蹲街弑〆低调 提交于 2019-12-01 19:54:55
In C++ I'd write bool positive (int a) { #ifdef DEBUG cout << "Checking the number " << a << "\n"; #endif return a > 0; } In OCaml I could write let positive x = begin printf "Checking the number %d\n" x; x > 0 end But how can I disable the printf statement when not in debug mode? you can use cppo : https://github.com/mjambon/cppo . This is available via opam, and offers C like preprocessor features. Without preprocessing you can simply have a global flag defined as let debug = true and write: if debug then printf ...; This code is removed by ocamlopt if debug is false. That said, it's

Hashtable indexed on several fields

柔情痞子 提交于 2019-12-01 18:26:09
I'm currently programming an OCaml module defining a type corresponding to a CPU register. The interface of this module is the following : (* * Defines a type which represents a R3000 register. *) type t = | R0 (* Always 0 *) | AT (* Assembler temporary *) | V0 | V1 (* Subroutine return values *) | A0 | A1 | A2 | A3 (* Subroutine arguments *) | T0 | T1 | T2 | T3 | T4 | T5 | T6 | T7 (* Temporary registers *) | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 (* Register variables *) | T8 | T9 (* Temporary registers *) | K0 | K1 (* Reserved for kernels *) | GP | SP | FP (* Global/Stack/Frame pointer *) |

function returns list in reverse order in OCaml

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 18:03:22
I want to read some numbers from a file, take them to a list and finally display them on the screen. numbers.txt currently has 2 3 5 7 11 however as output i'am getting 11 7 5 3 2 - : unit = () Why is this happening? let rec int_list_from_sb sb n = match n with | 0 -> []; | _ -> (bscanf sb " %d" (fun a -> a))::(int_list_from_sb sb (n - 1));; let file_name = open_in "numbers.txt" in let sb = Scanning.from_channel file_name in let int_list = int_list_from_sb sb 5 in List.iter (fun a -> print_int a) int_list;; The order of evaluation of arguments is unspecified in OCaml. So when you do f x :: g y

How do you inspect the type of (*) on OCaml's toplevel?

醉酒当歌 提交于 2019-12-01 18:01:27
I wanted to see the type of the multiplication function (*), so I tapped it into the OCaml toplevel. # (*) However, the toplevel echoed: (*);; 1: this is the start of a comment. and then consumed any further input I put in. I figured that I had to get out of the comment mode by pressing Ctrl+d to send EOF. Great. But surely, I should be able to query the type of any function, including our mysterious multiplication function (*) ?! I would be incredibly disappointed if that is a limitation of the toplevel. It does recognize *) as the end of the comment, but it's still waiting for the end of the

OCaml - declaring n-dimensional arrays

眉间皱痕 提交于 2019-12-01 17:55:01
I want to create 3-dimensional array in ocaml. Here's what I tried to do: let dp = Array.make n (Array.make n (Array.make k (-1)) However it doesn't work - changing a value dp[0][0][0] changes all values dp[i][j][0]. So how to create matrix with distinct arrays, not with copies of the same one? Array.make n v will just replicate the second argument n times. In other words, it will assign it in a cycle to each element. Since arrays (as well as all other heap-allocated values) are passed by reference, all cells will point to the same array. You need to use the Array.init function, that will call

Compiling multiple Ocaml files

你。 提交于 2019-12-01 17:51:23
I am new to Ocaml and trying to write some small example application. I am using ocamlc version 3.11.2 under Linux Ubuntu 10.04. I want to compile two files: a.ml b.ml File b.ml uses definitions from a.ml . As far as I understand, I can use ocamlc -c to perform compilation only. I can call ocamlc one final time when I have all the .cmo files to link them to an executable. Also, when compiling a file that uses definitions from another file, I have to tell the compiler in which .cmi file to find the external definitions. So my idea was to use: ocamlc -i -c a.ml > a.mli ocamlc -c a.mli b.ml

OCaml - declaring n-dimensional arrays

拜拜、爱过 提交于 2019-12-01 17:11:24
问题 I want to create 3-dimensional array in ocaml. Here's what I tried to do: let dp = Array.make n (Array.make n (Array.make k (-1)) However it doesn't work - changing a value dp[0][0][0] changes all values dp[i][j][0]. So how to create matrix with distinct arrays, not with copies of the same one? 回答1: Array.make n v will just replicate the second argument n times. In other words, it will assign it in a cycle to each element. Since arrays (as well as all other heap-allocated values) are passed