Value of the last element of a list

前端 未结 10 941
小蘑菇
小蘑菇 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:23

    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
    

提交回复
热议问题