mod package:base-prelude

integer modulus, satisfying
(x `div` y)*y + (x `mod` y) == x
Generalisation of mod to any instance of Real
Catch any IOException that occurs in the computation and throw a modified version.
Mutate the contents of an IORef. Be warned that modifyIORef does not apply the function strictly. This means if the program calls modifyIORef many times, but seldom uses the value, thunks will pile up in memory resulting in a space leak. This is a common mistake made when using an IORef as a counter. For example, the following will likely produce a stack overflow:
ref <- newIORef 0
replicateM_ 1000000 $ modifyIORef ref (+1)
readIORef ref >>= print
To avoid this problem, use modifyIORef' instead.
Strict version of modifyIORef
A slight variation on modifyMVar_ that allows a value to be returned (b) in addition to the modified value of the MVar.
Like modifyMVar, but the IO action in the second argument is executed with asynchronous exceptions masked.
Like modifyMVar_, but the IO action in the second argument is executed with asynchronous exceptions masked.
An exception-safe wrapper for modifying the contents of an MVar. Like withMVar, modifyMVar will replace the original contents of the MVar if an exception is raised during the operation. This function is only atomic if there are no other producers for this MVar.
Mutate the contents of an STRef.
>>> :{
runST (do
ref <- newSTRef ""
modifySTRef ref (const "world")
modifySTRef ref (++ "!")
modifySTRef ref ("Hello, " ++)
readSTRef ref )
:}
"Hello, world!"
Be warned that modifySTRef does not apply the function strictly. This means if the program calls modifySTRef many times, but seldom uses the value, thunks will pile up in memory resulting in a space leak. This is a common mistake made when using an STRef as a counter. For example, the following will leak memory and may produce a stack overflow:
>>> import Control.Monad (replicateM_)

>>> :{
print (runST (do
ref <- newSTRef 0
replicateM_ 1000 $ modifySTRef ref (+1)
readSTRef ref ))
:}
1000
To avoid this problem, use modifySTRef' instead.
Strict version of modifySTRef
Lm: Letter, Modifier
Sk: Symbol, Modifier
Three kinds of buffering are supported: line-buffering, block-buffering or no-buffering. These modes have the following effects. For output, items are written out, or flushed, from the internal buffer according to the buffer mode:
  • line-buffering: the entire output buffer is flushed whenever a newline is output, the buffer overflows, a hFlush is issued, or the handle is closed.
  • block-buffering: the entire buffer is written out whenever it overflows, a hFlush is issued, or the handle is closed.
  • no-buffering: output is written immediately, and never stored in the buffer.
An implementation is free to flush the buffer more frequently, but not less frequently, than specified above. The output buffer is emptied as soon as it has been written out. Similarly, input occurs according to the buffer mode for the handle:
  • line-buffering: when the buffer for the handle is not empty, the next item is obtained from the buffer; otherwise, when the buffer is empty, characters up to and including the next newline character are read into the buffer. No characters are available until the newline character is available or the buffer is full.
  • block-buffering: when the buffer for the handle becomes empty, the next block of data is read into the buffer.
  • no-buffering: the next input item is read and returned. The hLookAhead operation implies that even a no-buffered handle may require a one-character buffer.
The default buffering mode when a handle is opened is implementation-dependent and may depend on the file system object which is attached to that handle. For most implementations, physical files will normally be block-buffered and terminals will normally be line-buffered.
Specifies the translation, if any, of newline characters between internal Strings and the external file or stream. Haskell Strings are assumed to represent newlines with the '\n' character; the newline mode specifies how to translate '\n' on output, and what to translate into '\n' on input.
A mode that determines the effect of hSeek hdl mode i.
Atomically modifies the contents of an IORef. This function is useful for using IORef in a safe way in a multithreaded program. If you only have one IORef, then using atomicModifyIORef to access and modify it will prevent race conditions. Extending the atomicity to multiple IORefs is problematic, so it is recommended that if you need to do anything more complicated then using MVar instead is a good idea. atomicModifyIORef does not apply the function strictly. This is important to know even if all you are doing is replacing the value. For example, this will leak memory:
ref <- newIORef '1'
forever $ atomicModifyIORef ref (\_ -> ('2', ()))
Use atomicModifyIORef' or atomicWriteIORef to avoid this problem.
Strict version of atomicModifyIORef. This forces both the value stored in the IORef and the value returned. The new value is installed in the IORef before the returned value is forced. So
atomicModifyIORef' ref (x -> (x+1, undefined))
will increment the IORef and then throw an exception in the calling thread.
simultaneous div and mod