ocaml

Pattern matching a variable in OCaml?

假装没事ソ 提交于 2020-01-03 18:33:07
问题 If I do let check n = function | n -> true | _ -> false then I get Warning 11: this match case is unused. I understand why , since the n in | n -> true is actually not the argument of check . It is basically a variable created by the pattern matching. My question is , in this case, do we have any way to still using pattern matching (instead of if else) to force this check? I.e., I want to pattern match with the argument n . 回答1: You can use when to have patterns along with boolean conditions:

Feed ocamlyacc parser from explicit token list?

主宰稳场 提交于 2020-01-03 16:54:55
问题 Is it possible to feed an OCamlYacc-generated parser an explicit token list for analysis? I'd like to use OCamlLex to explicitly generate a token list which I then analyze using a Yacc-generated parser later. However, the standard use case generates a parser that calls a lexer implicitly for the next token. Here tokens are computed during the yacc analysis rather than before. Conceptually a parser should only work on tokens but a Yacc-generated parser provides an interface that relies on a

What is the meaning of `abstract` in the `interface definition`?

强颜欢笑 提交于 2020-01-03 16:46:52
问题 I quite don't understand how the interface thing works in OCaml. Let's see an example: About the 'a So what the meaning of 'a here? I mean I understand that when describing the functions, 'a means arbitrary type . Then what's its meaning here? Does it mean arbitrary set ? Also, Why put 'a in front of set ? abstract When explaining this example, Jason Hickey's Introduction to Objective Caml says: we need to define a polymorphic type of sets ’a set abstractly . That is, in the interface we will

How to check whether two values are created with the same constructor?

霸气de小男生 提交于 2020-01-03 13:36:14
问题 let's say I have type t = A of int | B of int let xx = A(2);; let yy = A(3);; and I want to test if the constructors of xx and yy are equal, is there an easy way to do this ? Instead of having to match xx with A _ -> (match yy with A _ -> true | B _ -> false) | B _ -> (match yy with A _ -> false | B _ -> true);; which gets quite messy when there many constructors on a type 回答1: You can rewrite the above to, somewhat simpler: match xx, yy with | A _, A _ | B _, B _ -> true | (A _ | B _), _ ->

Where can I find the OCaml Option module?

ぐ巨炮叔叔 提交于 2020-01-03 09:12:28
问题 I mean this module : Option I can't find it, open Option gives me Error: Unbound module Option and there is no 'option.cma' file Is it in the standard library ? Is it named 'option.cma' ? 回答1: This is not part of the OCaml standard library, no. It looks like it might be part of a former library named Extlib. Extlib, in turn, seems to have become part of OCaml Batteries Included. The Option module is now named BatOption. If you want this module, you should get OCaml Batteries Included. Then

Where can I find the OCaml Option module?

霸气de小男生 提交于 2020-01-03 09:12:27
问题 I mean this module : Option I can't find it, open Option gives me Error: Unbound module Option and there is no 'option.cma' file Is it in the standard library ? Is it named 'option.cma' ? 回答1: This is not part of the OCaml standard library, no. It looks like it might be part of a former library named Extlib. Extlib, in turn, seems to have become part of OCaml Batteries Included. The Option module is now named BatOption. If you want this module, you should get OCaml Batteries Included. Then

Match one item in List of tuples

非 Y 不嫁゛ 提交于 2020-01-03 08:27:28
问题 I have a List of tuples of the form (string, int) . I'm trying to search through the list and return the tuple whose string component matches the parameter, as in: let find_tuple string_name tuples_list = How can I do this? I can't quite wrap my head around it. Is there a way to use matching syntax like (string, _) ->... ? 回答1: You can achieve this as following let rec find_tuple string_name tuples_list = match tuples_list with [] -> raise Not_found |(s, i)::tl -> if s = string_name then (s,

how to get a sub list from a list in ocaml

穿精又带淫゛_ 提交于 2020-01-03 07:28:27
问题 I'm looking at the List documentation. It seems the library does not provide a sublist function. I'm trying to get list of elements from i to j . Now I have to write it as: let rec sublist list i j = if i > j then [] else (List.nth list i) :: (sublist list (i+1) j) which is quite concise but I'm questioning the efficiency of List.nth , because if it's O(n) , I would rather have to write it in a less concise way. I'm wondering why didn't they provide List.sublist func, if List.nth is not O(1)

how to get a sub list from a list in ocaml

主宰稳场 提交于 2020-01-03 07:28:06
问题 I'm looking at the List documentation. It seems the library does not provide a sublist function. I'm trying to get list of elements from i to j . Now I have to write it as: let rec sublist list i j = if i > j then [] else (List.nth list i) :: (sublist list (i+1) j) which is quite concise but I'm questioning the efficiency of List.nth , because if it's O(n) , I would rather have to write it in a less concise way. I'm wondering why didn't they provide List.sublist func, if List.nth is not O(1)

OCaml recursive modules across compilation units

纵然是瞬间 提交于 2020-01-02 10:09:12
问题 I'm trying to split the following recursive modules into separate compilation units. Specifically, I'd like B to be in its own b.ml, to be able to reuse it with other A's. module type AT = sig type b type t = Foo of b | Bar val f : t -> b list end module type BT = sig type a type t = { aaa: a list; bo: t option } val g : t -> t list end module rec A : (AT with type b = B.t) = struct type b = B.t type t = Foo of b | Bar let f = function Foo b -> [ b ] | Bar -> [] end and B : (BT with type a =