小编Oxt*_*tis的帖子

OCaml 列表匹配模式

所以我正在编写一个简单的方法来总结列表中前 3 个或更少的整数,但我对匹配模式感到困惑。
我目前有

let sums l = match l with
    | [] -> 0
    | (h1::h2::h3::_) -> h1+h2+h3
    | [h1;h2;h3] -> h1+h2+h3
    | [h1;h2] -> h1+h2
    | [h1] -> h1
Run Code Online (Sandbox Code Playgroud)

这是否涵盖了所有情况?另外,为什么对于 3 个或更多元素我不能写类似 [h1;h2;h3;_] 的东西?
抱歉,如果这些问题看起来太简单,我刚刚开始学习 OCaml,我在网上找不到类似的东西。

ocaml pattern-matching

6
推荐指数
1
解决办法
4833
查看次数

字符不会存储在列表中

List<String> commandList = new ArrayList<String>();
int num = 0;
String command;

command = JOptionPane.showInputDialog(null, "Enter your commands here: ");

while (command.length() > num) {
    commandList.add(num, command.substring(num, num+1));
    num++;
}
Run Code Online (Sandbox Code Playgroud)

我让用户输入一个字符串,我想将字符串中的每个单独的字符存储到列表中。

我现在所拥有的并没有正确地做到这一点。我该如何解决这个问题?

java arrays list

4
推荐指数
1
解决办法
149
查看次数

标签 统计

arrays ×1

java ×1

list ×1

ocaml ×1

pattern-matching ×1