英文:
How to print a list of lists
问题
我试图打印一个特定的列表,但它不起作用
let rec listes_paires l = 匹配 l 与
| [] -> []
| x :: r -> 如果 List.length x mod 2 = 0 则 (x :: (listes_paires r))
else ((x@x):: (listes_paires r));;
List.iter print_int (listes_paires [[]; [1];[1;2];[1;2;3];[];[5;4;3;2;1]]);;
错误消息显示:
第 5 行,字符 20-74:
错误:此表达式的类型是 int list list
但是预期一个类型为 int list 的表达式
类型 int list 与 int 类型不兼容
val listes_paires : 'a list list -> 'a list list = <fun>
英文:
I'm trying to print a specific list, but it doesn't work
let rec listes_paires l = match l with
| [] -> []
| x :: r -> if List.length x mod 2 = 0 then (x :: (listes_paires r))
else ((x@x):: (listes_paires r));;
List.iter print_int (listes_paires [[]; [1];[1;2];[1;2;3];[];[5;4;3;2;1]]);;
Error message says :
Line 5, characters 20-74:
Error: This expression has type int list list
but an expression was expected of type int list
Type int list is not compatible with type int
val listes_paires : 'a list list -> 'a list list = <fun>
答案1
得分: 1
You're not trying to print a list, but a list of lists. The type of listes_paires
, as you point out yourself, is a list list -> a list list
.
You can print a list of lists just by adding another List.iter
to iterate over the inner lists as well:
List.iter (List.iter print_int) (listes_paires [[]; [1];[1;2];[1;2;3];[];[5;4;3;2;1]]);
英文:
You're not trying to print a list, but a list of lists. The type of listes_paires
, as you point out yourself, is 'a list list -> 'a list list
You can print a list of lists just by adding another List.iter
to iterate over the inner lists as well:
List.iter (List.iter print_int) (listes_paires [[]; [1];[1;2];[1;2;3];[];[5;4;3;2;1]]);;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论