1

我知道如何对列表的元素进行平方,但是如何在列表中平方?

要平方列表的元素,我可以使用,例如:

List.map (fun x -> x*x) [1; 2; 3];;

如何在列表列表中执行此操作?

[[1; 2]; [2; 3]]  --> [[1; 4]; [4; 9]]

或者

[[1; 2; 3]; [4; 2; 0]] --> [[1; 4; 9]; [16; 4; 0]]

例如。

谢谢

4

1 回答 1

4
let square = fun x -> x * x;;
(* val square : int -> int = <fun> *)

List.map square;;
(* - : int list -> int list = <fun> *)

List.map (List.map square);;
(* - : int list list -> int list list = <fun> *)

List.map (List.map (fun x -> x*x)) [[1; 2]; [2; 3]];;
(* - : int list list = [[1; 4]; [4; 9]] *)
于 2013-04-21T10:21:39.347 回答