ocaml

Ocaml representation of values - Atoms

匿名 (未验证) 提交于 2019-12-03 01:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I looked at the internal representation of some OCaml values. The representation of an empty array is an atom(0) , i.e. a block with tag=0 and size=0 . Empty arrays of floats are represented by an atom(0) too. Is there any OCaml value represented by an atom with tag > 0 ? If not: for what purpose the OCaml bytecode set contains the ATOM n instruction? 回答1: A tag > 0 is used for constructors with arguments, which would make them not atoms. Constructors without arguments on the other hand are stored as int instead of blocks so again not atoms.

F# vs OCaml: Stack overflow

被刻印的时光 ゝ 提交于 2019-12-03 01:17:47
问题 I recently found a presentation about F# for Python programmers, and after watching it, decided to implement a solution to the "ant puzzle" on my own. There is an ant which can walk around on a planar grid. The ant can move one space at a time left, right, up or down. That is, from cell (x, y) the ant can go to cells (x+1, y), (x-1, y), (x, y+1), and (x, y-1). Points where the sum of the digits of the x and y coordinates are greater than 25 are inaccessible to the ant. For example, the point

What's the reason of 'let rec' for impure functional language OCaml?

自闭症网瘾萝莉.ら 提交于 2019-12-03 00:59:37
In the book Real World OCaml , the authors put why OCaml uses let rec for defining recursive functions. OCaml distinguishes between nonrecursive definitions (using let) and recursive definitions (using let rec) largely for technical reasons: the type-inference algorithm needs to know when a set of function definitions are mutually recursive, and for reasons that don't apply to a pure language like Haskell, these have to be marked explicitly by the programmer. What are the technical reasons that enforces let rec while pure functional languages not? ivg When you define a semantics of function

Ideas for Natural Language Processing project? [closed]

☆樱花仙子☆ 提交于 2019-12-03 00:58:55
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I have to do a final project for my computational linguistics class. We've been using OCaml the entire time, but I also have

“Error: unbound module” in OCaml

匿名 (未验证) 提交于 2019-12-03 00:52:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Here's a simple example of using the library Cohttp: open Lwt open Cohttp open Cohttp_lwt_unix let body = Client.get (Uri.of_string "http://www.reddit.com/") >>= fun (resp, body) -> let code = resp |> Response.status |> Code.code_of_status in Printf.printf "Response code: %d\n" code; Printf.printf "Headers: %s\n" (resp |> Response.headers |> Header.to_string); body |> Cohttp_lwt.Body.to_string >|= fun body -> Printf.printf "Body of length: %d\n" (String.length body); body let () = let body = Lwt_main.run body in print_endline ("Received body

When should objects be used in OCaml?

人盡茶涼 提交于 2019-12-03 00:29:56
问题 Usually, an OCaml program can be written with or without objects. When is it most beneficial to use objects, and when should they be avoided? 回答1: As a general rule of thumb, don't use objects. The additional complexity they bring in is not that often worth it. I think that's a rule that apply to other languages as well, but that's another story. At least with OCaml one can objectively (no pun intended) say that the common practice is to not use object except in rare cases. Object provide a

Using Opam to manage project dependencies

冷暖自知 提交于 2019-12-02 23:02:56
I am a complete newbie to OCaml. Other languages I have used (for instance Scala, Clojure, Javascript on Node.js) have package managers that allow one to start a project as a clean slate that has a declared set of dependencies of known versions. I am trying to do something of the sort with Opam. Ideally, I would like to have a file that lists dependencies (and possibly OCaml version) so that a collaborator can start a project with git clone myproject <magic opam command> ocamlbuild and have a working version, without having installed anything globally. I understand that the right way to do

What is a principal type?

ぐ巨炮叔叔 提交于 2019-12-02 22:34:31
The OCaml compiler has a "-principal" option and the term "principal type" is sometimes mentioned in the mailing list. What exactly does it mean? The definition in Wikipedia is recursive, as it assumes the reader is already familiar with the notion. gasche The process of type inference is the fact of guessing, given an user-written program, what the type of this program is. In general, there may be several correct types for a given program. For example, the program fun x -> x can be given the types int -> int and bool -> bool . Given a program, a type for this program is principal if it is the

In OCaml, what type definition is this: 'a. unit -> 'a

烂漫一生 提交于 2019-12-02 22:26:17
Questions It was my first time I saw a type definition like 'a. unit -> 'a in Explicit polymorphic type in record Q1 : What is this 'a. (notice the dot)? Q2 : What is the terminology for this kind of type definition? If I do let f:'a. 'a list -> int = fun l -> List.length l;; utop shows val f : 'a list -> int = <fun> Q3 : Why doesn't utop shows the type 'a. 'a list -> int ? Q4 : When should I use this kind of type definition? In addition, I can use this kind of definition in record: type t = { f: 'a. 'a list -> int};; (* this is correct *) but I can't use it in variants: type t = Node of ('a.

What are type quantifiers?

和自甴很熟 提交于 2019-12-02 22:20:59
Many statically typed languages have parametric polymorphism. For example in C# one can define: T Foo<T>(T x){ return x; } In a call site you can do: int y = Foo<int>(3); These types are also sometimes written like this: Foo :: forall T. T -> T I have heard people say "forall is like lambda-abstraction at the type level". So Foo is a function that takes a type (for example int), and produces a value (for example a function of type int -> int). Many languages infer the type parameter, so that you can write Foo(3) instead of Foo<int>(3) . Suppose we have an object f of type forall T. T -> T .