4
我构建了一个简单的函数,在给定列表的情况下,返回该列表的第一个n
元素。列表清单上的操作
let rec first l n =
match l, n with
(_, 0) -> l
| (x::xs 1) -> [x]
| (x::xs n) -> x::(first xs (n-1))
但是如果输入是列表列表而不是列表呢?我想建立一个函数,给出一个列表,从返回每个列表中的第一个n
元素。 例如:
first [[1; 2]; [5; 6; 7]; []; []; [9; 8; 0]] 1 =
[1; 5; 9]
我试图找出一种方法,通过使模式列表的列表:
let rec first l n =
match l, n with
(_, 0) -> l
| ([[x]::[xs]], n) -> [x::[first xs (n-1)]]
它不工作,但我更关心的办法。这是对的吗?
你想达到什么目的? –
对不起@FyodorSoikin,我忘了指定问题。现在应该没问题。 – Worice
看看这里:https://fsharpforfunandprofit.com/posts/elevated-world/ –