Home   Archive   Permalink

need help getting started

Hi,so i am quite new to the rebol community and have been assigned a project to work on.So to be frank i started reading rebol 2 days ago and i am quite confused since i worked with c++ before that.I am stuck at flow control and operators(sad i know).So basicaly i thought of when i moved from delphi to c++,basically if one of you guys can provide me with a rebol version of this small program i whipped up(flow control number check-the basics) it would be of great help to me,so here is my program and thanks in advance.
It inputs an integer number n and outputs the sum: 1+22+32+...+n2.I use input validation for n to be positive.
using namespace std;
int main()
     int n;
     cin >> n;
     if (n < 0) return 1;
     int sum = 0;
     int i = 0;
     while (i <= n) sum += i*i;
     cout << sum;
     return 0;

posted by:   martin     18-Jan-2011/11:43:38-8:00

oops actually it is 1+2^2+3^2+.....+n^2

posted by:   martin     18-Jan-2011/12:54:54-8:00

This is one way of doing it:
...the forever loop -- keeps ASKing until it gets an integer > 0
...the FOR loop adds up the squares
forever [
     n: ask "please enter an integer (1 or larger): "
     if all [not error? try [n: to-integer n] n > 0] [break]
     print "oops --- can't convert that to an integer greater than zero, please try again"
sum: 0.0
for i 1 n 1 [
     sum: i * i + sum
print sum

posted by:   Sunanda     18-Jan-2011/14:41:11-8:00

I'm assuming you wanted an i++ increment in your while loop:
while (i <= n) {
     sum += i*i;
A REBOL equivalent to your program is:
main: does [
     n: to-integer ask "Input integer: "
     if n < 0 [return 1]
     sum: 0
     i: 0
     while [i <= n] [
         sum: sum + (i * i)
         i: i + 1
     print sum
     return 0

posted by:   Nick     19-Jan-2011/1:15:50-8:00

But this is much more REBOLish:
do main: [
     sum: 0
     repeat i to-integer ask "number: " [sum: sum + (i * i)]

posted by:   Nick     19-Jan-2011/1:30:56-8:00

Here is another version in one-line:
     sum: 0 repeat i either 0 < n: to-integer ask "" [n] [1] [sum: i * i + sum]
Copy & paste this line into Console and try.

posted by:   Endo     19-Jan-2011/4:12:24-8:00

Here is the explanation:
set sum variable to 0. (sum: 0)
get input from user. (ask "")
convert it to integer. (to-integer)
if it is a positive number then return it else return 1. (either 0 < n [n] [1])
repeat the block [sum: i * i + sum], n times, count i from 1 to n.
if user input is not a number then to-integer gives invalid argument error. You can simply ignore that error by using an attempt.
     attempt [sum: 0 repeat i either 0 < n: to-integer ask "" [n] [1] [sum: i * i + sum]]
which returns none if any error occurs in the given block.
Or you can give show an error message, using try:
     either error? try [sum: 0 repeat i either 0 < n: to-integer ask "" [n] [1] [sum: i * i + sum]] [print "Please enter a number."] [sum]
if no error occurred return sum.

posted by:   Endo     19-Jan-2011/4:22:07-8:00

That's one of the things I love about REBOL - there's less cruft than any other language. All of my programs are SMALL, and that makes them easier to edit later. Martin, my example doesn't require the 'main' function - it can also all be reduced to one line:
s: 0 repeat i to-integer ask "" [s: i * i + s] s

posted by:   Nick     21-Jan-2011/0:15:08-8:00

And that is what I love about Rebol, in any other language when you try to make your code smaller it is getting complicated, unreadable. But it is not like that in Rebol, you can make your code smaller and nicer and faster at the same time.
At the beginning Rebol sources look complicated if you don't have enough knowledge. But then it looks simple, especially when you learn how to read the order-of-commands.

posted by:   Endo     21-Jan-2011/3:00:14-8:00