Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct description in functional_programming.zh.md #1610

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM ocaml/opam:debian-10-ocaml-4.10
RUN git -C /home/opam/opam-repository pull origin master && git -C /home/opam/opam-repository checkout 1003e5965fbc38d98b88570ac280b519fdae302f && opam update -u -y
RUN git -C /home/opam/opam-repository pull origin master && git -C /home/opam/opam-repository checkout 3606210eb2472a23e8ef7960ce143026e43e507d && opam update -u -y
WORKDIR /home/opam/src
RUN sudo chown opam /home/opam/src
COPY --chown=opam *.opam /home/opam/src
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.deploy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM ocaml/opam:debian-10-ocaml-4.10 as build
RUN git -C /home/opam/opam-repository pull origin master && git -C /home/opam/opam-repository checkout 1003e5965fbc38d98b88570ac280b519fdae302f && opam update -u -y
RUN git -C /home/opam/opam-repository pull origin master && git -C /home/opam/opam-repository checkout 3606210eb2472a23e8ef7960ce143026e43e507d && opam update -u -y
WORKDIR /home/opam/src
RUN sudo chown opam /home/opam/src
COPY --chown=opam *.opam /home/opam/src
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.staging
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM ocaml/opam:debian-10-ocaml-4.10 as build
RUN git -C /home/opam/opam-repository pull origin master && git -C /home/opam/opam-repository checkout 1003e5965fbc38d98b88570ac280b519fdae302f && opam update -u -y
RUN git -C /home/opam/opam-repository pull origin master && git -C /home/opam/opam-repository checkout 3606210eb2472a23e8ef7960ce143026e43e507d && opam update -u -y
WORKDIR /home/opam/src
RUN sudo chown opam /home/opam/src
COPY --chown=opam *.opam /home/opam/src
Expand Down
2 changes: 1 addition & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ template/%_code_snippet.html:template/%_code_snippet.md $(OMD_PP)
cat "$<" | $(OMD_PP) | ${OMD} -o $@

opam_update_list: script/generate_opam_update_list
curl -O http://opam.ocaml.org/json/last10_updates.json
curl -O https://opam.ocaml.org/json/last10_updates.json
script/generate_opam_update_list

script/generate_opam_update_list: script/generate_opam_update_list.ml
Expand Down
8 changes: 4 additions & 4 deletions site/docs/cheat_sheets.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
[OCamlPro](http://www.ocamlpro.com/) has published cheat sheets (one or
two-page summaries) on OCaml:

