【发布时间】:2020-06-26 16:45:24
【问题描述】:
我创建了一个函数p,它检查给定值的平方是否小于30。
然后在另一个函数(作为参数)中调用此函数以返回列表中的第一个值,其平方小于 30(如果 p 为真,基本上我必须检查函数 p 是 true 还是 @ 987654325@)。
这是代码:
let p numb =
let return = (numb * numb) < 30 in return
let find p listT =
let rec support p listT =
match listT with
| []-> raise (Failure "No element in list for p")
| hd :: tl -> if p hd then hd
else support p tl in
let ret = support (p listT) in ret
let () =
let a = [5;6;7] in
let b = find p a in print_int b
但它在最后一行说:
Error: This expression (p) has type int -> bool
but an expression was expected of type int -> 'a -> bool
Type bool is not compatible with type 'a -> bool
但是,我认为我没有以正确的方式使用高阶函数,我认为它应该更自动,或者不是?
【问题讨论】:
标签: functional-programming ocaml