From 812ac3ac100a5800cfbfeab8262c41ec248376cc Mon Sep 17 00:00:00 2001 From: inoas Date: Mon, 27 Jun 2022 23:33:43 +0200 Subject: make List.contains tco compatible --- src/gleam/list.gleam | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 6977338..71f332a 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -167,7 +167,15 @@ pub fn is_empty(list: List(a)) -> Bool { pub fn contains(list: List(a), any elem: a) -> Bool { case list { [] -> False - [head, ..rest] -> head == elem || contains(rest, elem) + list -> do_contains(list, elem, False) + } +} + +fn do_contains(list: List(a), any elem: a, accumulator: Bool) -> Bool { + case list { + _ if accumulator == True -> True + [] -> accumulator + [head, ..rest] -> do_contains(rest, elem, head == elem || accumulator) } } -- cgit v1.2.3