列表列表的平方 - ocaml

pri*_*ima 1 ocaml list

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

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

List.map (fun x -> x*x) [1; 2; 3];;
Run Code Online (Sandbox Code Playgroud)

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

[[1; 2]; [2; 3]]  --> [[1; 4]; [4; 9]]
Run Code Online (Sandbox Code Playgroud)

或者

[[1; 2; 3]; [4; 2; 0]] --> [[1; 4; 9]; [16; 4; 0]]
Run Code Online (Sandbox Code Playgroud)

例如。

谢谢

gas*_*che 5

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]] *)
Run Code Online (Sandbox Code Playgroud)