蓝天白云是什么生肖| 什么的鼻子| 羊得布病什么症状| 胃疼吃什么食物对胃好| 省内流量是什么意思| 切除阑尾对身体有什么影响| 洗礼是什么意思| 皮肤软组织感染用什么消炎药| 碎片化是什么意思| 50pcs是什么意思| 肾炎是什么原因引起的| 产后能吃什么水果| 皮肤糖化是什么意思| 李白有什么诗| 加应子是什么水果| 脑堵塞有什么症状| 吃什么能升血压| 7月8号是什么日子| 甲功三项能查出什么病| 母亲节送给妈妈什么礼物| 毛孔大什么原因形成的| 秘鲁说什么语言| 豆汁是什么味道| 肌酐500多属于什么期怎么治疗| 嘴苦是什么原因| 痞满是什么意思| 媳妇是什么意思| 气不够用是什么原因| 河南人喜欢吃什么| 漂发是什么意思| 佛跳墙是什么意思| 1月25日什么星座| 羡煞旁人是什么意思| 翡翠跟玉有什么区别| 芹菜炒什么| 得瑟是什么意思| 碟鱼是什么鱼| 三十六计最后一计是什么| 板命什么意思| 一年四季穿棉衣是什么生肖| 大便带血是什么原因男| 脑白质脱髓鞘改变是什么意思| 沸去掉三点水念什么| 曹操属什么| 二级建造师什么时候出成绩| 正常人尿液是什么颜色| 云代表什么动物| 色盲色弱是什么意思| 犹太人为什么那么聪明| 双性人什么意思| 谷丙转氨酶偏高是什么原因| 大理寺卿是什么职位| 郫县豆瓣酱能做什么菜| 十一月四日是什么星座| ir是什么意思| 鼠的本命佛是什么佛| 为什么会厌学| 多囊卵巢综合症吃什么食物好| 掉头发吃什么药最有效| 艾滋病是什么病毒| 鹅和什么一起炖最好吃| 水牛吃什么| 3月16号是什么星座的| 子宫肌瘤是什么原因造成的| 什么是制动| 小暑吃什么| 心塞是什么意思| 行政助理是干什么的| 什么的琥珀| 瘦脸针的危害有什么副作用| 什么情况下会宫外孕| 什么情况下会感染hpv病毒| 感冒为什么会打喷嚏| 小肚子大是什么原因| 宝齐莱手表什么档次| 正月初七什么星座| 桃子和什么不能一起吃| 伽马射线是什么| 车顶放饮料什么意思| 备孕男性检查什么项目| 鸭肉和什么一起炖好吃| 孔雀开屏是什么行为| 七月种什么菜| 老年人睡眠多是什么原因| 退着走路有什么好处| 没有什么过不去| wa是什么意思| 九月29号是什么星座| 晚上老咳嗽是什么原因| 副团级是什么军衔| 烟火是什么意思| 晚上睡觉放屁多是什么原因| 为什么家里蟑螂特别多| 世界上最毒的蛇是什么蛇| 僵尸为什么怕糯米| 这是什么鱼| 灯火葳蕤是什么意思| 寿眉茶属于什么茶| 孕妇吃海参对胎儿有什么好处| 三月初什么星座| 儿童吃什么钙片补钙效果好| 怀孕前三个月需要注意什么| 上海属于什么方向| 扭曲是什么意思| 梦见蛇挡路是什么意思| 晚上睡觉小腿抽筋是什么原因| npn是什么意思| bdp是什么意思| 姜维属什么生肖| 女人什么时候最想要| 娇妻是什么意思| 整天放屁是什么原因| 张良属什么生肖| 肩袖损伤吃什么药| 什么月披星| 聊胜于无的聊是什么意思| 隅读什么| 三个水读什么| 单飞是什么意思| 薄幸是什么意思| 13什么意思| 左手发麻什么原因| 肾盂肾炎吃什么药好| mon什么意思| 吃马齿菜有什么好处| 团五行属什么| 率真是什么意思| 女人梦见掉牙齿是什么征兆| 满月打什么疫苗| 千里马比喻什么样的人| 头发掉什么原因| 渴望是什么意思| 羊字五行属什么| 睾丸是什么形状的| dn是什么意思| 忌廉是什么东西| 玉米和什么不能一起吃| 挑染什么颜色好看| 头晕是什么原因引起| 妇检tct是什么检查| 肝囊肿有什么症状表现| 先明后不争什么意思| 流年是什么| 金不换是什么菜| 下巴长闭口是什么原因| 扁桃体2度是什么意思| 吃头孢不能吃什么水果| 尿胆原normal是什么意思| 硝酸酯类药物有什么药| 动脉ct主要检查什么| 伤元气是什么意思| 妙不可言是什么意思| arb是什么意思| 碧色是什么颜色| 什么影院| 外科看什么病| 拉屎特别臭是什么原因| 小孩满月送什么礼物好| 姥姥的妈妈叫什么| 彼岸花代表什么星座| hpv52高危阳性是什么意思| 甲子日是什么意思| 月经为什么会提前| 917是什么意思| 皮肤过敏用什么药| 久卧伤气是什么意思| 疑神疑鬼是什么意思| 孩子发烧呕吐是什么原因| 打胶原蛋白针有什么副作用吗| 莞尔是什么意思| 重磅是什么意思| 什么叫混合斑块| 测血型挂什么科| 月经一直不干净是什么原因引起的| 我用什么才能留住你| er什么意思| 吃什么可以提高新陈代谢| 紫色属于五行属什么| 咖喱是什么| 什么补肾效果最好| 辣椒含有什么维生素| 舌系带短挂什么科| 枧水是什么| dew是什么意思| 10月5日什么星座| 涉黑是什么意思| 没睡好头疼是什么原因| 马甲是什么| 吃猪肺有什么好处和坏处| 生肖龙和什么生肖相冲| 绝经有什么症状| 静对什么| 如鱼得水是什么意思| 加味逍遥丸和逍遥丸有什么区别| 肝右叶钙化灶什么意思| 后背刺痛什么原因引起的| 鼻息肉是什么症状| 什么是肠梗阻| 低落是什么意思| 山楂可以和什么一起泡水喝| 生理盐水敷脸有什么作用| 总是拉肚子是什么原因| 四方草地是什么生肖| 尿路感染是什么引起的| 王一博是什么星座| 女娲和伏羲是什么关系| 中元节又叫什么节| 胃溃疡十二指肠溃疡吃什么药| Mo什么元素| 石光荣是什么军衔| 腊月是什么月| 甲状腺毒症是什么意思| 彩虹代表什么| 盐和醋泡脚有什么好处| 男人吃逍遥丸治什么病| 落花流水什么意思| 手上长疣是什么原因造成的| 什么因果才会有双胞胎| 打嗝是什么意思| e3是什么意思| 看见黄鼠狼有什么预兆| 压车是什么意思| 木樨是什么意思| 糖尿病人早餐吃什么| 乐不思蜀什么意思| 吃什么水果容易减肥| 茯苓长什么样子图片| 半月板后角变性什么意思| 心力衰竭是什么症状| 孕吐吃什么可以缓解| 心衰吃什么药效果最好| 亚硝酸钠是什么东西| 睡觉睁眼睛是什么原因| 胃寒吃什么可以暖胃| 泌尿系统感染什么症状| 男人吃荔枝有什么好处| 妈妈的哥哥的老婆叫什么| 脑袋痛什么原因| 三价铁离子什么颜色| 割包皮挂什么科室| 尿黄吃什么药| 浑身发热是什么原因| 胃肠镜能检查出什么病| 腹部左侧是什么器官| 35岁属什么生肖| 吃海带有什么好处和坏处| 梦见自己吃肉是什么预兆| 脑梗长期吃什么药好| 茄子能治什么病| 茜读什么| 耳朵上有痣代表什么| 刷存在感是什么意思| 秦始皇的真名叫什么| smart是什么| 睡眠不好用什么药调理| 鲁迅原名什么| 总恶心是什么病的前兆| hc2是什么检查| 吃什么补气补血最见效| 春砂仁与什么煲汤最佳| 什么是口爆| 大黄是什么药材| 梨和什么一起榨汁好喝| 阴道什么形状| 百度

成都五行属什么

Prerequisites

百度 科学技术部对外保留国家外国专家局牌子。

It is possible to give names and default values to function parameters. This is broadly known as labels. In this tutorial, we learn how to use labels.

Throughout this tutorial, the code is written in UTop. In this document parameters that are not labelled are called positional parameters.

Passing Labelled Arguments

The function Option.value from the standard library has a parameter labelled default.

# Option.value;;
- : 'a option -> default:'a -> 'a = <fun>

Labelled arguments are passed using a tilde ~ and can be placed at any position and in any order.

# Option.value (Some 10) ~default:42;;
- : int = 10

# Option.value ~default:42 (Some 10);;
- : int = 10

# Option.value ~default:42 None;;
- : int = 42

Note: Passing labelled arguments through the pipe operator (|>) throws a syntax error:

# ~default:42 |> Option.value None;;
Error: Syntax error

Labelling Parameters

Here is how to name parameters in a function definition:

# let rec range ~first:lo ~last:hi =
    if lo > hi then []
    else lo :: range ~first:(lo + 1) ~last:hi;;
val range : first:int -> last:int -> int list = <fun>

The parameters of range are named

  • lo and hi inside the function's body, as usual
  • first and last when calling the function; these are the labels.

Here is how range is used:

# range ~first:1 ~last:10;;
- : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]

