[Haskell-beginners] Definition of last: why complicate it?

Daniel Fischer daniel.is.fischer at googlemail.com
Fri Apr 4 20:01:19 UTC 2014


On Friday 04 April 2014, 13:51:42, Dimitri DeFigueiredo wrote:
> I think that in the second case (ulast' in your test) still performs 2
> checks per iteration as it has to check for the empty list on its second
> argument at every recursive call (that's how it knows it's done). So, we
> are performing the same number of iterations.

But in each iteration, there is only one case, versus the two in the plast 
version, we have

ulast' y ys = case ys of
                [] -> y
                z:zs -> ulast' z zs

> Maybe there's another
> reason for the "ugly"  version to be preferred. Even if it is
> performance and not lazyness, I still don't understand what would make
> it faster.
> 
> Thanks,
> 
> Dimitri
> 
> Em 04/04/14 13:26, Rudy Matela escreveu:
> > You can test the speed in your ghci:

No.

ghci is not a measure for efficiency of code. You need compiled (with 
optimisations) code to be able to judge and compare efficiency. Even when 
loading compiled and optimised code in ghci, ghci's runtime still behaves 
different from the runtime of the compiled code in binaries.

To check the efficiency of code, ghc -O2 and the criterion library are your 
friends.

Cheers,
Daniel


More information about the Beginners mailing list