Calculating differences of subsequent elements of a sequence in F#

ぃ、小莉子 提交于 2019-12-10 17:09:12

问题


I have a sequence of floats in F#, and I need to get a sequence defined of (Math.Log currentElement)/(Math.Log previousElement). Obviously it will be shorter than the original sequence by one element.

What is the most elegant way to achieve this in F#? I was thinking to use a seq{} expression with a for loop inside, but even then handling the first element in a reasonably nice way seems difficult...


回答1:


items |> Seq.pairwise |> Seq.map (fun (x, y) -> log y / log x)



回答2:


Or if you prefer:

let f (x, y) = (log y) / (log x)

let ans = s |> Seq.pairwise |> Seq.map f


来源:https://stackoverflow.com/questions/12161015/calculating-differences-of-subsequent-elements-of-a-sequence-in-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!