# range ~last:10 ~first:1;;
- : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]

It is possible to use a shorter syntax when using the same name as label and parameter.

# let rec range ~first ~last =
    if first > last then []
    else first :: range ~first:(first + 1) ~last;;
val range : first:int -> last:int -> int list = <fun>

At parameter definition ~first is the same as ~first:first. Passing argument ~last is the same as ~last:last.

Passing Optional Arguments

Optional arguments can be omitted. When passed, a tilde ~ or a question mark ? must be used. They can be placed at any position and in any order.

# let sum ?(init=0) u = List.fold_left ( + ) init u;;
val sum : ?init:int -> int list -> int = <fun>

# sum [0; 1; 2; 3; 4; 5];;
- : int = 15

# sum [0; 1; 2; 3; 4; 5] ~init:100;;
- : int = 115

It is also possible to pass optional arguments as values of type option. This is done using a question mark when passing the argument.

# sum [0; 1; 2; 3; 4; 5] ?init:(Some 100);;
- : int = 115

# sum [0; 1; 2; 3; 4; 5] ?init:None;;
- : int = 15

Defining Optional Parameters With Default Values

In the previous section, we've defined a function with an optional parameter without explaining how it works. Let's look at a different variant of this function:

# let sum ?init:(x=0) u = List.fold_left ( + ) x u;;
val sum : ?init:int -> int list -> int = <fun>

It behaves the same, but in this case, ?init:(x = 0) means that ~init is an optional parameter that defaults to 0. Inside the function, the parameter is named x.

The definition in the previous section used the shortcut that makes ?(init = 0) the same as ?init:(init = 0).

Defining Optional Parameters Without Default Values

An optional parameter can be declared without specifying a default value.

# let sub ?(pos=0) ?len:len_opt s =
    let default = String.length s - pos in
    let length = Option.value ~default len_opt in
    String.sub s pos length;;
val sub : ?pos:int -> ?len:int -> string -> string = <fun>

Here, we're defining a variant of the function String.sub from the standard library.

  • s is the string from which we are extracting a substring.
  • pos is the substring's starting position. It defaults to 0.
  • len is the substring's length. If missing, it defaults to String.length s - pos.

When an optional parameter isn't given a default value, its type inside the function is made an option. Here, len appears as ?len:int in the function signature. However, inside the body of the function, len_opt is an int option.

This enables the following usages:

# sub ~len:5 ~pos:2 "immutability";;
- : string = "mutab"

# sub "immutability" ~pos:7 ;;
- : string = "ility"

# sub ~len:2 "immutability";;
- : string = "im"

# sub "immutability";;
- : string = "immutability"

It is possible to use the same name for the len parameter and label name.

# let sub ?(pos=0) ?len s =
    let default = String.length s - pos in
    let length = Option.value ~default len in
    String.sub s pos length;;
val sub : ?pos:int -> ?len:int -> string -> string = <fun>

Optional Arguments and Partial Application

Let's compare two possible variants of the String.concat function from the standard library which has type string -> string list -> string.

In the first version, the optional separator is the last declared parameter.

# let concat_warn ss ?(sep="") = String.concat sep ss;;
Line 1, characters 15-18:
  Warning 16 [unerasable-optional-argument]:
  this optional argument cannot be erased.
val concat_warn : string list -> ?sep:string -> string = <fun>

# concat_warn ~sep:"--" ["foo"; "bar"; "baz"];;
- : string = "foo--bar--baz"

# concat_warn ~sep:"";;
- : string list -> string

# concat_warn ["foo"; "bar"; "baz"];;
- : ?sep:string -> string = <fun>

In the second version, the optional separator is the first declared parameter.

# let concat ?(sep="") ss = String.concat sep ss;;
val concat : ?sep:string -> string list -> string = <fun>

# concat ["foo"; "bar"; "baz"] ~sep:"--";;
- : string = "foo--bar--baz"

# concat ~sep:"--";;
- : string list -> string = <fun>
t
# concat ["foo"; "bar"; "baz"];;
- : string = "foobarbaz"

