码迷,mamicode.com
首页 > 其他好文 > 详细

Ocaml入门(3)

时间:2015-09-01 18:19:46      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:

如果在centos环境下配置好了opam,安装好了ocaml, core, utop,接下来就可以开始学习ocaml了。

跟随《Real world Ocaml》的脚步来学吧。网上有html版。

先概览一下。

普通的数字计算

ocaml很在乎类型,浮点数和整数类型运算符不能混用。

utop # 3 + 4;;
- : int = 7  

utop # 3.1 + 4.2;;
Error: This expression has type float
but an expression was expected of type int 

utop # 3.1 +. 4.2;;
- : float = 7.3  

整数数位如很多,为清晰起见,可以使用下划线分开:

utop # 100_000 / 1000;;
- : int = 100 

可以把值绑定绑定到符号。在最顶层绑定相当于其它语言的“全局变量”。

变量名可以含有引号(数学上的用法),可以用小写字母或下划线开始。

utop # let x = 5;;
val x : int = 5   
utop # let x = 6;;
val x : int = 6 
utop # let _x = x + x;;

注意,下划线开始的变量,utop不去打印它的值,但你直接访问该变量值是可以的。
虽然名字叫“变量”,其实和其它语言的变量不一样,ocaml中的变量是“不变”的,ocaml更鼓励重新绑定,而不是修改变量的值。

很多函数式语言都是这个观点。

函数

函数当然是函数式语言的核心。

函数在此处是“一等公民”,像其它语言中的int,double一样基本,有类型,可传递,可加工,可复合,可偏参等许多花样。

utop # let dd x = x * x;;
val dd : int -> int = <fun> 
utop # dd 5;;
- : int = 25 

当然,可以多个参数

utop # let myadd x y = x * 10 + y;;
val myadd : int -> int -> int = <fun> 
utop # myadd 5 7;;
- : int = 57 

TUPLES

很多高级语言的函数都可以传入多个参数,但最多只能返回一个值。如果需要返回多个值很麻烦。

ocaml提供tuple类型可以解决这个问题。

utop # let mydiv x y = x/y, x mod y;;
val mydiv : int -> int -> int * int = <fun>  
utop # mydiv 10 3;;
- : int * int = (3, 1)  

tuple 类型就是用逗号分开的值,最好外边加小括号。
注意系统返回的类型。

ocaml支持“解构赋值”,这对取得tuple中的数据来说很方便。

utop # let (a,b) = mydiv 10 3;;
val a : int = 3                                                      
val b : int = 1 
utop # let distance (x1,y1) (x2,y2) =
  sqrt((x1 -. x2) ** 2. +. (y1 -. y2) ** 2.);;
val distance : float * float -> float * float -> float = <fun>  
utop # distance (1.0, 1.0) (2.0, 2.0);;
- : float = 1.41421356237   

上边的代码说明,“解构”可以用在参数位置,这十分方便。

 Lists

utop # let an = ["cat";"bird";"elephant"];;
val an : string list = ["cat"; "bird"; "elephant"] 

注意,此处元素间的分割是分号,不是逗号。很无奈,逗号是用来分割tuple元素的。

utop # List.map String.length an;;
- : int list = [3; 4; 8] 

列表的写法是“语法糖”,实际上是逐渐构造的。

utop # 1::2::3::[];;
- : int list = [1; 2; 3]   

list可以运算:

utop # [1;2;3] @ [4;5];;
- : int list = [1; 2; 3; 4; 5]   

列表也可以进行“解构”绑定:

utop # let h::t = [1;2;3;4];;
Characters 4-8:                                                      
Warning 8: this pattern-matching is not exhaustive.   
 Here is an example of a value that is not matched:   []
val h : int = 1
val t : int list = [2; 3; 4]

列表的经典动作,取得“头元素”和剩余的部分

之所以有Warning,是因为从语法角度看,如果右边的列表是个空的,左边就无法解构了,会出异常。
可以用match语句来避免,相当于其它语言的分支。

utop # let f x = match x with
| [] -> 0
| (h::t) -> h;;
val f : int list -> int = <fun>  
utop # f [1;2;3;4];;
- : int = 1  
utop # f [];;
- : int = 0     

 

Ocaml入门(3)

标签:

原文地址:http://www.cnblogs.com/gyhang/p/4776415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!