ocaml

caesar cipher check in ocaml

拟墨画扇 提交于 2019-12-13 08:38:16
问题 I want to implement a check function that given two strings s1 and s2 will check if s2 is the caesar cipher of s1 or not. the inter face needs to be looked like string->string->bool . the problem is that I am not allowed to use any string functions other than String.length , so how can I solve it? i am not permitted any list array , iterations . Only recursions and pattern matching . Please help me. And also can you tell me how I can write a substring function in ocaml other than the module

Explanation for group by 3 elements from a list function using FOLD in OCAML

你说的曾经没有我的故事 提交于 2019-12-13 08:24:11
问题 I have a piece of code that does the following: group 3 elements of a list of n elements. The main function is called group_by_3 . For example, executing group_by_3 [1;2;3;4;5;6;7] will give me ([1;2;3],[4;5;6],[7]) . let group_by_3 lst = let accum = ( [], [], 0 ) in let f (all_groups, current_group, size) x = if size = 3 then ( (List.rev current_group) :: all_groups, [x], 1 ) else ( all_groups, x::current_group, size+1) in let (groups, last, _) = List.fold_left f accum lst in List.rev ( List

OCaml - return a list containing all the elements in even position in the input list

狂风中的少年 提交于 2019-12-13 08:19:31
问题 I am new to OCaml, and I am now trying to implement a function that returns a list containing all the elements in even position in the input list. For [1;2;3;5] returns [2;5] and for [1 3] returns [3]; and for ["i";"am";"new";"to";"ocaml"] returns ["am";"to"]. Hope someone can give me some suggestions to solve this problem. 回答1: There's nothing particularly OCaml-like in this problem. Most likely your main task is to learn to solve problems recursively. The way to solve a problem recursively

Add elements to list in a loop in OCAML

别来无恙 提交于 2019-12-13 06:30:41
问题 Hi i'm a beginner in OCAML and i would like to create a function to add elements a the end of a list and return this list. Here is my code : let test () : int list = let mylist = [] in for i = 1 to 3 do mylist@[i]; done; mylist;; It says that mylist@[i] should have type unit. When I call this function it returns an empty list. Can anyone help me ? Thanks 回答1: Ocaml lists are immutable, i.e., they cannot be changed. The expression mylist@[i] creates a new list. However, since you do nothing

Try the first rule if the second rule fails

二次信任 提交于 2019-12-13 06:26:57
问题 I have defined two sets of identifiers IDENTIFIER_ONE and IDENTIFIER_TWO which are both exculsive subsets of IDENTIFIER . I would like to write a parser such that: "i1(arg) EOS" can't be parsed (1) "i2(arg) EOS" can be parsed (2) "i1(arg) = value EOS" can be parsed (3) "i2(arg) = value EOS" can be parsed (4) where i1 (resp., i2 ) belongs to IDENTIFIER_ONE (resp., IDENTIFIER_TWO ); arg and value belong to IDENTIFIER . The following parser.mly has already realized all the points I am after,

OCaml function to evaluate arithmetic expressions

前提是你 提交于 2019-12-13 06:07:56
问题 I’ve written part of a mathematical calculator program. I’ve finished the parser of the program, so when a user types in a mathematical expression as a string, I already have a way to obtain a corresponding data structure of the following type: type expression = | Term of int | Addition of expression * expression | Multiplication of expression * expression | Subtraction of expression * expression All I need now is a way to evaluate these kinds of data structures. (a) Write the expression

OCaml: input redirection

醉酒当歌 提交于 2019-12-13 04:42:06
问题 Is there anyway to redirect the input file into our program in OCaml? Something like this: filename.ml < input.<can be any extension> I have googled for this problem. The module Unix comes up to mind but I really don't understand how it works. Could someone please give me some suggestion? If I'm right about the Unix module, can you please give me an example of how it works?! Thank you very much! 回答1: Since you know how to redirect from the command line, I assume you're asking how to redirect

OCaml polymorphic recursion errors

烂漫一生 提交于 2019-12-13 03:46:45
问题 Given the following types: type _ task = | Success : 'a -> 'a task | Fail : 'a -> 'a task | Binding : (('a task -> unit) -> unit) -> 'a task | AndThen : ('a -> 'b task) * 'a task -> 'b task | OnError : ('a -> 'b task) * 'a task -> 'b task type _ stack = | NoStack : 'a stack | AndThenStack : ('a -> 'b task) * 'b stack -> 'a stack | OnErrorStack : ('a -> 'b task) * 'b stack -> 'a stack type 'a process = { root: 'a task ; stack: 'a stack } let rec loop : 'a. 'a process -> unit = fun proc ->

Debuging all the code computing and sorting equivalence classes

送分小仙女□ 提交于 2019-12-13 03:22:21
问题 I have this function compute an equivalence class let eq_class m i = let column = m.(i) and set = ref [] in Array.iteri begin fun j l -> if j = i || column.(j) && m.(j).(i) then set := j :: !set else ignore l end column; !set;; and this function to collect all the classes are equivalence let eq_classes m = let classes = ref [] in Array.iteri begin fun e _ -> if not (List.exists (List.mem e) !classes) then classes := eq_class m e :: !classes end m; !classes;; I have this function to compare

What is the use of eval `opam config env`?

僤鯓⒐⒋嵵緔 提交于 2019-12-13 02:23:44
问题 After the installation of opam , it asks to do eval `opam config env` . What is the exact usage of it? 回答1: It will setup environment variables, that are necessary for the toolchain to work properly, e.g., CAML_LD_LIBRARY_PATH . It is like activating virtual environment in Python's virtualenv . If you want to see what variables are set exactly, the you can run it without the eval part: $ opam config env CAML_LD_LIBRARY_PATH="/home/ivg/.opam/4.02.1/lib/stublibs"; export CAML_LD_LIBRARY_PATH;