我正在尝试编写一个函数,根据给定的相等函数确定连续重复,seq<'a> 但是有一个扭曲:我需要从一系列重复项中删除最后一个副本,使其成为结果序列.例如,如果我有一个序列[("a", 1); ("b", 2); ("b", 3); ("b", 4); ("c", 5)],并且我fun ((x1, y1),(x2, y2)) -> x1=x2用来检查相等性,我想看到的结果是[("a", 1); ("b", 4); ("c", 5)].这个函数的关键在于我有数据点进入,有时数据点合法地具有相同的时间戳,但我只关心最新的一个,所以我想抛弃前面的那些具有相同的时间戳.我实现的功能如下:
let rec dedupeTakingLast equalityFn prev s = seq {
match ( Seq.isEmpty s ) with
| true -> match prev with
| None -> yield! s
| Some value -> yield value
| false ->
match prev with
| None -> yield! dedupeTakingLast equalityFn (Some (Seq.head s)) (Seq.tail s)
| …Run Code Online (Sandbox Code Playgroud) 我的问题是什么时候输入Seq.为什么没有Seq.tail功能?
在这个不将序列转换为列表的代码Seq.tail中,递归函数中没有可用的函数.是因为Seq.initInfinte用于创建序列,还是有其他原因?
open System
let readList() =
Seq.initInfinite (fun _ -> Console.ReadLine())
|> Seq.takeWhile (fun s -> (s <> ""))
|> Seq.map (fun x -> Int32.Parse(x))
let rec listLen list1 acc =
if Seq.isEmpty list1 then
acc
else
(* There is no Seq.tail available. Compile error. *)
listLen (Seq.tail list1) (acc + 1)
[<EntryPoint>]
let main argv =
let inList = (readList())
let inListLen = listLen inList 0
printfn "%A" inListLen
0 …Run Code Online (Sandbox Code Playgroud)