奥卡姆介绍
我现在正在尝试学习 ocaml,想从一个小程序开始,生成所有位组合:
["0","0","0"]
["0","0","1"]
["0","1","0"]
...等等
我的想法是以下代码:
let rec bitstr length list =
if length = 0 then
list
else begin
bitstr (length-1)("0"::list);
bitstr (length-1)("1"::list);
end;;
但我收到以下错误:
Warning S: this expression should have type unit.
val bitstr : int -> string list -> string list = <fun>
# bitstr 3 [];;
- : string list = ["1"; "1"; "1"]
我不明白要做什么改变,你能帮我吗?
此致 菲利普
i'm trying to learn ocaml right now and wanted to start with a little program, generating all bit-combinations:
["0","0","0"]
["0","0","1"]
["0","1","0"]
... and so on
My idea is the following code:
let rec bitstr length list =
if length = 0 then
list
else begin
bitstr (length-1)("0"::list);
bitstr (length-1)("1"::list);
end;;
But i get the following error:
Warning S: this expression should have type unit.
val bitstr : int -> string list -> string list = <fun>
# bitstr 3 [];;
- : string list = ["1"; "1"; "1"]
I did not understand what to change, can you help me?
Best regards
Philipp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
开始 foo; bar end
执行foo
并丢弃结果,然后执行 bar.因为这只有在foo
有副作用并且没有有意义的返回值时才有意义,如果 foo 有除 unit 之外的返回值,ocaml 会发出警告,因为其他一切都可能是程序员错误(即程序员实际上并不打算丢弃结果) - 就像这里的情况一样。在这种情况下,用“0”计算列表然后将其丢弃确实没有任何意义。大概您想连接两个列表。您可以使用
@
运算符来执行此操作:请注意,我还使
length = 0
情况返回[list]
而不仅仅是list
因此结果是列表的列表而不是平面列表。begin foo; bar end
executesfoo
and throws the result away, then it executes bar. Since this makes only sense iffoo
has side-effects and no meaningful return value ocaml emits a warning if foo has a return value other than unit, since everything else is likely to be a programmer error (i.e. the programmer does not actually intend for the result to be discarded) - as is the case here.In this case it really does make no sense to calculate the list with "0" and then throw it away. Presumably you want to concatenate the two lists instead. You can do this using the
@
operator:Note that I also made the
length = 0
case return[list]
instead of justlist
so the result is a list of lists instead of a flat list.虽然 sepp2k 的答案是正确的,但我想添加以下替代方案(它与您提出的签名不匹配,但实际上做了您想要的事情):
第一个区别是您不需要传递一个空列表来调用函数
bitsr 2
返回[["0"; “0”]; [“0”; “1”]; [“1”; “0”]; [“1”; “1”]]
。其次,它返回有序二进制值的列表。但更重要的是,在我看来,它更接近ocaml的精神。Although sepp2k's answer is spot on, I would like to add the following alternative (which doesn't match the signature you proposed, but actually does what you want) :
The first difference is that you do not need to pass an empty list to call the function
bitsr 2
returns[["0"; "0"]; ["0"; "1"]; ["1"; "0"]; ["1"; "1"]]
. Second, it returns a list of ordered binary values. But more importantly, in my opinion, it is closer to the spirit of ocaml.所以这就是......
结果:
So here it is...
Result: