Commit b709f0a0 authored by Joachim Breitner's avatar Joachim Breitner
Browse files

Make last a good consumer

Summary:
Make last a good consumer simply by implementing it as foldl. This fixes Trac: #9339.
Thanks to David Feuer for bringing it up.

Test Plan: perf/should_run/T9339 + general validation

Reviewers: austin

Reviewed By: austin

Subscribers: phaskell, simonmar, relrod, carter

Differential Revision: https://phabricator.haskell.org/D86

Trac Issues: #9339
parent 7aabfa62
......@@ -83,11 +83,8 @@ last [x] = x
last (_:xs) = last xs
last [] = errorEmptyList "last"
#else
-- eliminate repeated cases
last [] = errorEmptyList "last"
last (x:xs) = last' x xs
where last' y [] = y
last' _ (y:ys) = last' y ys
-- use foldl to allow fusion
last = foldl (\_ x -> x) (errorEmptyList "last")
#endif
-- | Return all the elements of a list except the last one.
......
-- Tests that `last` successfully fuses.
main :: IO ()
main = print $ last $ filter odd $ [1::Int ..10000000]
......@@ -379,3 +379,12 @@ test('T9203',
only_ways(['normal'])],
compile_and_run,
['-O2'])
test('T9339',
[stats_num_field('bytes allocated',
[ (wordsize(64), 80050760, 5) ]),
# w/o fusing last: 320005080
# 2014-07-22: 80050760
only_ways(['normal'])],
compile_and_run,
['-O2'])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment