[clean-list] Help Please: Beginner to Clean.. Console I/O
Jerzy Karczmarczuk
jerzy.karczmarczuk at unicaen.fr
Thu May 5 21:40:11 MEST 2011
Khurram Khan:
We believe that probably Clean user-guides are written for people
who have some previous knowledge of functional programming (...)
Yes, I believe you are right. If you and your friends have problems with
the *understanding* of the IO issues, please, instead of following the
advice to read about some syntactic structures in Clean, look into some
general articles on functional programming, purity and laziness,
otherwise you will be constantly perplexed (which is not so bad after
all). The first thing to understand is the IMPLICATIONS of the fact that
a functional program does not modify your data structures.
we are also aware of the fact that by doing regular I/O operations
in functional programming might defeat the purpose of writing a
program in a functional language because it will then resemble
procedural programming style,(...)
This is not exact. Psychologically, perhaps (that's why in Haskell we
have the "do" block), but the functional IO is ... functional. Even the
global environment of your program has a functional avatar. That's why
you write:
(console, wrld) = stdio wrld
A file (here: the console) must be picked out of this environment (the
class: FileSystem, whose instance is the World, a possible parameter of
Start). But the referential transparency forbids you to write, say :
console = stdio(), or console=Stdio(world) since making a link to your
file system changes the world. So, you create a new world, and the
"uniqueness" property of Clean ensures that you cannot reach the
previous one, it is "destroyed". So, you may reuse its name. Of course
"in reality" (btw., somebody knows what is "reality"?), this reduces to
the modification of the unique world exemplar.
And the Ship Sails On.
To read a line from the console, you write
(line, console) = freadline console
Since reading modifies the console, its "new instantiation" is passed as
a part of the result. If you want to write something, you have to write
console = fwrites "And the Ship Sails On\n" console
and not just:fwrites "something". This IS SIMPLE, you need only to keep
in your mind, that nothing is implicit, under the rug, and nothing is
"modified", rather "recreated".
I believe that we could help you in a more efficient way, if you pose
some concrete questions, detailed, and "atomic".
All the best
Jerzy Karczmarczuk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/clean-list/attachments/20110505/b4c66b9e/attachment-0001.html>
More information about the clean-list
mailing list