f# - Value of the last element of a list -
How to obtain the value of the last element of a list? I have seen that List.hd (or.) Returns an item, while List.tl (or .ail) returns a list.
Modify the list and find the only way HD? Thank you.
Try this function. It uses recursive, although it is optimized for recursion anyway because this tail is recursive. In any case, expecting the entire list to be reversed (using List.rev
) Happens faster.
REC final = give function. HD :: [] - & gt; HD | HD :: TL - & gt; Last TL | _ - & gt; Failed "empty list."
Powell Minave's answer is definitely worth considering, however, the algorithm you requested may be useful in some rare cases, and the most efficient way to go about the work is.
Comments
Post a Comment