[Hugs-users] Excerpt from "Yet another Haskell Tutorial"
Bulat Ziganshin
bulat.ziganshin at gmail.com
Sat Aug 5 11:40:27 EDT 2006
Hello aldirithms,
Saturday, August 5, 2006, 5:11:19 PM, you wrote:
> <<After all, suppose you have a function that reads a string from
> the keyboard. Ifyou call this function twice, and the user types
> something the first time and something else the second time, then
> you no longer have a function, since it would return two different values.>>
you can see it in that way: something which has no parameters and
return different values on different calls is not a function in
_mathematical_ meaning of this word. Haskell functions is a
mathematical (it also named "pure") functions. In order to deal with
such real-world issues I/O functions just have additional hidden
argument which is considered as containing "state of the world" at the
moment of function call. this allow us to think that we "getstr" is
really a pure (mathematical) function, just called with different
parameters each time (because state of the world is definitely changed
between calls :D )
you can read further explanations at the http://haskell.org/haskellwiki/IO_inside
> How do I have to interpret this? I cannot make sense of it for I do
> not see where the definition of a function that I know is violated.
> I view it that way: The string is just a kind of argument to the
> string-reading function and that different strings (i.e. different
> arguments) yield different return values is a commonplace phenomenon
> with functions, isn't it? How do I have to alter this (over?)simple
> interpretation to see the point the author wants to make?
in C, function reading string form keyboard can be defined as
char *getstr();
as you can see, it don't have string arguments
in Haskell, such function will be defined as
getstr :: IO String
which is internally translated to something like
getstr :: WorldState -> String
and compiler silently adds different "world state" values to each
call, making them different in mathematical sense
--
Best regards,
Bulat mailto:Bulat.Ziganshin at gmail.com
More information about the Hugs-Users
mailing list