August 21, 2024

P01 - Find the last element of a list.

Example:

> p01:last([1,2,3,34,234,2,423,423,4]).
  4

erlang

%
% Find the last element of a list.
%

-module(p01).
-export([last/1]).

last([Head | []]) -> 
    Head;

last([_ | Tail]) -> 
    last(Tail).

% To test:
% p01:last([1,2,3])
% p01:last([1])
%
Be first to comment
Leave a reply