首先,我在f#中非常非常的菜鸟,所以我需要你的帮助:)
我有一个包含50个列表的库,每个列表大约有10个条目
我需要做的是将所有50个列表加入一个大列表中。事情是我无法使用"因为"或可变变量。 我所做的(我认为是可怕的)是:
let rec finalList x =
if x < wallID.Length then List.append [interfaz.hola(wallID.Item(x)).[0].[1]] [finalList]
else listaFinal (x+1)
printfn "list %A" (listaFinal 10 )
WallID代表50个列表中的一个和interfaz.GetMuroHumano(wallID.Item(x))。[0]。[1]给我一个我需要的条目。 (现在,如果一个人可以获得每个wallID的数据之一,我可以)
再次,我是一个非常好的诺布,我希望你们能帮助我
谢谢
编辑:
所以现在它部分工作..
let rec finalList x y =
if x < wallID.Length then
if y < [interfaz.GetMuroHumano(wallID.Item(x)).[y]].Length then
let current = [interfaz.GetMuroHumano(wallID.Item(x)).[y].[1]]
let rest = finalList (x y+1)
List.append current rest
else finalList (x+1 y)
else []
即时通过调用函数finalList获取错误它表示&#34; y&#34;不是int而是字符串
答案 0 :(得分:2)
如果没有看到完整版本,很难说你的代码出了什么问题。正如丹尼尔指出的那样,有一个内置的库函数可以做到这一点 - 事实上,你甚至不需要List.collect
,因为List.concat
有一个列表列表。
但是,您仍然可以尝试使用原始代码 - 这对于理解功能概念很有用!我添加了一些评论,可以帮助您了解它应该如何运作:
let rec finalList x =
if x < wallIDLength then
// Get the list at the index 'x'
let current = interfaz.GetMuroHumano(wallID.Item(x))
// Recursively process the rest of the lists
let rest = finalList (x + 1)
// Check that both 'current' and 'rest' are variables
// of type list<'T> where 'T is the element type
List.append current rest
else
// Return empty list if we got too far
[]
// Start from the first index: 0
printfn "list %A" (finalList 0)
答案 1 :(得分:0)
let flatten xs = List.collect id xs