我正在尝试编写一个函数,其中有一个辅助函数,'a - >布尔。我的问题是,当我尝试使用此辅助函数的返回值时,我得到错误(因为它是一个函数?)
类型
'a -> bool' does not support the 'equality' constraint
,
有没有办法让它预先评估函数,以便我可以使用返回值?
示例:
let primelist n =
let rec prim n = function
| [] -> true
| x::xs when n % x = 0 -> false
| x::xs -> prim n xs
let rec help n list = function
| n when (prim n = true) -> List.rev (n::list)
| n -> List.rev list
| i when (prim i = true) -> help n (i::list) (i+1)
| i -> help n list (i+1)
help [2] n
答案 0 :(得分:2)
这样的事情可以解决问题:
let primelist n =
let rec prim n = function
| [] -> true
| x::xs when n % x = 0 -> false
| x::xs -> prim n xs
let rec help n list arg =
let prime n = prim n list
match arg with
| n when (prime n = true) -> List.rev (n::list)
| n -> List.rev list
| i when (prime i = true) -> help n (i::list) (i+1)
| i -> help n list (i+1)
help [2] n
这里我使用currying来创建一个新函数。