Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 构造N*N元素的子列表_List_Integer_Ocaml - Fatal编程技术网

List 构造N*N元素的子列表

List 构造N*N元素的子列表,list,integer,ocaml,List,Integer,Ocaml,我试图仅使用list.map、list.filters和匿名函数在ocaml中构建一个列表 我想得到的是: - : int list list = [[2; 2]; [5; 5; 5; 5; 5]; [7; 7; 7; 7; 7; 7; 7]; [3; 3; 3]; 2 [12; 12; 12; 12; 12; 12; 12; 12; 12; 12; 12; 12]; [4; 4; 4; 4]; ... ] 从这个列表中 let entiers = [2; 5; 7; 3; 12; 4; 9;

我试图仅使用list.map、list.filters匿名函数在ocaml中构建一个列表

我想得到的是:

- : int list list = [[2; 2]; [5; 5; 5; 5; 5]; [7; 7; 7; 7; 7; 7; 7]; [3; 3; 3];
2
[12; 12; 12; 12; 12; 12; 12; 12; 12; 12; 12; 12]; [4; 4; 4; 4]; ... ]
从这个列表中

let entiers = [2; 5; 7; 3; 12; 4; 9; 2; 11];;
到目前为止,我所尝试的:

List.map (fun n acc -> acc = n if acc = 0 then [] else n :: fun n acc -1 ) entiers;;
但是我有一个语法错误,所以我有点卡住了

有人能帮我解决这个问题吗?
谢谢大家!

正如@ChristopheRiolo注释所示,您似乎试图定义一个递归的匿名函数。这是非常困难的,因为您确实需要函数名才能递归地调用自己

特别是,它看起来像这样的片段:

fun n (acc - 1)
用于递归调用。但是,这根本不是函数调用,而是新(内部)匿名函数定义的开始。(这是语法错误的来源,因为它不是有效的lambda定义。)

如果在调用
map
之外将要映射的函数定义为一个带名称的函数,则效果会更好

大致如下:

let rec helper n =
    if n = 0 then [] else n :: helper (n - 1)
in
List.map helper ...

(助手的这个定义可能不正确;我只是想展示一下您可能想要使用的总体布局。)

第一个
acc=n
是可疑的。还可以考虑类型。
acc
是整数吗?另外,当您执行
acc-1
时,您需要括号。最后,递归匿名函数不是不可能的,但最好忘记它。