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 checkthe 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. #include <iostream> 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 18Jan2011/11:43:388:00
oops actually it is 1+2^2+3^2+.....+n^2
posted by: martin 18Jan2011/12:54:548: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: tointeger 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 18Jan2011/14:41:118:00
Martin, I'm assuming you wanted an i++ increment in your while loop: while (i <= n) { sum += i*i; i++; } A REBOL equivalent to your program is: main: does [ n: tointeger 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 ] main
posted by: Nick 19Jan2011/1:15:508:00
But this is much more REBOLish: do main: [ sum: 0 repeat i tointeger ask "number: " [sum: sum + (i * i)] sum ]
posted by: Nick 19Jan2011/1:30:568:00
Here is another version in oneline: sum: 0 repeat i either 0 < n: tointeger ask "" [n] [1] [sum: i * i + sum] Copy & paste this line into Console and try.
posted by: Endo 19Jan2011/4:12:248:00
Here is the explanation: set sum variable to 0. (sum: 0) get input from user. (ask "") convert it to integer. (tointeger) 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 tointeger gives invalid argument error. You can simply ignore that error by using an attempt. attempt [sum: 0 repeat i either 0 < n: tointeger 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: tointeger ask "" [n] [1] [sum: i * i + sum]] [print "Please enter a number."] [sum] if no error occurred return sum.
posted by: Endo 19Jan2011/4:22:078: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 tointeger ask "" [s: i * i + s] s
posted by: Nick 21Jan2011/0:15:088: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 orderofcommands.
posted by: Endo 21Jan2011/3:00:148:00
