Function 我能定义一个“a”吗;rec";功能在另一个;rec";Ocaml中的函数?
我必须创建一个函数Function 我能定义一个“a”吗;rec";功能在另一个;rec";Ocaml中的函数?,function,recursion,ocaml,Function,Recursion,Ocaml,我必须创建一个函数isFunction,该函数将一组对作为参数,如果没有重复,则返回true,如果有重复,则返回false 例如:isFunction[(1,3);(2,40);(3,40)]返回true但isFunction[(1,3);(2,40);(1,40)]返回false,因为1重复 现在,我的代码是: let rec exist e = function |[] -> false |(a,_)::l -> e=a || exist e l;; let re
isFunction
,该函数将一组对作为参数,如果没有重复,则返回true,如果有重复,则返回false
例如:isFunction[(1,3);(2,40);(3,40)]
返回true
但isFunction[(1,3);(2,40);(1,40)]
返回false
,因为1重复
现在,我的代码是:
let rec exist e = function
|[] -> false
|(a,_)::l -> e=a || exist e l;;
let rec isFunction = function
|[]->true
|(a,_)::l -> not(exist a l) && isFunction l;;
这很好用!但问题是:是否有其他方法可以定义
isFunction
,而不定义其他辅助函数?您可以将exist
声明为isFunction
的本地函数:
let rec isFunction l =
let rec exist e = function
|[] -> false
|(a,_)::l -> e=a || exist e l
in
match l with
|[]->true
|(a,_)::l -> not(exist a l) && isFunction l
您可以将
exist
声明为isFunction
的本地:
let rec isFunction l =
let rec exist e = function
|[] -> false
|(a,_)::l -> e=a || exist e l
in
match l with
|[]->true
|(a,_)::l -> not(exist a l) && isFunction l
您可以使用List.exists:
let isFunction = function
| [] -> true
| (a,_)::tl -> not (List.exists (fun (a',_) -> a'=a) tl) && isFunction l;;
您可以使用List.exists:
let isFunction = function
| [] -> true
| (a,_)::tl -> not (List.exists (fun (a',_) -> a'=a) tl) && isFunction l;;