* [The OCaml Language](http://www.ocamlpro.com/wp-content/uploads/2019/09/ocaml-lang.pdf) (PDF, September 2019)
* [The OCaml Language](https://ocamlpro.github.io/ocaml-cheat-sheets/ocaml-lang.pdf) (PDF, September 2019)
General overview of the OCaml language: basic data types, basic
concepts, functions, modules, etc.

* [OCaml Standard Tools](http://www.ocamlpro.com/files/ocaml-tools.pdf) (PDF, June 2011)
* [OCaml Standard Tools](https://ocamlpro.github.io/ocaml-cheat-sheets/ocaml-tools.pdf) (PDF, June 2011)
Overview of OCaml compilers and their options, tools for lexing and
parsing, Makefile rules, etc.

* [OCaml Standard Library](http://www.ocamlpro.com/wp-content/uploads/2019/09/ocaml-stdlib.pdf) (PDF, September 2019)
* [OCaml Standard Library](https://ocamlpro.github.io/ocaml-cheat-sheets/ocaml-stdlib.pdf) (PDF, September 2019)
Overview of the standard library's most common modules.

* [OCaml Emacs Mode (Tuareg)](http://www.ocamlpro.com/files/tuareg-mode.pdf) (PDF, June 2011)
* [OCaml Emacs Mode (Tuareg)](https://ocamlpro.github.io/ocaml-cheat-sheets/tuareg-mode.pdf) (PDF, June 2011)
Overview of the Emacs Tuareg mode keyboard shortcuts.
7 changes: 4 additions & 3 deletions site/index.fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
</a>
<h1><a href="https://opam.ocaml.org">Contributions</a></h1>
<p>Le gestionnaire de paquets <a href="https://opam.ocaml.org">OPAM</a> vous donne accès aux multiples versions de
<a href="https://opam.ocaml.org/packages/">centaines de paquets</a>.</p>
<a href="https://opam.ocaml.org/packages/">milliers de paquets</a>.</p>
</section>
<section class="span4 home-feature">
<a href="/community/index.fr.html">
Expand Down Expand Up @@ -102,6 +102,7 @@
/><img class="png" src="/img/rss.png" alt="RSS" /></a>
</h1>
<ul class="news-feed" style="margin-bottom: 0px">
<!-- Commented between workshop
<li class="announcement"><article>
<h1><a title="OCaml Users and Developers Workshop"
href="/meetings/ocaml/2020/">OCaml 2020</a></h1>
Expand All @@ -111,12 +112,12 @@
<img alt="" src="/img/announcement.svg" class="svg" />
<img alt="" src="/img/announcement.png" class="png" />
</a>
</article></li>
</article></li> -->
<li class="announcement"><article>
<h1><a title="Release of OCaml {{! get LATEST_OCAML_VERSION !}}"
href="/releases/{{! get LATEST_OCAML_VERSION !}}.html"
>Parution d'OCaml {{! get LATEST_OCAML_VERSION !}}</a></h1>
<p>24 février 2021</p>
<p>1er octobre 2021</p>
<a title="Release of OCaml {{! get LATEST_OCAML_VERSION !}}"
href="/releases/{{! get LATEST_OCAML_VERSION !}}.html">
<img alt="" src="/img/announcement.svg" class="svg" />
Expand Down
7 changes: 4 additions & 3 deletions site/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<h1><a href="https://opam.ocaml.org">Packages</a></h1>
<p>The <a href="https://opam.ocaml.org">OCaml Package
Manager</a>, gives you access to multiple versions of
<a href="https://opam.ocaml.org/packages/">hundreds of
<a href="https://opam.ocaml.org/packages/">thousands of
packages</a>.</p>
</section>
<section class="span4 home-feature">
Expand Down Expand Up @@ -103,7 +103,7 @@
</h1>
<ul class="news-feed" style="margin-bottom: 0px">

<!-- Commented out between workshops -->
<!-- Commented out between workshops
<li class="announcement"><article>
<h1><a title="OCaml Users and Developers Workshop"
href="/meetings/ocaml/2020/">OCaml 2020</a></h1>
Expand All @@ -114,11 +114,12 @@
<img alt="" src="/img/announcement.png" class="png" />
</a>
</article></li>
-->
<li class="announcement"><article>
<h1><a title="Release of OCaml {{! get LATEST_OCAML_VERSION !}}"
href="/releases/{{! get LATEST_OCAML_VERSION !}}.html"
>Release of OCaml {{! get LATEST_OCAML_VERSION !}}</a></h1>
<p>February 24, 2021</p>
<p>October 1, 2021</p>
<a title="Release of OCaml {{! get LATEST_OCAML_VERSION !}}"
href="/releases/{{! get LATEST_OCAML_VERSION !}}.html">
<img alt="" src="/img/announcement.svg" class="svg" />
Expand Down
61 changes: 27 additions & 34 deletions site/learn/tutorials/functional_programming.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,31 @@

到现在我们已经讲了很多了,但还没有真正涉及到**函数式编程**。
目前所讲的所有特性 - 丰富的数据类型(rich data types),
模式匹配(pattern matching), 类型推导(type inference),
嵌套函数(nested functions) -
可以想象它们都可以在一种”超级C“语言中存在。这些特性当然很酷,它们使得代码简洁易读,减少bug,但是它们实际和函数式编程没什么关系。实际上我的
观点是函数式编程语言的妙处*不是*在于函数式编程,而是因为在我们长年习惯于类C语言编程的时候,编程技术已经提高很多了。因此当我们一次又一次地写`struct { int type; union { ... } }`的时候,ML和Haskell程序员却有着很多安全的变量和数据类型的模式匹配。当我们小心翼翼地
`free`所有的`malloc`时候,很多语言在上世纪八十年代就有了超越手工管理的内存垃圾收集器。
模式匹配(pattern matching), 类型推导(type inference),嵌套函数(nested functions) -
可以想象它们都可以在一种“超级C”语言中存在。这些特性当然很酷,可以让代码跟家简洁、易读、并具有更少的bug,
但是实际上这与函数式编程的关系不大。实际上函数式编程语言的妙处*不是*在于函数式编程,而是因为在我们多年来使用类C语言编程的时间里,
编程的前沿技术已经得到了极大地发展。因此当我们一次又一次地写`struct { int type; union { ... } }`的时候,
ML和Haskell程序员却有着很多安全的可变类型(Safe Variants)和数据类型的模式匹配(Pattern Matching)。当我们小心翼翼地
`free`所有的`malloc`时候,很多语言在上世纪八十年代就有了超越手工管理内存的垃圾收集器。

好了,现在是时候告诉你们什么是函数式编程了。

基本的但不是很能说明问题的定义是在**函数式语言中**,
**函数(functions)**是一等公民。
最基本的定义是:**函数(functions)**在**函数式语言中**是一等公民。

听上去不是很有用,让我们来看个例子
听上去比较抽象,让我们来看一个实际的例子

```ocamltop
let double x = x * 2 in
List.map double [ 1; 2; 3 ]
```
在这个例子中,我首先定义了一个嵌套函数`double`,它读入一个参数`x`后返回`x * 2`。然后`map`在给定的列表(`[1; 2; 3]`)的每个元素上调用`double`来生成结果:一个每个数都扩大一倍的新的列表
在这个例子中,我首先定义了一个嵌套函数`double`,它读入一个参数`x`后返回`x * 2`。然后`map`会对给定的列表(`[1; 2; 3]`)中的每个元素都调用`double`来生成结果:一个每个元素的值都翻倍的新的列表

`map`被称为**高阶函数(higher-order function)**
(HOF)。高阶函数是指一个把其他函数作为参数之一的函数
(HOF)。高阶函数是指接受函数作为参数的函数

到现在为止还算简单。如果你对C/C++熟悉的,这就象传递一个函数指针作为参数。Java中有匿名类(anonymous
class)就像一个低速的闭包(closure)。如果你知道Perl那么你可能已经知道和使用了Perl中的闭包和Perl的`map`函数,这和我们现在所说的完全相同。事实上Perl很大程度上也是一个函数式语言。
如果你对C/C++熟悉的话,可以把高阶函数想象为一个接受函数指针作为参数的函数。

**闭包**是那些带着它们被定义时的环境的函数。特别的,一个闭包可以引用它定义时存在的变量。让我们把上面那个函数变得更通用一些,以便我们可以对任何整数列表乘以一个任意值`n`:
**闭包(Closure)** 是指那些带有它们被定义时的“环境”的函数。特别的,一个闭包可以引用它定义时存在的变量。让我们把上面那个函数变得更通用一些,以便我们可以对任何整数列表乘以一个任意值`n`:

```ocamltop
let multiply n list =
Expand All @@ -52,7 +51,7 @@ multiply 5 [1; 2; 3];;
```

关于`multiply`函数有一点值得注意的是嵌套函数`f`.
这是一个闭包。我们注意一下`f`怎样使用变量`n`的值,我们并没有把`n`作为显式的参数传递给它。`f`是从它的环境中找到它的。`n`是传递给函数`multiply`的参数,所以在这个函数中都是有效的。
这是一个闭包。我们注意一下`f`怎样使用变量`n`的值,我们并没有把`n`作为显式的参数传递给它。`f`是从它的“环境”中找到它的。`n`是传递给函数`multiply`的参数,所以在这个函数中都是有效的。

这可能听上去很简单,但让我们更进一步的仔细观察下那个对map的调用`List.map f list`.

Expand Down Expand Up @@ -82,7 +81,7 @@ end
现在来看`receiver_fn`的定义。这个函数是一个闭包,因为它含有一个引用,
这个引用指向它的环境中的`chan`。

## 偏函数应用(Partial function applications)和 currying(科里化)
## 偏函数应用(Partial function applications)

让我们定义一个加法函数用来相加两个整数。

Expand Down Expand Up @@ -145,11 +144,7 @@ let plus 2 b = (* 这不是真正的OCaml代码! *)
plus 2 3 : int
```

这个过程叫做**currying(科里化)** (或者应该叫 **uncurrying(去科里化)**,
我一直搞不清这两个定义).这个名字来源与Haskell Curry的与lambda
calculus有关的重要发现。为了避免进入OCaml背后的数学世界而使这个教程变得过于繁琐,我将不会再进一步地说明这个主题。如果感兴趣,你可以从[用
Google](http://www.google.com/search?q=currying "http://www.google.com/search?q=currying")
(译注:国内的小伙伴们可能要自行百度了)来获得更多关于currying的信息。
这个过程叫做**偏函数的应用(Partial Application)**

还记得开始时候我们的`double`和`multiply`函数吗? `multiply`是这样定义的:

Expand Down Expand Up @@ -195,7 +190,7 @@ let plus = ( + );;
plus 2 3;;
```

这里是更多的一些有趣的curring
更多关于偏函数应用的例子

```ocamltop
List.map (plus 2) [1; 2; 3];;
Expand Down Expand Up @@ -242,43 +237,41 @@ for (i = 0; i < strlen (s); ++i)
现在流行的做法是由顶至下地计划程序,但是作者的意见是这往往会让整个项目崩盘
(译注人话:计划赶不上变化,怎么做评估都没用)。

## 非懒惰和懒惰
## 严格求值(Strictness)和惰性求值(Laziness)

C类和ML类的语言都是非懒惰的(饥饿求值),而Haskell和Miranda都是懒惰的。OCaml是缺省非懒惰
但是在需要的时候支持懒惰的风格
类C和ML系的语言都是严格求值的,而Haskell和Miranda都是非严格求值的,或者说是惰性的。虽然OCaml默认是严格求值的
但需要时也可使用惰性求值风格进行编程

对于一个非懒惰的语言,参数和函数总是在使用前被求值,然后再传入到函数中。比如
对于一个严格求值的语言,参数和函数总是在使用前被求值,然后再传给函数。比如
下面的代码会引起除零错误:

```ocamltop
let give_me_a_three _ = 3;;
give_me_a_three (1/0);;
```

如果你用一些传统的语言编程,这就是这么回事,但是你可能会为事情还可以
以另一个方式发生而惊讶。
如果使用一些传统的语言编程,可能以上代码会如描述的那样产生错误。但如果使用惰性求值的方式运行这段代码,结果则可能会出人意表。

在懒惰语言中,一些奇怪的事情会发生。函数的参数只有在被使用的时候才会被求值。
`give_me_a_three`没有使用参数而直接返回3,因而在一个懒惰语言中,这个函数调用
不会失败,因为这个参数根本没有被求值!因此,也不会引起除零错误。
在惰性求值的语言中,因为函数的参数只有在被使用的时候才会被求值,所以会有一些不一样的事情发生。
比如,因为其第一个参数不会被求值,函数`give_me_a_three`会直接返回`3`。因此,除零错误根本不会发生。

懒惰语言允许你定义无限长的链表,只要你不会真的要遍历整个链表(比方说你只要前10个元素)。
惰性求值的语言允许你定义无限长的链表,只要你不会真的要遍历整个链表(比方说你只要前10个元素)。

OCaml是一个非懒惰的语言,但是`Lazy`模块允许你写懒惰的表达式,下面就是这样一个例子
虽然OCaml是一个严格求值的语言,但是`Lazy`模块允许你写惰性求值的表达式,下面是一个示例

```ocamltop
let lazy_expr = lazy (1/0)
```

注意到其类型是 `int lazy_t`。

因为`give_me_a_three`的输入是任何类型,所以我们也可以把这个懒惰表达式传入
因为`give_me_a_three`的输入可以是任何类型,所以我们也可以把惰性求值的表达式传进去

```ocamltop
give_me_a_three lazy_expr
```

如果要求值,我们要用`Lazy.force`函数:
如果要求值,我们需要使用`Lazy.force`函数:

```ocamltop
Lazy.force lazy_expr
Expand Down
2 changes: 1 addition & 1 deletion site/releases/4.12.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ opam switch create 4.12.1+flambda+nnpchecker --package=ocaml-variants.4.12.1+opt
or with opam 2.1:

```
opam switch create 4.12.1+flambda -ocaml-variants.4.12.1+options ocaml-option-flambda
opam switch create 4.12.1+flambda+nnpchecker ocaml-variants.4.12.1+options ocaml-option-flambda ocaml-option-nnpchecker
```


Expand Down
2 changes: 1 addition & 1 deletion site/releases/4.13.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ opam switch create 4.13.0+flambda+nnpchecker --package=ocaml-variants.4.13.0+opt
or with opam 2.1:

```
opam switch create 4.13.0+flambda -ocaml-variants.4.13.0+options ocaml-option-flambda
opam switch create 4.13.0+flambda+nnpchecker ocaml-variants.4.13.0+options ocaml-option-flambda ocaml-option-nnpchecker
```


Expand Down
60 changes: 60 additions & 0 deletions site/releases/4.13.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!-- ((! set title OCaml 4.13.1 !)) -->

# OCaml 4.13.1

This page describes OCaml **4.13.1**, released on Oct 1, 2021.
This is a bug-fix release of [OCaml 4.13.0](4.13.0.html).



Configuration options
---------------------

The configuration of the installed opam switch can be tuned with the
following options:

- ocaml-option-32bit: set OCaml to be compiled in 32-bit mode for 64-bit Linux and OS X hosts
- ocaml-option-afl: set OCaml to be compiled with afl-fuzz instrumentation
- ocaml-option-bytecode-only: compile OCaml without the native-code compiler
- ocaml-option-default-unsafe-string: set OCaml to be compiled without safe strings by default
- ocaml-option-flambda: set OCaml to be compiled with flambda activated
- ocaml-option-fp: set OCaml to be compiled with frame-pointers enabled
- ocaml-option-musl: set OCaml to be compiled with musl-gcc
- ocaml-option-nnp : set OCaml to be compiled with --disable-naked-pointers
- ocaml-option-nnpchecker: set OCaml to be compiled with --enable-naked-pointers-checker
- ocaml-option-no-flat-float-array: set OCaml to be compiled with --disable-flat-float-array
- ocaml-option-static :set OCaml to be compiled with musl-gcc -static

For instance, one can install a switch with both `flambda` and the naked-pointer checker enabled with

```
opam switch create 4.13.1+flambda+nnpchecker --package=ocaml-variants.4.13.1+options,ocaml-option-flambda,ocaml-option-nnpchecker
```

or with opam 2.1:

```
opam switch create 4.13.1+flambda+nnpchecker ocaml-variants.4.13.1+options ocaml-option-flambda ocaml-option-nnpchecker
```


![](../img/source.gif "") Source distribution
---------------------------------------------

- [Source
tarball](https://github.com/ocaml/ocaml/archive/4.13.1.tar.gz)
(.tar.gz) for compilation under Unix (including Linux and macOS)
and Microsoft Windows (including Cygwin).
- Also available in
[.zip](https://github.com/ocaml/ocaml/archive/4.13.1.zip)
format.
- The official development repo is hosted on
[GitHub](https://github.com/ocaml/ocaml).

Changes
-------

### Regression fix

- [#10661](https://github.com/ocaml/ocaml/issues/10661), [#10662](https://github.com/ocaml/ocaml/issues/10662): fix a bug with classes named "row"
(Gabriel Scherer, report by Nicolás Ojeda Bär)
9 changes: 9 additions & 0 deletions site/releases/4.13/notes/Changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
OCaml 4.13.1 (01 October 2021)
--------------------------------

### Bug fixes

- #10661, #10662: fix a bug with classes named "row"
(Gabriel Scherer, report by Nicolás Ojeda Bär)


OCaml 4.13.0 (24 September 2021)
--------------------------------

Expand Down
1 change: 1 addition & 0 deletions site/releases/index.fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ instructions pour installer OCaml par d'autres moyens que la
compilation des sources, comme par exemple le gestionnaire de paquets
OPAM et les gestionnaire de paquets spécifiques à une plateforme.

* OCaml [4.13.1](4.13.1.html), [téléchargé](https://github.com/ocaml/ocaml/archive/4.13.1.tar.gz), publiée le 1er octobre, 2021.
* OCaml [4.13.0](4.13.0.html), [téléchargé](https://github.com/ocaml/ocaml/archive/4.13.0.tar.gz), publiée le 24 septembre 2021.
* OCaml [4.12.1](4.12.1.html), [téléchargé](https://github.com/ocaml/ocaml/archive/4.12.1.tar.gz), publiée le 24 septembre 2021.
* OCaml [4.12.0](4.12.0.html), [téléchargé](https://github.com/ocaml/ocaml/archive/4.12.0.tar.gz), publiée le 24 février 2021.
Expand Down
1 change: 1 addition & 0 deletions site/releases/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ See also the [install](/docs/install.html) page for instructions on
installing OCaml by other means, such as the OPAM package manager and
platform specific package managers.

* OCaml [4.13.1](4.13.1.html), [Download](https://github.com/ocaml/ocaml/archive/4.13.1.tar.gz), released Oct 1, 2021.
* OCaml [4.13.0](4.13.0.html), [Download](https://github.com/ocaml/ocaml/archive/4.13.0.tar.gz), released Sep 24, 2021.
* OCaml [4.12.1](4.12.1.html), [Download](https://github.com/ocaml/ocaml/archive/4.12.1.tar.gz), released Sep 24, 2021.
* OCaml [4.12.0](4.12.0.html), [Download](https://github.com/ocaml/ocaml/archive/4.12.0.tar.gz), released Feb 24, 2021.
Expand Down