[clean-list] Help Please: Beginner to Clean.. Console I/O
Khurram Khan
kkjcid at hotmail.com
Mon May 9 17:08:12 MEST 2011
Dear Peter
Thanks for taking time out and translating the C++ code for us. It was very useful and your explanations as well. We all appriciate your and other members of clean-list's contribution, who helped us understand things.
Regards
Khurram
Date: Mon, 9 May 2011 14:11:20 +0200
From: P.Achten at cs.ru.nl
To: kkjcid at hotmail.com
CC: clean-list at cs.ru.nl
Subject: Re: [clean-list] Help Please: Beginner to Clean.. Console I/O
Dear Khurram and everybody else,
...back from holidays...
Thanks everybody for the excellent explanations! For your
convenience, I've included the Clean version of your C++ example in
this message below. I hope that it demonstrates that basic console
I/O in Clean is not that different from basic console I/O in C++.
The key differences are that you must pass around the modified
environments (notably world and console), and that basic Clean file
I/O does not offer a >> operation as C++ does. So, for the
input of integers I use freadline (which reads an entire line
including terminating newline character), and a local utility
function, turn_to_Int, that first removes the trailing newline
character (str%(0,size str-2)) and then uses the standard toInt
function to obtain the integer value. Note that if you forget to
eliminate the trailing newline character, toInt will produce 0. This
is actually also the case for any input that does not match an
integer denotation exactly (for instance, toInt "100 " also
returns 0).
Regards,
Peter
On 5/4/2011 3:46 PM, Khurram Khan wrote:
Many thanks for your reply. I've read through let-before section
and it has developed a bit more understanding now on what is
happening the "hello1" example. I should've been more specific
with my question last time. Actually me and a couple of my
classmates are working together to learn Clean and finding it very
hard to understand I/O operations in Clean. We believe that
probably Clean user-guides are written for people who have some
previous knowledge of functional programming, but we are very keen
to learn the language and we have up til now found it very
interesting, specially the fact that it, in many ways, resembles
to how you write functions in pure mathematical sense.
However, as we are so much used to C/C++, we were thinking that
Clean must also have some basic way of doing simple I/O
operations, like we have "cout" and "cin" in C++, but we are
unable to develop that understanding from examples "hello1" and
"hello2". We have also looked at some code provided with Clean IDE
software as examples, but those examples are also on
intermediate/advanced level, compared to where we are at the
moment.
We were thinking if its possible for you to translate this simple
code of C++ in Clean and provide some comments on it as well. We
are certain that it will help solve I/O dilemma for us, and we are
deeply thankful for the time you take out to help us.
/* Just a simple program performing input/output to user console
and performing a calculation*/
void main ()
{
double FirstNumber=0.0;
double SecondNumber=0.0;
double ThirdNumber =0.0;
double Result =0.0;
cout << "This program will accept three integer
numbers and find their average value\n\n";
cout << "Enter First Number: ";
cin >> FirstNumber;
cout << "\nEnter Second Number: ";
cin >> SecondNumber;
cout << "\nEnter Third Number: ";
cin >> ThirdNumber;
Result = (FirstNumber+SecondNumber+ThirdNumber)/3;
cout << "\nThe result is : " << Result <<
"\n\n";
system("pause");
}
===========================
Start :: *World -> *World
Start world
# (console,world) = stdio world
# console = console <<< "This program will
accept three integer numbers and find their average value\n\n"
# console = console <<< "Enter First Number: "
# (i1,console) = freadline console
# console = console <<< "\nEnter Second
Number: "
# (i2,console) = freadline console
# console = console <<< "\nEnter Third Number:
"
# (i3,console) = freadline console
# (i1,i2,i3) = (turn_to_Int i1,turn_to_Int i2,turn_to_Int
i3)
# result = fromInt (i1+i2+i3) / 3.0
# console = console <<< "\nThe result is : "
<<< result <<< "\n\n"
# (ok,world) = fclose console world
| not ok = abort "Could not close stdio.\n"
| otherwise = world
turn_to_Int str = toInt (str%(0,size str-2))
===========================
p.s.: the evaluation of every Clean program is driven by the need to
print the outcome of the Start-function. In case of these
interactive programs, the outcome is a value of type World.
Internally, the World is represented by an integer of value 65536.
This is the reason why that value is also printed on the console. As
commented by Isaac, you can suppress printing this result via the
CleanIDE.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/clean-list/attachments/20110509/eb7b589d/attachment-0001.html>
More information about the clean-list
mailing list