The only difference between the two versions is the order in which the parameters are declared. Both functions behave the same, except when only applied to the argument ["foo"; "bar"; "baz"]. In that case:

  • concat returns "foobarbaz". The default value "" of ~sep is passed.
  • concat_warn returns a partially applied function of type ?sep:string -> string. The default value is not passed.

Most often, concat is needed. Therefore a function's last declared parameter shouldn't be optional. The warning suggests turning concat_warn into concat. Disregarding it exposes a function with an optional parameter that must be provided, which is contradictory.

Note: Optional parameters make it difficult for the compiler to know if a function is partially applied or not. This is why at least one positional parameter is required after the optional ones. If present at application, it means the function is fully applied, if missing, it means the function is partially applied.

Passing Labelled Arguments Using the Pipe Operator

Declaring a function's unlabelled argument as the first one simplifies reading the function's type and does not prevent passing this argument using the pipe operator.

Let's modify the range function previously defined by adding an additional parameter step.

# let rec range step ~first ~last = if first > last then [] else first :: range step ~first:(first + step) ~last;;
val range : int -> first:int -> last:int -> int list = <fun>

# 3 |> range ~last:10 ~first:1;;
- : int list = [1; 4; 7; 10]

Function with Only Optional Arguments

When all parameters of a function need to be optional, a dummy, positional and occurring last parameter must be added. The unit () value comes in handy for this. This is what is done here.

# let hello ?(who="world") () = "hello, " ^ who;;
val hello : ?who:string -> string = <fun>

# hello;;
- : ?who:string -> unit -> string = <fun>

# hello ();;
- : string = "hello, world"

# hello ~who:"sabine";;
- : unit -> string = <fun>

# hello ~who:"sabine" ();;
- : string = "hello, sabine"

# hello () ?who:None;;
- : string = "hello, world"

# hello ?who:(Some "christine") ();;
- : string = "hello, christine"

Without the unit parameter, the optional argument cannot be erased warning would be emitted.

Forwarding an Optional Argument

Passing an optional argument with a question mark sign ? allows forwarding it without unwrapping. These examples reuse the sub function defined in the Optional Arguments Without Default Values section.

# let take ?len s = sub ?len s;;
val take : ?len:int -> string -> string = <fun>

# take "immutability" ~len:2;;
- : string = "im"

# let rtake ?off s = sub ?pos:off s;;
val rtake : ?off:int -> string -> string = <fun>

# rtake "immutability" ~off:7;;
- : string = "ility"

In the definitions of take and rtake, the function sub is called with optional arguments passed with question marks.

In take the optional argument has the same name as in sub; writing ?len is sufficient to forward without unwrapping.

Conclusion

Functions can have named or optional parameters. Refer to the reference manual for more examples and details on labels.

Help Improve Our Documentation

All OCaml docs are open source. See something that's wrong or unclear? Submit a pull request.

韧带损伤挂什么科 孕妇什么时候做nt progress什么意思 蔓越莓是什么水果 打车费计入什么科目
红虫是什么的幼虫 乐极生悲是什么意思 搓是什么意思 一什么面包 副乳是什么意思
晟读什么 舌头发涩是什么原因造成的 奔跑吧 什么时候开播 d2聚体高是什么意思 最近老做噩梦是什么原因
肾结石吃什么食物好 运动不出汗是什么原因 头晕出冷汗是什么原因 射的快吃什么药 血色素是什么
胆固醇高不能吃什么hcv7jop5ns3r.cn 三焦湿热吃什么中成药hcv9jop3ns0r.cn 淋巴细胞百分比低是什么意思hcv7jop7ns4r.cn 钙片吃多了有什么副作用hcv9jop7ns2r.cn diy什么意思dayuxmw.com
早泄有什么办法hcv8jop4ns0r.cn 藏红花有什么作用和功效hcv8jop5ns5r.cn 纵隔占位是什么意思hcv8jop6ns1r.cn 经期上火了吃什么降火shenchushe.com 男人手大代表什么hcv9jop3ns4r.cn
酸奶能做什么美食hcv8jop1ns6r.cn 胎盘低是什么原因造成的hcv9jop6ns7r.cn 0l是什么意思hcv8jop9ns0r.cn 蚊子怕什么颜色hcv9jop0ns3r.cn 芝柏手表什么档次hcv9jop0ns8r.cn
午时右眼跳是什么预兆hcv7jop9ns0r.cn 单核细胞偏高是什么意思hcv9jop7ns1r.cn 女人盗汗吃什么好得快onlinewuye.com 风花雪月什么意思hcv8jop6ns1r.cn 山梨酸是什么xianpinbao.com
百度