[Haskell-beginners] 'Simple' function
Rein Henrichs
rein.henrichs at gmail.com
Wed Jun 10 18:46:32 UTC 2015
On Wed, Jun 10, 2015 at 10:50 AM Mike Houghton <mike_k_houghton at yahoo.co.uk>
wrote:
> The IO string has already been supplied - maybe via keyboard input -
>
A value of type IO String has been provided, but a value of type IO String *is
not a string.* In much the same way that `ls` is not a list of files, but
rather a recipe for retrieving a list of files, a value of type IO String
is a not a String but a recipe for performing IO that will retrieve a
String once it is executed. It is important to remember is that *evaluating
an IO action does not execute it*. Only the runtime system can do that, and
the only IO action the runtime system executes is main (which can, of
course, be composed of many other IO actions by using, e.g., the Monad
interface).
You can't write a function of type IO String -> String that "retrieves" the
string because there is no string to retrieve. There is only a recipe that
must be *executed* and that execution must stay within the IO context.
(It is possible to write this function using unsafePerformIO precisely
because unsafePerformIO instructs the runtime to ignore its usual safety
mechanisms (that ensure that IO actions can be used with in a safe, pure,
and referentially transparent way) and force the execution of the IO action
as part of its evalulation and, as the name suggests, this is unsafe for a
number of reasons and should only be used when the programmer is willing to
take on the obligation to prove that they are using it in safe way.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150610/a4efb8a7/attachment.html>
More information about the Beginners
mailing list