File 4491-Improve-lists-nth-2-and-nthtail-2.patch of Package erlang
From 46c351c138b13699f5661ec9603a9fd9706826aa Mon Sep 17 00:00:00 2001
From: Maria Scott <maria-12648430@hnc-agency.org>
Date: Wed, 1 Mar 2023 09:33:56 +0100
Subject: [PATCH] Improve lists:nth/2 and nthtail/2
---
lib/stdlib/src/lists.erl | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/lib/stdlib/src/lists.erl b/lib/stdlib/src/lists.erl
index b1be8e1c2d..25927f328a 100644
--- a/lib/stdlib/src/lists.erl
+++ b/lib/stdlib/src/lists.erl
@@ -196,8 +196,12 @@ reverse([A, B | L]) ->
T :: term().
nth(1, [H|_]) -> H;
-nth(N, [_|T]) when N > 1 ->
- nth(N - 1, T).
+nth(N, [_|_]=L) when is_integer(N), N > 1 ->
+ nth_1(N, L).
+
+nth_1(1, [H|_]) -> H;
+nth_1(N, [_|T]) ->
+ nth_1(N - 1, T).
-spec nthtail(N, List) -> Tail when
N :: non_neg_integer(),
@@ -205,10 +209,15 @@ nth(N, [_|T]) when N > 1 ->
Tail :: [T],
T :: term().
+nthtail(0, []) -> [];
+nthtail(0, [_|_]=L) -> L;
nthtail(1, [_|T]) -> T;
-nthtail(N, [_|T]) when N > 1 ->
- nthtail(N - 1, T);
-nthtail(0, L) when is_list(L) -> L.
+nthtail(N, [_|_]=L) when is_integer(N), N > 1 ->
+ nthtail_1(N, L).
+
+nthtail_1(1, [_|T]) -> T;
+nthtail_1(N, [_|T]) ->
+ nthtail_1(N - 1, T).
%% prefix(Prefix, List) -> (true | false)
--
2.35.3