【发布时间】:2013-12-26 11:45:02
【问题描述】:
我不理解以下 OCaml 代码,因为我不确定 extract_ident 函数在未明确指定时如何推断一元参数是 string_lexer 并且它是如何传递到 extract 中的。我知道你可以有一个没有 match 构造的函数,模式匹配中的类型是最后一个参数,但我不明白如何将 string_lexer 参数传递给提取隐式?
type string_lexer = {string:string; mutable current:int; size:int } ;;
let init_lex s = {string=s, current=0; size=String.length s} ;;
let forward cl = cl.current <- cl.current+1 ;;
let forward_n cl n = cl.current <- cl.current+n ;;
let extract pred cl =
let st = cl.string and pos = cl.current in
let rec ext n = if n<cl.size && (pred st.[n]) then ext (n+1) else n in
let res = ext pos in
cl.current <- res; String.sub cl.string pos (res-pos) ;;
let extract_int =
let is_int = function '0'..'9' -> true | _ -> false
in function cl -> int_of_string (extract is_int cl);;
let extract_ident =
let is_alpha_num = function
'a'..'z' | 'A'..'Z' | '0'..'9' | '_' -> true
| _ -> false
in extract is_alpha_num ;;
【问题讨论】:
标签: ocaml