[Haskell-cafe] Efficiency of passing function arguments

KS kramosik at gmail.com
Sat Apr 4 07:53:27 UTC 2020


Hello everyone,

I was coming from a C++ background and I always wondered if there is a
distinction between pass-by-value and pass-by-reference in Haskell (or
pass-by-thunk? since an argument might be partially evaluated due to
laziness in Haskell).

Is there a way to guarantee that an function argument is passed in an
efficient manner? For example, if we have a data type

data Document
  = Document { title   :: Text
             , author  :: Name
             , content :: Text
             -- and potentially many more
             }
doc = Document {..} -- omitted

and we want to pass doc to a function, we should ideally hope that doc is
not getting duplicated when passing it as an argument. Another example
might be the Reader monad, where we are passing possibly the same
environment to multiple functions. In C++, we could either pass by a
pointer or a reference, but I'm not sure what the best practices are in
Haskell.

The most relevant construct in Haskell seems to be  IORef, though I'm not
sure if this is the correct way to use it. For exmaple, if I want to
pre-load a template document on disk as Text or ByteString into the memory
when the program starts, should I put it in a IORef and pass it into (more
than one) functions or should I pass it into the function directly? Should
I worry about this at all?

This question might be related to the implementation of the ghc compiler.
It would be better if anyone could shed some lights on the implementation
details.

Thank you,
Kram
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200404/c734ece7/attachment.html>


More information about the Haskell-Cafe mailing list