Value of the last element of a list

你说的曾经没有我的故事 提交于 2019-11-28 02:39:22

问题


how to get the value of the last element of a List? I've noted that List.hd (or .Head) return an item, while List.tl (or .Tail) returns a List.

Is rev the List and get the hd the only way around? Thanks.


回答1:


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).

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. Nonetheless, the algorithm you have requested may be useful in some rare cases, and is the most efficient way to go about the task.




回答2:


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. Try to rewrite your algorithm so that you always access the first element, not the last (which is 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.




回答3:


A quick & dirty way of doing it is by using List.reduce. Assuming the list is called ls,

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

As for efficiency, I agree with Pavel.




回答4:


A more concise version based on Mitch's answer:

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

The myList list is sent to List.rev function. The result is then processed by List.head




回答5:


Agreed, not so efficient to get the last element of list, or any other "enumerable" sequence. That said, this function already exists in the Seq module, Seq.last.




回答6:


As a novice F# developer, I don't see what the harm is in doing the following

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

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

Imperative in nature? Yes but no need for recursion.




回答7:


The regular way to work with lists in F# is to use recursion. 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



回答8:


I think you can just write

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



回答9:


You can call List.Head to get the first element of a list, such that the below expression evaluates to 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:

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. 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



回答10:


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

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


来源:https://stackoverflow.com/questions/1175056/value-of-the-last-element-of-a-list

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