打印一个列表的列表

huangapple go评论62阅读模式
英文:

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
    | [] -&gt; []
    | x :: r -&gt; 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 : &#39;a list list -&gt; &#39;a list list = &lt;fun&gt;

答案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 &#39;a list list -&gt; &#39;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]]);;

huangapple
  • 本文由 发表于 2023年4月6日 22:12:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定