ref: 3617e0f7af66ff17acfcb539b3862a5f60010a5b
parent: e1c8eca3b4c123543c57e72a344895dcdb41fa15
author: Lennart Augustsson <lennart.augustsson@epicgames.com>
date: Mon Sep 18 19:50:45 EDT 2023
Make length tail recursive.
--- a/lib/Data/List.hs
+++ b/lib/Data/List.hs
@@ -100,8 +100,14 @@
_ : xs -> drop (n - 1) xs
length :: forall a . [a] -> P.Int
-length [] = 0
-length (_:xs) = 1 + length xs
+length =
+ -- Make it tail recursive and strict
+ let
+ rec r [] = r
+ rec r (_:xs) =
+ let r' = r + 1
+ in r' `primSeq` rec r' xs
+ in rec 0
zip :: forall a b . [a] -> [b] -> [(a, b)]
zip = zipWith (\ x y -> (x, y))
--
⑨