ocaml

List reversing in Ocaml

六月ゝ 毕业季﹏ 提交于 2019-11-28 10:10:40
问题 How to reverse even sublists of a list if we assume that we count elements from 0. I want the solution to be "manually-coded". I've got a big problem with this task. For example: Function([[1;2;3] ; [2;3] ; [1;2;3] ; [5;6;7]]) returns: ([[3;2;1] ; [2;3] ; [3;2;1] ; [5;6;7]]) I already created a function that reverse a single list: let rev = let rec rev_append acc l = match l with [] -> acc | h::t -> rev_append (h::acc) t in fun l -> rev_append [] l;; But now i am stuck. 回答1: let rev_list l =

Extending an existing type in OCaml

半城伤御伤魂 提交于 2019-11-28 09:45:08
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 Algebraic data types, right? So why not something like: type nbexp = bexp | Nop nbexp ;; ...but this isn't

OCaml explicit type signatures

回眸只為那壹抹淺笑 提交于 2019-11-28 09:37:34
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. OCaml has two ways of specifying types, they can be done inline: let intEq (x : int) (y : int) : bool = ... or they can be placed in an

Implementing a tail recursive version of quicksort-like function in F#/OCaML

故事扮演 提交于 2019-11-28 08:48:02
Is it possible to implement a tail recursive version of the quick sort algorithm (via the continuation pattern)? And if it is, how would one implement it? Normal (not optimized) version: let rec quicksort list = match list with | [] -> [] | element::[] -> [element] | pivot::rest -> let ``elements smaller than pivot``, ``elements larger or equal to pivot``= rest |> List.partition(fun element -> element < pivot) quicksort ``elements smaller than pivot`` @ [pivot] @ quicksort ``elements larger or equal to pivot`` Direct style: let rec quicksort list = match list with | [] -> [] | [element] ->

Recognizing arrow keys with stdin

独自空忆成欢 提交于 2019-11-28 08:32:22
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 program and it's not handled by code itself so I don't know if it's possible. The program is compiled either

OPAM package not running

痴心易碎 提交于 2019-11-28 07:55:26
问题 I've installed OPAM and run the initialization and updated my .bashrc code too. I then installed the ocamlfind package, which went successfully. But when I try #use "topfind";; I get the following Cannot find file topfind I'm running ocaml 4.00.1 built from source on Opensuse 12.2. 回答1: If $OCAML_TOPLEVEL_PATH is not defined, you have to defined it: export OCAML_TOPLEVEL_PATH=/home/%user%/.opam/%version%/lib/toplevel Don't forget to change %user% and your compiler %version% Check this issue

Where to place a shared utility module in OCaml?

做~自己de王妃 提交于 2019-11-28 07:40:48
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 it, and done ocamlfind install tools META tools.cmo tools.cmx tools.ml as suggested, which looks going

What is the “let” keyword in functional languages like F# and OCaml for?

≯℡__Kan透↙ 提交于 2019-11-28 07:05:44
问题 When looking at F#, Ocaml and other functional language code examples I notice that the let keyword is used very often. Why do you need it? Why were the languages designed to have it? Why can't you just leave it out? e.g: let x=4 becomes x=4 回答1: In F# (and OCaml) let is quite powerful construct that is used for value binding , which means assigning some meaning to a symbol. This can mean various things: Declaring local or global value - you can use it for declaring local values. This is

Why is OCaml's (+) not polymorphic?

你离开我真会死。 提交于 2019-11-28 06:48:28
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 +. ? 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 always knows exactly which numeric type is in use, thus making it easier to recognize when the programmer

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

百般思念 提交于 2019-11-28 06:20:44
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? 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 runtime system are going to move values around, and may merge and unmerge pure (non-mutable) values at will.