ocaml

Is it possible to use pipes in OCaml?

断了今生、忘了曾经 提交于 2020-01-01 07:34:27
问题 In F# I can't live without pipes ( <| and |> ) let console(dashboard : Dashboard ref) = let rec eat (command : string) = command.Split(' ','(',')') |> Seq.filter(fun s -> s.Length <> 0) |> fun C -> (Seq.head C).ToUpper() |> fun head -> Can I use <| and |> in OCaml? 回答1: These are available since OCaml 4.01. However, <| is named @@ there, so it has the correct operator associativity. Alternatively, you can either define them yourself: let (|>) v f = f v let (<|) f v = f v (* or: *) let (@@) f

Is it possible to use arrow keys in OCaml interpreter?

末鹿安然 提交于 2020-01-01 01:17:23
问题 Everytime I use these keys in the interpreter I keep getting symbols like this appearing: [[D^[[C I'm using Linux Mint 12 in ZSH, however I'm getting the same result in Ubuntu with bash. Also, same thing in ssh. 回答1: The stock OCaml toplevel doesn't have line editing built in. I use rlwrap : $ cat bin/ocaml #!/bin/sh exec rlwrap /usr/local/bin/ocaml "$@" Using the toplevel without something like this is quite painful, in my opinion! Other possibilities are to run the toplevel under emacs (a

Recommended reading on general debugging techinques

女生的网名这么多〃 提交于 2019-12-31 22:32:29
问题 What reading would you recommend on general debugging techniques? I am more interested in principles and best practices than in specific platform solutions. For the record I mainly work with .NET (F#, C#), and dabble in Haskell and Ocaml. One of these Friday evenings we talked about debugging with my colleague on our walk home. I was surprised to learn that one can view and modify the state of live objects from the VisualStudio debugger. He also mentioned that another developer he knew, a

How to represent a simple finite state machine in Ocaml?

十年热恋 提交于 2019-12-31 15:40:05
问题 I have written some state machine in C++ and Java but never in a functional language like Ocaml Problem is I don't know if I can just adapt code from the object languages versions, since in Ocaml records and variants are more powerful than class; So, I need an event-driven finite state machine (hierarchical like in UML), easily configurable Could someone experienced in the field post a simple sample of that ? Just to avoid the most common traps thanks :) EDIT 16/03 : Is it possible to do it

How to convert a string to integer list in ocaml?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 04:44:08
问题 I need to pass two list as command line arguments in ocaml. I used the following code to access it in the program. let list1=Sys.argv.(1);; let list2=Sys.argv.(2);; I need to have the list1 and list2 as list of integers. I am getting the error This expression has type string but an expression was expected of type int list while processing. How can I convert that arguments to a list of integers. The arguments are passed in this format [1;2;3;4] [1;5;6;7] 回答1: Sys.argv.(n) will always be a

Why does a partial application have value restriction?

我们两清 提交于 2019-12-31 04:31:13
问题 I can understand that allowing mutable is the reason for value restriction and weakly polymorphism. Basically a mutable ref inside a function may change the type involved and affect the future use of the function. So real polymorphism may not be introduced in case of type mismatch. For example, # let remember = let cache = ref None in (fun x -> match !cache with | Some y -> y | None -> cache := Some x; x) ;; val remember : '_a -> '_a = <fun> In remember, cache originally was 'a option , but

Computing a set of all subsets (power set)

…衆ロ難τιáo~ 提交于 2019-12-31 04:04:10
问题 I am trying to get a function (given as a parameter a set) to return a set whose elements are all the subset formed from the main set. Ex: {1;2;3} -> { {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3} } But I don't exactly know how to make a module that lets me work with a set of sets. What type should I define it as? 回答1: A set of all subsets is called a power set. To implement an algorithm you don't really need a special data structure, as you can represent a set with a list. Correspondingly a

Disable or enable code by preprocessor

大憨熊 提交于 2019-12-31 00:03:27
问题 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? 回答1: you can use cppo : https://github.com/mjambon/cppo. This is available via opam, and offers C like preprocessor features. 回答2: Without preprocessing you can simply have a global flag defined as let debug = true

Two fields of two records have same label in OCaml

坚强是说给别人听的谎言 提交于 2019-12-30 17:26:14
问题 I have defined two record types: type name = { r0: int; r1: int; c0: int; c1: int; typ: dtype; uid: uid (* key *) } and func = { name: string; typ: dtype; params: var list; body: block } And I have got an error later for a line of code: Error: The record field label typ belongs to the type Syntax.func but is mixed here with labels of type Syntax.name Could anyone tell me if we should not have two fields of two records have same label, like typ here, which makes compiler confuse. 回答1: No you

how to populate existing list / array

℡╲_俬逩灬. 提交于 2019-12-30 13:55:31
问题 I am new at reason / ocaml / functional programming. I know about List.append and [] @ [] but these functions will create new list but how to populate existing list / array? What is the best way to populate list? What is the best way to populate array? Means if coords type is let coords: array point = []; Or this is wrong flow (algorithm) for such case? Reason code: type point = {x: int, y: int}; let coords: list point = []; let append raw => Array.iter ( fun data => { let p = {x: data.x, y: