Real World OCaml

ISBN 978-7-5123-7637-3

English First Edition PDF

Code Examples

Install Guidence from cornell/cs3110

Part I

based on Core instead of standard library

1 / 3;;
(* - : int = 0 *)

1 / 3.;;
1. /. 3;; 
2. / 3.;;
(* Error: This expression has type float but an expression was expected of type int *)

1. /. 3.;;
(* - : float = 0.333333333333333315 *)

let even x =
  x mod 2 = 0 ;;
(* val even : int -> bool = <fun> *)

Identifiers for variable names:
punctuation is excluded, except for _ and ', must start with a lowercase letter or an underscore.
By default, utop doesn’t print out variables starting with an underscore.

first-class functions

Module names
always start with a capital letter.

type inference

let sum_if_true test first second =
  (if test first then first else 0)
  + (if test second then second else 0)
;;
(* val sum_if_true : (int -> bool) -> int -> int -> int = <fun> *)

let sum_if_true (test : int -> bool) (x : int) (y : int) : int =
  (if test x then x else 0)
  + (if test y then y else 0)
;;

parametric polymorphism

generic type & type variable

let first_if_true test x y =
  if test x then x else y
;;
(* val first_if_true : ('a -> bool) -> 'a -> 'a -> 'a = <fun> *)

* compile time error & runtime exception

built-in simple types:
int, float, bool, char, string, unit

pattern matching

let t = (3, "four", 5.);;
(* val t : int * string * float = (3, "four", 5.) *)
let (x, y, z) = t;;

* tuple: fixed item number, different types

* list: any item number, the same type

~f: labeled argument

let langs = ["ocaml", "cpp", "c"];;
(* a singleton list containing a three-tuple
 * val langs : (string * string * string) list = [("ocaml", "cpp", "c")]
 *)

let langs = ["ocaml"; "cpp"; "c"];;
(* val langs : string list = ["ocaml"; "cpp"; "c"] *)
List.map langs ~f:String.length;;
(* - : int list = [5; 3; 1] *)

"python" :: langs;;
(* - : string list = ["python"; "ocaml"; "cpp"; "c"] *)
langs;;
(* - : string list = ["ocaml"; "cpp"; "c"] *)

["py"; "perl"] @ langs;;
(* - : string list = ["py"; "perl"; "ocaml"; "cpp"; "c"] *)

The bracket notation for lists is really just syntactic sugar for :: (right-associative).

Unlike ::, @ is not a constant-time operation (proportional to the length of the first list).

let my_fav_lang (my_fav :: the_rest) = my_fav;;
(* Here is an example of a case that is not matched: [] *)

let my_fav_lang langs =
  match langs with
  | first :: the_rest -> first
  | [] -> "ocaml"
;;

recursive

let rec sum l =
  match l with
  | [] -> 0
  | hd :: tl -> hd + sum tl
;;

option

let divide x y =
  if y = 0 then None else Some (x/y) ;;
(* val divide : int -> int -> int option = <fun> *)

A let paired with an in can be used to introduce a new binding within any local scope, including a function body.
The in marks the beginning of the scope within which the new variable can be used.

let x = 7 in
  let y = x * x in
    x + y
;;

record

type point2d = {x : float; y : float};;
let p = {x = 3.; y = -4.};;
(* val p : point2d = {x = 3.; y = -4.} *)

variant

type v = 
  | Int of int
  | Point2D of point2d
;;
(* type v = Int of int | Point2D of point2d *)

array

let numbers = [| 1; 2; 3; 4 |];;
(* val numbers : int array = [|1; 2; 3; 4|] *)

numbers.(2) <- 4;;
(* - : unit = () *)

The unit type has only one possible value, written (), generally used as a placeholder.
We use unit for the return value of an operation like setting a mutable field that communicates by side effect rather than by returning a value. It’s also used as the argument to functions that don’t require an input value (like void in C or Java).

mutable record

type running_sum =
{
  mutable sum : float;
  mutable samples : int;
};;

let mean rsum = rsum.sum /. float rsum.samples
let create () = {sum = 0.; samples = 0}
let update rsum x =
  rsum.samples <- rsum.samples + 1;
  rsum.sum <- rsum.sum +. x
;;
(* val mean : running_sum -> float = <fun>
 * val create : unit -> running_sum = <fun>
 * val update : running_sum -> float -> unit = <fun>
 *)

let rsum = create ();;
List.iter [1.;3.;2.;-7.;4.;5.] ~f:(fun x -> update rsum x);;
mean rsum;;

Anonymous functions
are declared using the fun keyword, and don’t need to be ex‐ plicitly named.

ref:
the standard way of simulating the traditional mutable variables

let x = {contents = 0};;
x.contents <- x.contents + 1;;
(* equivalent to *)
let x = ref 0;;
x := !x + 1;;
(* implementations *)
type 'a ref = {mutable contents : 'a}

let ref x = {contents = x}
let (!) r = r.contents
let (:=) r x = r.contents + x
;;

The parentheses around ! and := are needed because these are operators, rather than ordinary functions.

let sum list =
  let sum = ref 0 in
    List.iter list ~f:(fun x -> sum := !sum + x);
  !sum
;;

for & while

out of toplevel

(* sum.ml *)
open Core

let rec read_and_accumulate accum =
  let line = In_channel.input_line In_channel.stdin in
  match line with
  | None -> accum
  | Some x -> read_and_accumulate (accum +. Float.of_string x)

let () =
  printf "Total: %F\n" (read_and_accumulate 0.)
corebuild sum.ml

Part II

anonymous & higer-order function

let increments = [ (fun x -> x + 1); (fun x -> x + 2) ];;
(* val increments : (int -> int) list = [<fun>; <fun>] *)

List.map ~f:(fun g -> g 5) increments;;
(* - : int list = [6; 7] *)

currying

let abs_diff x y = abs (x - y);;
(* equivalent to *)
let abs_diff =
  (fun x -> (fun y -> abs (x - y)));;

(* partial application *)
let dist_from_3 = abs_diff 3;;

or pattern

| [] | [_]

Explicitly marking of recursion doesn’t apply to a pure language like Haskell.

Predetermined identifiers characters set and strings:
! $ % & * + - . / : < = > ? @ ^ | ~
or mod lsl (logical shift left)

(+) 1 2;;
(* - : int = 3 *)

Int.max 3 -4;; (* error *)
Int.max 3 (-4);;

let (|>) x f = f x;;
(* val ( |> ) : 'a -> ('a -> 'b) -> 'b = <fun> *)

Negation has lower precedence than function application.

function keyword

let some_or_zero function
  | Some x -> x
  | None -> 0
;;

labeled argument

let ratio ~num ~denom = float num /. float denom;;
(* val ratio : num:int -> denom:int -> float = <fun> *)

ratio ~denom:10 ~num:3;;

Label punning:
you get to drop the text after the : if the name of the label and the name of the variable being used are the same.

TODO

  1. files
  2. modules
  3. functors
  4. objects
  5. classes

Part III

TODO

  1. s-expression
  2. concurrency
  3. FFI
  4. GC
  5. compiler frontend

转载于:https://www.cnblogs.com/humz/p/10679849.html

真实世界中的SOA 微软 SOA in the Real World.pdf.zip 微软出版。英文 立即下载

相关推荐

OpenFoam实用技巧——starccm网格高效转换openfoam格式全解析

本文详细解析了如何利用OpenFOAM官方工具`ccm26toFoam`,将STAR-CCM+生成的高质量网格高效、稳定地转换为OpenFOAM格式。内容涵盖从STAR-CCM+正确导出网格、执行转换命令、解读转换日志,到转换后关键的边界条件修复与网格质量检查等全流程,旨在帮助用户规避常见错误,实现无缝工作流衔接。

weixin_28309571的博客 457

探秘Real World OCaml实战示例仓库:掌握函数式编程新境界

探秘Real World OCaml实战示例仓库:掌握函数式编程新境界 项目简介 是一个面向实践者的OCaml语言学习资源库,它提供了丰富的代码示例,旨在帮助开发者深入理解OCaml这门强大的函数式编程语言。该项目源自《Real World OCaml》这本书,通过解决实际问题,展示了OCaml的强大功能和优雅语法。 技术分析 OCaml是一种静态类型的、强类型、编译型的函数式编程语言。它的特点包...

gitblog_00082的博客 410

WorkBuddy驱动SolidWorks实测:AI建模流程、参数化与许可排查指南

AI辅助三维建模正在改变工程师的重复劳动方式。其核心理念不是替代人的设计决策,而是通过AI智能体接管CAD软件中的草图绘制、特征创建、尺寸修改等高频操作,让工程师从繁琐的GUI交互中解放出来。在SolidWorks这类主流CAD平台上,这类AI助手的落地依赖环境稳定、需求描述清晰和参数化建模规范。通过将外径、孔距等关键尺寸定义为全局变量并用方程式驱动,可实现改一个数、模型自动更新的高效流程;同时,遇到“无法获得许可”等环境故障时,需要按服务状态、安全软件隔离、干净卸载的顺序排查。以WorkBuddy驱动So

weixin_28559803的博客 369

开源项目教程:Real World OCaml 示例

开源项目教程:Real World OCaml 示例 1. 项目的目录结构及介绍 realworldocaml/ ├── examples/ │ ├── basic/ │ │ ├── hello_world.ml │ │ ├── variables.ml │ │ └── ... │ ├── advanced/ │ │ ├── concurrency.ml │...

gitblog_00902的博客 520

开源网格生成利器Gmsh:从入门到二次开发实战指南

本文详细介绍了开源网格生成工具Gmsh,从基础概念、安装配置到核心功能实战,提供了全面的入门指南。重点阐述了其强大的二次开发能力,通过Python API实现参数化建模、边界层网格生成以及与FEniCS、OpenFOAM等求解器的集成,为有限元分析和计算流体力学仿真提供灵活高效的自动化网格划分解决方案。

weixin_29066781的博客 435

SMRATS(一) SMARTS+PPO baseline运行教程

python类方法 class Agent: @classmethod def from_function() python typing 含义 为防止弱类型python出现忘记变量类型的问题,python3.5及之后版本增加了对应的类型提示。 输入和输出类型 函数接受并返回一个字符串,注释像下面这样: def greeting(name: str) -> str: return 'Hello ' + name 常用的typing类型 int,long,float: 整型,

Freshduke的博客 1414

探索现实世界中的OCaml:Real World OCaml 第二版源码解析

OCaml是一种功能强大的多范式编程语言,以其静态类型和高效的编译器著称。《Real World OCaml》第二版是深入学习该语言的权威指南,由Yaron Minsky、Anil Madhavapeddy等专家编写,并持续更新中。现在,这个开源项目正等待你的探索。 ## 项目简介 这个开源仓库包含了《Real World OCaml》第二版的所有章节源代码,每个章节都放在`book/`目录下

gitblog_00091的博客 561

Spring Boot 构建war 部署到tomcat下无法在Nacos中注册服务

因此,当使用外部容器时,如此处的 Tomcat 来部署项目,AbstractAutoServiceRegistration 就不能监听到容器启动事件了,也就不会尝试向服务注册中心注册当前这个微服务,那么注册就失败了,并且也就没有异常信息了。自定义获取获取外部容器端口的方法, 然后监听应用启动事件,当应用被启动时,获取外部容器启动的端口号,然后将这个 port 设置到 NacosAutoServiceReigistration 中。在启动类同级目录,复制下面代码即可(不需要修改,直接构建war即可)

ttyy1112的专栏 2519

ts 字符串不能做索引异常提示 type because expression of type ‘string‘

let c=a[b]就会爆这个错误,因为在编译器看来b是一个未知的东西,它不属于a下面的任何一个属性b或者c,所以我们需要告诉它。

小漠007的专栏 623

深入解析poi-tl自定义表格渲染插件中的多级子循环实现

本文深入解析了poi-tl自定义表格渲染插件中的多级子循环实现,详细介绍了其在处理复杂业务数据时的核心价值、实现原理及实战技巧。通过多级子循环机制,开发者可以高效地将层级数据结构渲染到Word表格中,显著提升代码简洁性和维护效率。文章还分享了性能优化和异常处理经验,帮助开发者更好地应用该插件。

weixin_33734785的博客 484

当R语言遇见MaxENT:参数优化的艺术与自动化调参实践

本文探讨了R语言与MaxENT在生态位建模中的结合应用,重点介绍了参数优化与自动化调参的实践方法。通过caret与ENMeval工具链,研究人员可以高效处理多物种、多环境变量的大规模分析任务,提升模型性能与工作效率。文章还涵盖了从数据准备到结果可视化的全流程,为生态学家提供了实用的技术指导。

corn8的博客 455

编程英语:常见代码错误 error 语句学习(11)

编程英语:常见代码错误 error 语句学习(10) error 1:String expression expected 缺字符串表达式 error 2:Circular unit reference 单元 部件循环引用 error 3:Unit name mismatch 单元名不匹配 error 4:Unit version mismatch 单元版本不匹配 error 5:Internal stack overflow 内部堆栈溢出 历史: 编程英语:

集电极 459

《网络安全自学教程》- 防火墙原理分析与Windows防火墙使用配置

防火墙分类及原理,Windows开启防火墙,Windows防火墙添加策略,

wangyuxiang946的博客 1万+

基于 Trackio 记录训练指标的完整指南:在 ai-engineering-hub 中用 wandb 兼容 API 实现本地优先的实验跟踪与 HF Space 实时可视化

