简体   繁体   English

列表的最后一个元素的值

[英]Value of the last element of a list

how to get the value of the last element of a List? 如何获取List的最后一个元素的值? I've noted that List.hd (or .Head) return an item, while List.tl (or .Tail) returns a List. 我注意到List.hd(或.Head)返回一个项目,而List.tl(或.Tail)返回一个List。

Is rev the List and get the hd the only way around? 转发列表并获得高清唯一的方法吗? Thanks. 谢谢。

Try this function. 试试这个功能。 It uses recursion, though it gets optimised to iteration anyway since it's tail recursion. 它使用递归,但无论如何都会优化迭代,因为它是尾递归。 In any case, it is most likely quicker than reversing the entire list (using List.rev ). 无论如何,它很可能比反转整个列表更快(使用List.rev )。

let rec last = function
    | hd :: [] -> hd
    | hd :: tl -> last tl
    | _ -> failwith "Empty list."

The answer of Pavel Minaev is definitely worth taking into account, however. 然而,Pavel Minaev的答案绝对值得考虑。 Nonetheless, the algorithm you have requested may be useful in some rare cases, and is the most efficient way to go about the task. 尽管如此,您所请求的算法在某些极少数情况下可能很有用,并且是执行该任务的最有效方法。

In general, if you need to do this, you're doing something wrong. 一般来说,如果你需要这样做,你就会做错事。 Since F# lists are single-linked, accessing the last element is costly - O(N) , where N is size of list . 由于F#列表是单链接的,因此访问最后一个元素是昂贵的 - O(N) ,其中N是list大小。 Try to rewrite your algorithm so that you always access the first element, not the last (which is O(1) ). 尝试重写算法,以便始终访问第一个元素,而不是最后一个元素(即O(1) )。 If you cannot do so, chances are good that your choice of list for a data structure wasn't correct in the first place. 如果你不能这样做,那么你首先选择的数据结构list是不正确的。

A quick & dirty way of doing it is by using List.reduce. 快速而肮脏的方法是使用List.reduce。 Assuming the list is called ls , 假设列表名为ls

let lastElement ls = List.reduce (fun _ i -> i) ls

As for efficiency, I agree with Pavel. 至于效率,我同意帕维尔的观点。

A more concise version based on Mitch's answer: 基于Mitch答案的更简洁版本:

let lastItem = myList |> List.rev |> List.head

The myList list is sent to List.rev function. myList列表被发送到List.rev函数。 The result is then processed by List.head 然后由List.head处理结果

Agreed, not so efficient to get the last element of list , or any other "enumerable" sequence. 同意,不是那么有效地获得list的最后一个元素,或任何其他“可枚举”序列。 That said, this function already exists in the Seq module, Seq.last . 也就是说,这个功能已经存在于Seq模块Seq.last

As a novice F# developer, I don't see what the harm is in doing the following 作为新手F#开发人员,我不知道在执行以下操作时会有什么危害

let mylist = [1;2;3;4;5]

let lastValue = mylist.[mylist.Length - 1]

Imperative in nature? 势在必行吗? Yes but no need for recursion. 是但不需要递归。

The regular way to work with lists in F# is to use recursion. 在F#中使用列表的常规方法是使用递归。 The first item in a list is the head (obviously) and the rest of the list is the tail (as oppose to the last item). 列表中的第一项是头(显然),列表的其余部分是尾部(与最后一项相反)。 So when a function recieves a list it processes the head and then recursively processes the rest of the list (the tail ). 因此,当一个函数收到一个列表时,它会处理头部 ,然后递归处理列表的其余部分( 尾部 )。

let reversedList = List.rev originalList
let tailItem = List.hd reversedList

我想你可以写

list.[0..list.Length-1]

You can call List.Head to get the first element of a list, such that the below expression evaluates to true: 您可以调用List.Head来获取列表的第一个元素,以便下面的表达式求值为true:

let lst = [1;2;3;4;5]
List.head lst = 1

However, calling List.Tail will return every element in the list after the first element, such that the below expression is true: 但是,调用List.Tail将返回第一个元素后面的列表中的每个元素,以便下面的表达式为true:

let lst = [1;2;3;4;5]
List.tail lst = [2;3;4;5]

Like some other people have mentioned, there isn't an efficient way in F# to get the tail end of a list, basic lists just aren't built with that functionality in mind. 就像其他人提到的那样,在F#中没有一种有效的方法可以获得列表的尾部,基本列表并没有考虑到这些功能。 If you really want to get the last element you're going to have to reverse your list first, and then take the new head (which was the previous tail). 如果你真的想要获得最后一个元素,你将不得不首先反转你的列表,然后采取新的头(这是前一个尾部)。

let lst = [1;2;3;4;5]
(List.head (List.rev lst) ) = 5

Below code worked fine with me, I've an array of integers, want to start from the 5th item, then take it minus the item number 下面的代码和我一起工作正常,我有一个整数数组,想从第5项开始,然后把它减去项目编号

Sum of [Array(xi) - Array(xi-5)] where i start at 5

The code used is: 使用的代码是:

series |> Array.windowed 5
       |> Array.fold (fun s x -> 
                            (x |> Array.rev |> Array.head) -  (x |> Array.head) + s) 0
       |> float

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM