我使用OCaml中写一个函数,整数的列表和一个int元素,并返回对的列表,其中每对中的第一个元素是INT元件和该对中的第二个元素是列表中的成员。例如,假设我有数字1和列表[10; 20; 30]作为输入。我喜欢函数返回[(1,10); (1,20); (1,30)]。我写了下面的功能:配对的int与整数的列表OCaml中
let rec f (lst : int list) (elm : int) : (int*int) list =
match lst with
| [] -> failwith "empty list"
| [x] -> [(x, elm)];;
我收到以下错误:
Characters 59-120:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
_::_::_ val f : int list -> int -> (int * int) list = <fun>
我缺少什么?
谢谢你,我如何才能在有一个以上的元素列表中的元素呢?我知道有像h :: t这样的东西,但是我没有成功地使用它。 – user1787222
@ user1787222一次只能获取列表的头元素。为了获得所有的元素,你需要使用递归,并且每次你把头伸出来。 –
@ user1787222我更新了我的答案。如果您认为它很好,请将我的答案标记为正确 –