String 从许多其他列表中创建一个列表f#
首先,我在f#非常非常不在行,所以我需要你的帮助:) 我有一个库,有50个列表,每个列表大约有10个条目 我需要做的是将所有50个列表合并成一个大列表。问题是我不能使用“for”或可变变量。 我所做的(我认为这是可怕的)是:String 从许多其他列表中创建一个列表f#,string,f#,append,String,F#,Append,首先,我在f#非常非常不在行,所以我需要你的帮助:) 我有一个库,有50个列表,每个列表大约有10个条目 我需要做的是将所有50个列表合并成一个大列表。问题是我不能使用“for”或可变变量。 我所做的(我认为这是可怕的)是: 让rec finalList x= 如果x
让rec finalList x=
如果x
WallID表示50个列表中的一个,interfaz.GetMuroHumano(WallID.Item(x))[0][1]为我获取所需的条目之一。(目前,如果a只需要获取每个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 []
让rec finalList x y=
如果x
vut im调用函数finalList时出错它说“y”不是int而是字符串如果看不到完整的版本,很难说出代码有什么问题。正如Daniel指出的,有一个内置的库函数可以实现这一点——事实上,您甚至不需要
List.collect
,因为有List.concat
可以获取列表列表
let flatten xs = List.collect id xs
但是,您仍然可以尝试让原始代码正常工作-这对于理解函数概念非常有用!我添加了一些可以帮助您理解其工作原理的注释:
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)
让rec finalList x=
如果x<壁长,则
//获取索引“x”处的列表
let current=interfaz.GetMuroHumano(wallID.Item(x))
//递归地处理其余的列表
让rest=finalList(x+1)
//检查“当前”和“剩余”是否都是变量
//对于listOops类型,我忘记了concat
。hmmm我得到的错误是当前的类型是string[]],但需要“列表…”。。。。如何将当前值转换为列表?string[][]
是一个数组数组。您可以使用Array.concat
将其转换为string[]
,然后使用List.ofArray
获取List
。。。。但您的原始代码有一些索引,我不太了解,所以我有点修复了它,但我忘记了一个迭代。。。但是现在它不起作用了。。我忘记了一次迭代。。。我收到格式错误(检查我编辑的原始帖子你正在调用finalList,两个参数组合在一起。请尝试finalList x(y+1)和finalList(x+1)y
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)