ocaml

Point-free: confused about where to put parenthesis

拈花ヽ惹草 提交于 2019-12-11 13:16:05
问题 let list_to_string = (String.concat "") (List.map (String.make 1));; This is wrong, but how do I make it understand that the argument is still to be supplied? The argument is expected to be of type char list , I.e. the first function that needs to be applied to it is the (List.map (String.make 1)) , and then pass it to String.concat "" . I think I've tried all combinations of parenthesis I could think of... no joy so far. Help? I also figured I could do it like this: let ($) f g x = f (g x);;

Optional argument in a method with ocaml

北慕城南 提交于 2019-12-11 13:00:14
问题 I encounter a problem with a optional argument in a method class. let me explain. I have a pathfinding class graph (in the Wally module) and one his method shorthestPath . It use a optional argument. The fact is when I call (with or not the optional argument) this method OCaml return a conflict of type : Error: This expression has type Wally.graph but an expression was expected of type < getCoor : string -> int * int; getNearestNode : int * int -> string; shorthestPath : src:string -> string

Losing type precision from module signature

两盒软妹~` 提交于 2019-12-11 12:42:44
问题 Let's say I had a simple module MyFoo that looks something like this module MyFoo = struct type t = | A of string | B of int let to_string thing = match thing with | A str -> str | B n -> string_of_int n end With this definition, it works great and as expected — I can do something like let _ = MyFoo.A "";; - : MyFoo.t = MyFoo.A "" without any problems. Now maybe I want to create a functor that consumes modules with this structure, so I define a module signature that describes generally what

Actually changing text size with OCaml Graphics

痴心易碎 提交于 2019-12-11 12:19:19
问题 I was wondering how to set text size in OCaml. I tried Graphics.set_text_size which should do the deal I guess. But whether I put set_text_size 200 or set_text_size 20 doesn't change a thing… 回答1: Graphics.set_text_size is not implemented yet. https://github.com/ocaml/ocaml/blob/trunk/otherlibs/graph/graphics.ml#L165 https://github.com/ocaml/ocaml/blob/trunk/otherlibs/graph/text.c#L36 https://github.com/ocaml/ocaml/blob/trunk/otherlibs/win32graph/draw.c#L350 回答2: maybe for now, Graphics.set

ocaml - deoptionalize a list: is there a simpler way?

馋奶兔 提交于 2019-12-11 12:18:30
问题 I have written a function to deoptionalize an integer list and I would like to know if there is a better way to write it. let deoptionalize (lst:'a option list) : 'a list = List.map ~f:(fun x -> match x with Some x -> x | None -> assert false) (List.filter ~f:(fun x -> x <> None) lst) ;; In the assignment I am currently working its using map and filter is a must. 回答1: I suppose that a "hand-coded" solution (i.e. without map and filter ) is easier to read, but if you really need to use them,

Building OCaml code that uses list comprehension

大城市里の小女人 提交于 2019-12-11 12:17:03
问题 From this SO question: List Comprehension in Ocaml?, I could install the comprehension package with opam install pa_comprehension , and use the package in toplevel REPL. # #require "pa_comprehension";; # open Batteries;; # [? 2 * x | x <- 0 -- max_int ; x * x > 3 ?];; - : int Batteries.Enum.t = <abstr> Then, how can I compile the code? 回答1: Unfortunately, since pa_comprehension package name is not ended with .syntax extension, this is a little bit harder than it should be. (The fact that this

How do I use List.fold_left?

假装没事ソ 提交于 2019-12-11 11:45:22
问题 I'm still trying to understand how fold_left exactly works. Does it iterate through the list like List.iter ? Or is there just something else wrong with my code? I'm thinking that e is the element in the list (so it's a tuple) and fst e takes the first element of the tuple and snd e takes the second element in the tuple. let rec pow x n = if n < 0 then 0 else if n = 0 then 1 else x * pow x (n - 1);; let polynomial lst = function | x -> List.fold_left (fun e -> (fst e) * (pow x (snd e))) 1 lst

Square a list of lists - ocaml

别来无恙 提交于 2019-12-11 11:09:46
问题 I know how to square elements of a list, but how to square in list of lists? To square an element of a list i could use, for example: List.map (fun x -> x*x) [1; 2; 3];; How to do this on list of lists? [[1; 2]; [2; 3]] --> [[1; 4]; [4; 9]] or [[1; 2; 3]; [4; 2; 0]] --> [[1; 4; 9]; [16; 4; 0]] for example. Thanks 回答1: let square = fun x -> x * x;; (* val square : int -> int = <fun> *) List.map square;; (* - : int list -> int list = <fun> *) List.map (List.map square);; (* - : int list list ->

How to get a binary representation of Uint64 in OCAML

风流意气都作罢 提交于 2019-12-11 10:56:20
问题 Suppose I have this let var = Uint64.of_string "0x15E" ;; How do I convert the above no. to get a binary string which would be 0b101011110 Are there any libraries that would assist in this ? Any help regarding this matter would be appreciated. 回答1: It looks like you're using the uint module from OPAM. The uint module has a function named Uint64.to_string_bin that sounds like it does what you want. I don't have the module installed at the moment, so I can't try it. 来源: https://stackoverflow

Function applied to too many arguments in lablgtk

可紊 提交于 2019-12-11 09:39:48
问题 I need some help understanding why the following code will not compile. I am attempting to place a GSourceView2 inside a vertical box in lablgtk. I've adapted the code from this Ocaml article open GMain open GdkKeysyms let locale = GtkMain.Main.init () let main () = let window = GWindow.window ~width:320 ~height:240 ~title:"Simple lablgtk program" () in let vbox = GPack.vbox ~packing:window#add () in window#connect#destroy ~callback:Main.quit; (* Sourceview *) let _ = new GSourceView2.source