The OCaml Language Cheatsheets
The OCaml Language Cheatsheets
OCaml v.4.08.1
Syntax
Implementations are in .ml files, interfaces are in .mli files.
Comments can be nested, between delimiters (*...*)
Integers: 123, 1_000, 0x4533, 0o773, 0b1010101
Chars: 'a', '\255', '\xFF', '\n'
Floats: 0.1, -1.234e-34
Data Types
unit: (* void, takes only one value: () *)
int: (* integer of either 31 or 63 bits, like 42 *)
int32: (* 32 bits Integer, like 42l *)
int64: (* 64 bits Integer, like 42L *)
float: (* double precision float, like 1.0 *)
bool: (* boolean, takes two values: true or false *)
char: (* simple ASCII characters, like 'A' *)
string: (* strings, like "Hello" or foo|Hello|foo *)
bytes: (* mutable string of chars *)
'a list : (* lists, like head :: tail or [1;2;3] *)
'a array: (* arrays, like [|1;2;3|] *)
t1 * ... * tn: (* tuples, like (1, "foo", 'b') *)
Constructed Types
type record = (* new record type *)
{ field1 : bool; (* immutable field *)
mutable field2 : int; } (* mutable field *)
type enum = (* new variant type *)
| Constant (* Constant constructor *)
| Param of string (* Constructor with arg*)
| Pair of string * int (* Constructor with args *)
| Gadt : int -> enum (* GADT constructor *)
| Inlined of { x : int } (* Inline record *)
Constructed Values
let r = { field1 = true; field2 = 3; }
let r' = { r with field1 = false }
r.field2 <- r.field2 + 1;
let c = Constant
let c = Param "foo"
let c = Pair ("bar",3)
let c = Gadt 0
let c = Inlined { x = 3 }
References, Strings and Arrays
let x = ref 3 (* integer reference (mutable) *)
x := 4 (* reference assignation *)
print_int !x; (* reference access *)
s.[0] (* string char access *)
t.(0) (* array element access *)
t.(0) <- x (* array element modification *)
Imports - Namespaces
open Unix (* global open *)
let open Unix in expr (* local open *)
Unix.(expr) (* local open *)
Functions
let f x = expr (* function with one arg *)
let rec f x = expr (* recursive function, apply: f x *)
let f x y = expr (* with two args, apply: f x y *)
let f (x,y) = expr (* with a pair as arg, apply: f (x,y) *)
List.iter (fun x -> expr) (* anonymous function *)
let f = function None -> act (* function definition *)
| Some x -> act (* function definition [by cases] *)
(* apply: f (Some x) *)
let f ~str ~len = expr (* with labeled args *)
(* apply: f ~str:s ~len:10 *)
(* apply: (for ~str:str): f ~str ~len *)
let f ?len ~str = expr (* with optional arg (option) *)
let f ?(len=0) ~str = expr (* optional arg default *)
(* apply (with omitted arg): f ~str:s *)
(* apply (with commuting): f ~str:s ~len:12 *)
(* apply (len: int option): f ?len ~str:s *)
(* apply (explicitly omitted): f ?len:None ~str:s *)
let f (x : int) = expr (* arg has constrainted type *)
let f : 'a 'b. 'a*'b -> 'a (* function with constrainted *)
= fun (x,y) -> x (* polymorphic type *)
Modules
module M = struct .. end (* module definition *)
module M: sig .. end= struct .. end (* module and signature *)
module M = Unix (* module renaming *)
include M (* include items from *)
module type Sg = sig .. end (* signature definition *)
module type Sg = module type of M (* signature of module *)
let module M = struct .. end in .. (* local module *)
let m = (module M : Sg) (* to 1st-class module *)
module M = (val m : Sg) (* from 1st-class module *)
module Make(S: Sg) = struct .. end (* functor *)
module M = Make(M') (* functor application *)
Module type items: val, external, type, exception, module, open, include, class
Pattern-matching
match expr with
| pattern -> action
| pattern when guard -> action (* conditional case *)
| _ -> action (* default case *)
Patterns:
| Pair (x,y) -> (* variant pattern *)
| { field = 3; _ } -> (* record pattern *)
| head :: tail -> (* list pattern *)
| [1;2;x] -> (* list pattern *)
| (Some x) as y -> (* with extra binding *)
| (1,x) | (x,0) -> (* or-pattern *)
| exception exn -> (* try&match *)
Conditionals
Do NOT use on closures
x = y (* (Structural) Polymorphic Equality *)
x == y (* (Physical) Polymorphic Inequality *)
x <> y (* (Structural) Polymorphic Equality *)
x != y (* (Physical) Polymorphic Inequality *)
compare x y (* negative, when x < y *)
compare x y (* 0, when x = y *)
compare x y (* positive, when x > y *)
Other Polymorphic Comparisons: >, >=, <, <=
Loops
while cond do ... done;
for var = min_value to max_value do ... done;
for var = max_value downto min_value do ... done;
Exceptions
exception MyExn (* new exception *)
exception MyExn of t * t' (* same with arguments *)
exception MyFail = Failure (* rename exception with args *)
raise MyExn (* raise an exception *)
raise (MyExn (args)) (* raise with args *)
try expr (* catch MyExn *)
with MyExn -> ... (* if raised in expr *)
Objects and Classes
class virtual foo x = (* virtual class with arg *)
let y = x+2 in (* init before object creation *)
object (self: 'a) (* object with self reference *)
val mutable variable = x (* mutable instance variable *)
method get = variable (* accessor *)
method set z =
variable <- z+y (* mutator *)
method virtual copy : 'a (* virtual method *)
initializer (* init after object creation *)
self#set (self#get+1)
end
class bar = (* non-virtual class *)
let var = 42 in (* class variable *)
fun z -> object (* constructor argument *)
inherit foo z as super (* inheritance and ancestor reference *)
method! set y = (* method explicitly overridden *)
super#set (y+4) (* access to ancestor *)
method copy = {< x = 5 >} (* copy with change *)
end
let obj = new bar 3 (* new object *)
obj#set 4; obj#get (* method invocation *)
let obj = object .. end (* immediate object *)
Polymorphic variants
type t = [ `A | `B of int ] (* closed variant *)
type u = [ `A | `C of float ]
type v = [ t | u | ] (* union of variants *)
let f : [< t ] -> int = function (* argument must be a subtype of t *)
| `A -> 0 | `B n -> n
let f : [> t ] -> int = function (* t is subtype of the argument *)
|`A -> 0 | `B n -> n | _ -> 1
Reference
The OCaml Language Cheatsheets的更多相关文章
- OCaml Language Sucks
OCaml Language Sucks OCaml Language Sucks
- Github上的1000多本免费电子书重磅来袭!
Github上的1000多本免费电子书重磅来袭! 以前 StackOverFlow 也给出了一个免费电子书列表,现在在Github上可以看到时刻保持更新的列表了. 瞥一眼下面的书籍分类目录,你就能 ...
- Github 的一个免费编程书籍列表
Index Ada Agda Alef Android APL Arduino ASP.NET MVC Assembly Language Non-X86 AutoHotkey Autotools A ...
- [转]Keyword Reference (F#)
Visual F# Development Portal http://msdn.microsoft.com/en-us/library/vstudio/ff730280.aspx 本文转自:http ...
- gentoo use-flag 全局标识 大全 (官方搬运) 英文 适用funtoo
连接 https://www.gentoo.org/support/use-flags/ 提示 ctrl+F 可在页面查找 搬运 Global USE flags FlagDescription 3d ...
- function Language
什么是函数式语言: 函数式语言(functional language)一类程序设计语言.是一种非冯·诺伊曼式的程序设计语言.函数式语言主要成分是原始函数.定义函数和函数型.这种语言具有较强的组织数据 ...
- Function program language
历史 Lambda演算为描述函数及其评估提供了理论框架.它是一种数学抽象而不是编程语言 - 但它构成了几乎所有当前函数式编程语言的基础.等效的理论公式,组合逻辑,通常被认为比lambda演算更抽象,并 ...
- Functional Language
1.What is functional language? 函数式语言(functional language)一类程序设计语言,是一种非冯·诺伊曼式的程序设计语言.函数式语言主要成分是原始函数.定 ...
- 函数式编程语言(functional language)
内容根据百度词条整理! 转载请声明来源:https://baike.baidu.com/item/%E5%87%BD%E6%95%B0%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8 ...
- 函数式编程语言(Fuction Program Language)
一.什么是函数式编程语言 函数式编程语言(functional progarm language)一类程序设计语言,是一种非冯·诺伊曼式的程序设计语言.函数式语言主要成分是原始函数.定义函数和函数型. ...
随机推荐
- [转帖]陈巍谈芯:NLP里比BERT更优秀的XLNet长什么样?
https://zhuanlan.zhihu.com/p/447836322 目录 收起 一.XLNet的优势 1)独得AR与AE两大绝学 2)集成了Tansformer-XL 二.XLNet的结 ...
- TCP内核参数与Nginx配置的简单测试
背景 昨天晚上整理了下几个TCP内核的参数. 学习到了一点内核参数的影响. 但是因为时间比较晚了没有继续钻研与nginx的关系 今天想着继续研究一下TCP的部分参数与nginx的关系 每个系统都不一样 ...
- Ant Design Vue数字输入框InputNumber 有值但是验证却不能够通过
InputNumber 有值但是验证却不能够通过 今天遇见这样一个问题,InputNumber 输入框中有值 但是却却提示验证不能够通过 后来经过分析,怀疑是数据类型不正确, 后面经过验证,果然是数据 ...
- Ant Design Vue 单文件上传Upload
单文件上传 <a-upload name="file" :beforeUpload="beforeUpload" :multiple="fals ...
- VictoriaMetrics 1.80版本中值得关注的新特性
作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 change log请看:https://github.c ...
- Windows 核心编程笔记 [2] 字符串
1. ANSI 和 Unicode Windows 中涉及字符串的函数有两个版本 1)ANSI版本的函数会把字符串转换为Unicode形式,再从内部调用函数的Unicode版本 2)Unicode版本 ...
- PaddleHub--{超参优化AutoDL Finetuner}【二】
相关文章: 基础知识介绍: [一]ERNIE:飞桨开源开发套件,入门学习,看看行业顶尖持续学习语义理解框架,如何取得世界多个实战的SOTA效果?_汀.的博客-CSDN博客_ernie模型 百度飞桨: ...
- 特定领域知识图谱融合方案:文本匹配算法之预训练Simbert、ERNIE-Gram单塔模型等诸多模型【三】
特定领域知识图谱融合方案:文本匹配算法之预训练模型SimBert.ERNIE-Gram 文本匹配任务在自然语言处理中是非常重要的基础任务之一,一般研究两段文本之间的关系.有很多应用场景:如信息检索.问 ...
- 6.5 Windows驱动开发:内核枚举PspCidTable句柄表
在 Windows 操作系统内核中,PspCidTable 通常是与进程(Process)管理相关的数据结构之一.它与进程的标识和管理有关,每个进程都有一个唯一的标识符,称为进程 ID(PID).与之 ...
- MySQL 权限与备份管理(精简笔记)
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RD ...