OCaml 矩阵操作
OCaml matrix manipulation
我想用这种结构编写一个 OCaml 函数:
rows : int list list -> char list -> int list = <fun>
。
我遇到了一些麻烦,因为我对这门语言还很陌生。
该程序应该获取每个列表并对其元素执行加法或乘法等操作。例如:rows [[1;2;0];[4;5;6];[1;2;9]] [’+’;’-’;’*’];;-: int list = [3;-7;18]
提前感谢您的帮助。
这是基本问题吗?注:重要部分留给大家回答。
let nums =
[
[1;2;0];
[4;5;6];
[1;2;9];
]
let ops = ['+'; '-'; '*';]
let rec rows l o =
match (l, o) with
| ([],[]) -> []
| (hd::tl, op::tlo) ->
(
match hd with
| [] -> 0::(rows tl tlo)
| h::t -> (*The important part goes here*)
)
| _ -> failwith "Uneven list"
let ans = rows nums ops
我想用这种结构编写一个 OCaml 函数:
rows : int list list -> char list -> int list = <fun>
。
我遇到了一些麻烦,因为我对这门语言还很陌生。
该程序应该获取每个列表并对其元素执行加法或乘法等操作。例如:rows [[1;2;0];[4;5;6];[1;2;9]] [’+’;’-’;’*’];;-: int list = [3;-7;18]
提前感谢您的帮助。
这是基本问题吗?注:重要部分留给大家回答。
let nums =
[
[1;2;0];
[4;5;6];
[1;2;9];
]
let ops = ['+'; '-'; '*';]
let rec rows l o =
match (l, o) with
| ([],[]) -> []
| (hd::tl, op::tlo) ->
(
match hd with
| [] -> 0::(rows tl tlo)
| h::t -> (*The important part goes here*)
)
| _ -> failwith "Uneven list"
let ans = rows nums ops