Suppose I have:
import qualified Data.ByteString.Lazy as L
bs1 :: L.ByteString
bs2 :: L.ByteString
Is L.append bs1 bs2 also lazy in that this will not cause me to consume all of bs1 immediately? Similarly with L.concat and unlike L.length which I understand will cause it to consume the entire bytestring because it needs to count all the bytes.
A simple way of determining how lazy a function is, is to pass it partially defined values. For example, let’s define a lazy bytestring of one chunk followed by an undefined tail.
As you can see, attempting to print it throws an exception after the first chunk. Now, let’s define another lazy ByteString and try to append them.
L.appendwas able to produce the first chunk of the resulting lazy ByteString. This means that it only had to look at the first chunk ofbs1to produce the first chunk of the result, i.e. it’s as lazy as you would expect it to be.