Value of the last element of a list

前端 未结 10 981
小蘑菇
小蘑菇 2020-12-18 18:55

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

10条回答
  •  清歌不尽
    2020-12-18 19:08

    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.

提交回复
热议问题