ocaml

What is the benefit of purely functional data structure?

瘦欲@ 提交于 2019-11-28 15:42:53
There are large number of texts on data structures, and libraries of data structures code. I understand that purely functional data structure is easier to reason about. However I have trouble to understand the real world advantage of using purely functional data structure in pragmatic code (using functional programming language or not) over the imperative counterpart. Can somebody provide some real world cases where purely functional data structure has advantage and why? Examples along the line like I use data_structure_name in programming_language to do application because it can do certain

Why is an int in OCaml only 31 bits?

别等时光非礼了梦想. 提交于 2019-11-28 15:24:19
Haven't seen this "feature" anywhere else. I know that the 32nd bit is used for garbage collection. But why is it that way only for ints and not for the other basic types? Jörg W Mittag This is called a tagged pointer representation, and is a pretty common optimization trick used in many different interpreters, VMs and runtime systems for decades. Pretty much every Lisp implementation uses them, many Smalltalk VMs, many Ruby interpreters, and so on. Usually, in those languages, you always pass around pointers to objects. An object itself consists of an object header, which contains object

Should I learn Haskell or F# if I already know OCaml? [closed]

折月煮酒 提交于 2019-11-28 13:44:04
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not

OCaml performance of exceptions

廉价感情. 提交于 2019-11-28 13:21:07
I've often read that exceptions are somewhat slow and should be avoided if performance is an issue (for instance, in Java, F#, etc). Does that apply to common OCaml functions such as Hashtbl.find , which return exceptions for elements not found? In particular, if I want my application to be efficient, should I always test element membership using, for instance, Hashtable.mem before calling Hashtbl.find ? Or would the extra comparison of the mem function negatively impact performance? gasche OCaml exception handling make raising and catching exceptions extremely fast -- see this SO thread for

Frama-C Plugin development: Getting result of value-analysis

时光总嘲笑我的痴心妄想 提交于 2019-11-28 12:44:59
I am working on a Plugin for Frama-C, using the Value-analysis. I simply want to print the state of the variables (values) after each statement (I think the solution is quiet easy, but I couldn't figure it out). I got the current state with Db.Value.get_stmt_state in the vstmt_aux method in the visitor. How can I now get the values of the variables? PS: I found this post, but it didn't help, there is no real solution, and with the help of the description I was not able to do it: How to use functions in Value.Eval_expr, Value.Eval_op etc modules of Frama-c Value plugin Here's a concrete example

Splitting a list of items into two lists of odd and even indexed items

ⅰ亾dé卋堺 提交于 2019-11-28 11:58:12
I would like to make a function that accepts a list and returns two lists: the first contains every odd item, and the second contains every even item. For example, given [1;2;4;6;7;9] , I would like to return [ [1;4;7] ; [2;6;9] ] . I have written this so far and I do not know how to progress. let splitList list = let rec splitOdd oList list1 list2 = match oList with | [] -> [] | head :: tail -> splitEven tail (list1::head) list2 and splitEven oList list1 list2 = match oList with | [] -> [] | head :: tail -> splitOdd tail list1 (list2::head) splitOdd list [] [] Implementation which does not

Efficient input in OCaml

天大地大妈咪最大 提交于 2019-11-28 11:34:17
Suppose I am writing an OCaml program and my input will be a large stream of integers separated by spaces i.e. let string = input_line stdin;; will return a string which looks like e.g. "2 4 34 765 5 ..." Now, the program itself will take a further two values i and j which specify a small subsequence of this input on which the main procedure will take place (let's say that the main procedure is the find the maximum of this sublist). In other words, the whole stream will be inputted into the program but the program will only end up acting on a small subset of the input. My question is: what is

Creating GADT expression in OCaml

纵饮孤独 提交于 2019-11-28 11:27:44
There is my toy GADT expression: type _ expr = | Num : int -> int expr | Add : int expr * int expr -> int expr | Sub : int expr * int expr -> int expr | Mul : int expr * int expr -> int expr | Div : int expr * int expr -> int expr | Lt : int expr * int expr -> bool expr | Gt : int expr * int expr -> bool expr | And : bool expr * bool expr -> bool expr | Or : bool expr * bool expr -> bool expr Evaluation function: let rec eval : type a. a expr -> a = function | Num n -> n | Add (a, b) -> (eval a) + (eval b) | Sub (a, b) -> (eval a) - (eval b) | Mul (a, b) -> (eval a) * (eval b) | Div (a, b) ->

Asking about return type, list and set data structure in OCaml

孤街浪徒 提交于 2019-11-28 11:20:42
问题 I have a function compute a list to boolean matrix where num_of_name: 'a list -> 'a -> int : return a position of element in a list. 1) I would like mat_of_dep_rel : 'a list -> bool array array. My problem is that from the first List.iter it should take a list l and not an empty list [] . But if I return l instead of [] , it will give me a type: ('a * 'a list) list -> boolean array array. Which is not what I want. I would like to know how can I return mat_of_dep_rel: 'a list -> bool array

Integer exponentiation in OCaml

為{幸葍}努か 提交于 2019-11-28 11:00:33
Is there a function for integer exponentiation in OCaml? ** is only for floats. Although it seems to be mostly accurate, isn't there a possibility of precision errors, something like 2. ** 3. = 8. returning false sometimes? Is there a library function for integer exponentiation? I could write my own, but efficiency concerns come into that, and also I'd be surprised if there isn't such a function already. Regarding the floating-point part of your question: OCaml calls the underlying system's pow() function. Floating-point exponentiation is a difficult function to implement, but it only needs to