ocaml-core

Understand Core's `Fn.const`

余生颓废 提交于 2019-12-12 07:57:56
问题 Jane Street's Core lib has such a function: Fn.const . https://github.com/janestreet/core_kernel/blob/master/lib/fn.ml let const c = (); fun _ -> c val const : 'a -> 'b -> 'a produces a function that just returns its first argument I really don't understand it. What's the purpose of this function? In what scenario we have to use it? Why put (); first? Why not write it as let const c = fun () -> c ? this will give a function taking unit as parameter and always returns initial c . If I do let f

Core's `List.init` in Pervasives?

时光总嘲笑我的痴心妄想 提交于 2019-12-11 06:09:48
问题 I'm used to JaneStreet's Core library. Its List module has a neat init function: List.init;; - : int -> f:(int -> 'a) -> 'a list = <fun> It allows you to create a list with using a custom function to initialize elements: List.init 5 ~f:(Fn.id);; - : int list = [0; 1; 2; 3; 4] List.init 5 ~f:(Int.to_string);; - : string list = ["0"; "1"; "2"; "3"; "4"] However, this function doesn't seem to exist in Pervasives , which is sad. Am I missing something, or do I have to implement it myself? And if

Why is my string of type `bytes`?

瘦欲@ 提交于 2019-12-08 15:13:25
问题 According to Real World OCaml , the type of "abc" should be string . But actually in my utop REPL, it's of type bytes . I've already opened Core.Std . Why is that? (The version of OCaml is 4.02.2 ; Core is 112.24.01 ; utop is 1.18 .) 回答1: You must enable safe string mode explicitly. Just start utop with: $ utop -safe-string Before the introduction of type bytes in OCaml 4.02, strings were mutable. Now, strings are intended to be immutable, and bytes is the type to be used for "mutable strings

Understand Core's `Fn.const`

醉酒当歌 提交于 2019-12-03 13:18:11
Jane Street's Core lib has such a function: Fn.const . https://github.com/janestreet/core_kernel/blob/master/lib/fn.ml let const c = (); fun _ -> c val const : 'a -> 'b -> 'a produces a function that just returns its first argument I really don't understand it. What's the purpose of this function? In what scenario we have to use it? Why put (); first? Why not write it as let const c = fun () -> c ? this will give a function taking unit as parameter and always returns initial c . If I do let f = const 5 , f will become a function which takes '_a as parameter. What's the purpose of returning a