本文以 Hugging Face 官方轻量级实验跟踪库 **Trackio** 为核心,系统讲解如何在 `ai-engineering-hub` 仓库的 hugging-face-skills 体系中,通过 Python API 完成训练指标的记录、经 HF Space 实现远程实时可视化,并与 TRL 训练器(SFT/DPO/GRPO)无缝集成。读完本文,你将掌握 `trackio.init/l

gitblog_00548的博客 794

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Real World OCaml V2开发环境搭建:简单步骤轻松上手

Real World OCaml V2是面向实际应用的OCaml编程指南,本文将带你通过简单步骤快速搭建完整的开发环境,让你轻松开启OCaml编程之旅。无论是OCaml新手还是有一定经验的开发者,都能通过这份指南快速配置好高效的开发环境。 ## 📚 准备工作:安装基础依赖 在开始搭建Real World OCaml V2开发环境前,需要确保系统中已安装以下基础工具: - **OCaml编译

gitblog_00449的博客 1043

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

解锁OCaml编程潜能:《Real World OCaml》第二版完全指南

《Real World OCaml》第二版是面向现代开发者的终极OCaml学习资源,通过实用案例和最佳实践帮助你掌握这门强大的函数式编程语言。本指南将带你快速了解如何利用这本书提升OCaml编程技能,从基础语法到高级应用场景全面覆盖。 ## 📚 为什么选择《Real World OCaml》第二版? 作为OCaml领域的权威著作,第二版进行了全面更新,涵盖了最新的语言特性、工具链和实战案例。

gitblog_00759的博客 664

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

终极指南:如何快速掌握Real World OCaml第二版编程技巧

OCaml是一种功能强大的函数式编程语言,以其高效的编译器和静态类型系统而闻名。Real World OCaml第二版作为该领域的权威学习资料,通过大量实际案例帮助开发者从入门到精通。本书由Yaron Minsky和Anil Madhavapeddy领衔编写,汇集了多位OCaml专家的宝贵经验。 ## 为什么选择Real World OCaml第二版? ### 完整的学习路径设计 本书按照循序

gitblog_00072的博客 288

"Real World OCaml" Installation Instructions

https://github.com/realworldocaml/book/wiki/Installation-Instructions

bbtree的专栏 663

Real World OCaml V2网络编程实战:使用Cohttp构建HTTP客户端与服务器

Real World OCaml V2是一本全面的OCaml实战指南,其中网络编程章节详细介绍了如何使用Cohttp库构建高效的HTTP客户端与服务器。本文将带你快速掌握这一实用技能,从基础概念到实际应用,让你轻松上手OCaml网络开发。 ## 🚀 OCaml网络编程基础 OCaml作为一门函数式编程语言,在网络编程领域展现出独特的优势。其强大的类型系统和并发模型,结合Cohttp库的高效实

gitblog_01181的博客 952

Real World OCaml V2类与对象编程:从基础到高级应用

Real World OCaml V2是一本全面的OCaml编程指南,其中类与对象编程是面向对象设计的核心部分。本文将带您从基础概念出发,逐步掌握OCaml中类与对象编程的高级应用技巧,帮助您构建更加模块化和可维护的OCaml应用程序。 ## OCaml类与对象编程基础入门 🚀 ### 类与对象的基本概念 在OCaml中,类是创建对象的模板,对象则是类的实例。类定义了对象的属性和方法,而对

gitblog_00215的博客 1107

终极OCaml实战指南:从新手到专家的完整学习路径

想要掌握**函数式编程**的核心精髓吗?《Real World OCaml》第二版为你提供了最实用的**OCaml学习方案**。这本书不仅是理论知识的堆砌,更是通过真实案例带你深入理解如何在实际项目中应用OCaml的强大功能。 ## 为什么选择这本OCaml权威教程? [![OCaml书籍封面展示](https://raw.gitcode.com/gh_mirrors/book9/book/r

gitblog_00811的博客 431

`dkim/rwo-lwt` 开源项目教程

dkim/rwo-lwt 开源项目教程 本教程旨在帮助您快速了解并上手dkim/rwo-lwt这一开源项目,它提供了Real World OCaml(RWO)一书中Async代码示例的Lwt版本翻译。Lwt是OCaml中一个流行的并发库,而这个项目则对于想要从Async转向Lwt或反之亦然的开发者极其有用。以下是核心内容模块: 1. 项目目录结构及介绍 项目根目录包含以下主要部分: src: 存...

gitblog_00539的博客 432

RealWorldOCaml 示例项目教程

RealWorldOCaml 示例项目教程 项目介绍 RealWorldOCaml 是一个专注于教授 OCaml 编程语言的开源项目。该项目通过一系列精心设计的示例和教程,帮助开发者理解和掌握 OCaml 的核心概念和高级特性。这些示例涵盖了从基础语法到复杂数据结构和算法的各个方面,非常适合初学者和有经验的开发者。 项目快速启动 环境准备 在开始之前,请确保您的系统上已经安装了 OCaml 编译器...

gitblog_00663的博客 595

Jane Street OCaml Workshop完全指南:从零基础到游戏开发的终极OCaml学习路径

Jane Street OCaml Workshop是一个专为OCaml初学者设计的开源项目,包含丰富的练习和项目,帮助你从零基础逐步掌握OCaml编程,并最终实现游戏开发。本指南将带你探索这个全面的学习资源,开启你的OCaml编程之旅。 ## 🚀 为什么选择Jane Street OCaml Workshop? Jane Street作为OCaml领域的领军企业,其开源的Workshop项

gitblog_00170的博客 1009
上一篇: clang: error: linker command failed with exit code 1
下一篇: 语丝
daxia665544
博客等级 码龄12年 2粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值