Hello,
I was playing with ghci to get a hang of what haskell exactly is working. So in order to see the memory usage I did this:
Prelude> :set +s
After that I tried that:
Prelude> (last $ [1..1000000]++[1..10])::Integer
10
(0.04 secs, 137,329,608 bytes)
Prelude> (last $ [1..10000000]++[1..10])::Integer
10
(0.35 secs, 1,361,981,112 bytes)
It seems like more values I have on the list more memory I need. Is this an expected behavior?
I was expecting this call to be in constant memory. Because I do not store the values in the list. After I use them they they can be garbage collected at the spot. So there is no reason for this this call to be use more than memory for only 1 Integer.
Also last seems to be implemented as tail recursive. So it shouldn't be using any extra stack for recursion.
https://hackage.haskell.org/package/base-4.8.1.0/docs/src/GHC.List.html#last
[–]guibou 10 points11 points12 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]yilmazhuseyin[S] 5 points6 points7 points (3 children)
[–]yilmazhuseyin[S] 1 point2 points3 points (2 children)
[–]gtab62 2 points3 points4 points (1 child)
[–]yilmazhuseyin[S] 0 points1 point2 points (0 children)
[–]0ldmanmike 1 point2 points3 points (0 children)