[Haskell-cafe] Efficiency of passing function arguments

Georgi Lyubenov godzbanebane at gmail.com
Sat Apr 4 08:31:41 UTC 2020


What the other person said - passing an argument should always be "by
reference", since there is no danger in doing otherwise.
However, "copying" does happen when you deconstruct and reconstruct a value.
For example, consider this idList:
idList :: [a] -> [a]
idList [] = []
idList (x:xs) = x : idList xs
Semantically this is the same as (a stricter) id, but we're deconstructing
values and then constructing other ones (putting them in new boxes), so in
effect, we're "copying" the input list.
I'm not sure if ghc will do some magic optimisation to transform this
particular function into an operation with no copying, but you can imagine
something more complicated like map/foldr/reverse etc where it isn't
possible (although other stuff like fusion is, to eliminate intermediate
results from composed maps for example).

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200404/216714bc/attachment.html>

More information about the Haskell-Cafe mailing list