ocaml

Extending an existing type in OCaml

送分小仙女□ 提交于 2019-11-27 05:52:04
问题 I've been doing some OCaml programming lately to learn the language and to get more acquainted with functional programming. Recently, I've started to think that I'd like to be able to extend an existing type (either built in-or one of my own), for example: type bexp = And of bexp * bexp | Or of bexp * bexp | Xor of bexp * bexp | Not of bexp;; Now let's say I want to add a Nop variant to this type, but only for use in a new type - kind of like inheritance. Hey, these are supposed to be

F# changes to OCaml [closed]

一个人想着一个人 提交于 2019-11-27 05:51:51
F# is derived from OCaml, but what major items are missing or added? Specifically I'm curious as to whether the resources available for learning OCaml are also useful to someone who wants to learn F#. Chris Conway The main differences are that F# does not support: functors OCaml-style objects polymorphic variants the camlp4/5 preprocessor or extension points (ppx) In addition, F# has a different syntax for labeled and optional parameters. In theory, OCaml programs that don't use these features can be compiled with F#. Learning OCaml is a perfectly reasonable introduction to F# (and vice versa,

OCaml explicit type signatures

孤街浪徒 提交于 2019-11-27 05:51:07
问题 In Haskell, it is considered good practice to explicitly declare the type signature of your functions, even though it can (usually) be inferred. It seems like this isn't even possible in OCaml, e.g. val add : int -> int -> int ;; gives me an error. (Although I can make type modules which give only signatures.) Am I correct in that this isn't possible to do in OCaml? If so, why? The type system of OCaml doesn't seem that incredibly different from Haskell. 回答1: OCaml has two ways of specifying

Print a List in OCaml

亡梦爱人 提交于 2019-11-27 05:41:14
问题 I want to do something as simple as this: Print a list. let a = [1;2;3;4;5] How can I print this list to Standard Output? 回答1: You can do this with a simple recursion : let rec print_list = function [] -> () | e::l -> print_int e ; print_string " " ; print_list l The head of the list is printed, then you do a recursive call on the tail of the list. 回答2: You should become familiar with the List.iter and List.map functions. They are essential for programming in OCaml. If you also get

In Functional Programming, what is a functor?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 02:28:58
I've come across the term 'Functor' a few times while reading various articles on functional programming, but the authors typically assume the reader already understands the term. Looking around on the web has provided either excessively technical descriptions (see the Wikipedia article ) or incredibly vague descriptions (see the section on Functors at this ocaml-tutorial website ). Can someone kindly define the term, explain its use, and perhaps provide an example of how Functors are created and used? Edit : While I am interested in the theory behind the term, I am less interested in the

What is the difference between 'a and '_l?

匆匆过客 提交于 2019-11-27 02:05:42
What is the difference between 'a and '_l ? I was looking at this error, and couldn't comprehend it: Error: This expression has type ('a -> float polynomial) list but an expression was expected of type float polynomial list derivlist: ('_l → float polynomial) list gasche _ denotes a weakly polymorphic variable : it is in a position where it cannot be generalized. There are two explanations related to weak polymorphism in the OCaml FAQ: see A function obtained through partial application is not polymorphic enough and the next one. This generally happens when you're using a non-local reference

Recognizing arrow keys with stdin

对着背影说爱祢 提交于 2019-11-27 02:00:38
问题 is it possible to have a cross-platform way to handle backspace and arrows keys within a C or OCaml program? Actually an OCaml solution would be appreciated but many standard unix functions are wrapped directly to corresponding API calls so there's should be no problem in porting a C solution. What I'm going to achieve is to catch the arrow keys to override its behaviour inside the shell (by repropting last line or operations like these). I think that this thing falls before the actual

Where to place a shared utility module in OCaml?

和自甴很熟 提交于 2019-11-27 01:55:05
问题 I have a file Tools.ml which contains some common utility functions I write myself. Under .../Code/ I have several folders which each contains a project. My question is where I should place this Tools.ml such that all the folders and files under .../Code/ could share this module by Open Tools . Hope my question is clear... Does anyone have a good solution? Edit1: Following @gasche's answer, I have written tools.ml as follows: module Tools = struct let a_function = ... ... end Then I compiled

Why is OCaml's (+) not polymorphic?

泪湿孤枕 提交于 2019-11-27 01:30:03
问题 I am an OCaml newbie. I like OCaml's speed but I don't fully understand its design. For example, I would like the + operator to be polymorphic to support integer, float and so on. Why do we need +. ? 回答1: Ocaml does not support polymorphic operators (numeric or otherwise) other than comparison operators. The + versus +. thing removes a lot of subtle bugs which can crop up in converting different sizes of integers, floats, and other numeric types back and forth. It also means that the compiler

What's the difference between “equal (=)” and “identical (==)” in ocaml?

别来无恙 提交于 2019-11-27 01:17:00
问题 In OCaml , we have two kinds of equity comparisons : x = y and x == y , So what's exact the difference between them? Is that x = y in ocaml just like x.equals(y) in Java? and x == y just like x == y (comparing the address) in Java? 回答1: I don't know exactly how x.equals(y) works in Java. If it does a "deep" comparison, then the analogy is pretty close. One thing to be careful of is that physical equality is a slippery concept in OCaml (and functional languages in general). The compiler and