gpt4 book ai didi

compiler-errors - 编写一个函数 `smallest_absent t`,该函数返回 `l`中不存在的最小自然整数

转载 作者:行者123 更新时间:2023-12-02 10:49:12 25 4
gpt4 key购买 nike

编写签名函数smallest_absent : int_list -> int,例如调用smaller_absent l返回l中不存在的最小自然整数

let smallest_absent l =
match l with
|[] -> 0
|_ -> let m = ref (0,false) in
while !m.(1) = false do
if (mem l m.(0)) then !m.(1) := true ;
else incr(m.(0));
done;
!m.(0);;

错误:
> while !m.(1) = false do
this expression is of type int * bool, but is used with the type 'a vect>`

我想知道我的代码出了什么问题。并且如果它在概念上是正确的。谢谢。

最佳答案

类型错误说明了一切:您正在尝试在_.(1)int的元组上使用 vector 查找bool

您正在寻找的功能是 snd : 'a * 'b -> 'b

同样,您应该使用 m.(0) 而不是fst !m来编写fst : 'a * 'b -> 'a

关于compiler-errors - 编写一个函数 `smallest_absent t`,该函数返回 `l`中不存在的最小自然整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47967866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com