World: r3wp
[I'm new] Ask any question, and a helpful person will try to answer.
older newer | first last |
GrahamC 25-Dec-2010 [4041x2] | use Cheyenne |
which is also free :) | |
alemar 18-Jan-2011 [4043] | 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. #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; } |
Maxim 18-Jan-2011 [4044x2] | welcome to REBOL Martin , C++ to REBOL is probably the biggest jump you can make, but I'm sure you'll see the light in the end ;-) If your feel like REBOL is breaking the rules (those which you are used to in other languages), in fact see it more like, there are much *less* rules in REBOL. although this freedom is dauting at first, I hope you'll come to appreciate it. |
I'll give you a convertion in a few secs... | |
Henrik 18-Jan-2011 [4046] | welcome to REBOL :-) |
alemar 18-Jan-2011 [4047x4] | wow thanks alot maxim that would really help me get my act together it`s just that basic flow control and inputs are my weacknesses in this langueage |
oh and oops the code does: 1+2^2+3^2+...+n^2 | |
yeah my bad :D | |
thanks for the worm welcome | |
Maxim 18-Jan-2011 [4051] | yea.. hehe I was wondering ;-) |
alemar 18-Jan-2011 [4052] | well copying from linux does that to you |
Maxim 18-Jan-2011 [4053] | in the above you use cin ... do you expect the input to be piped ? |
alemar 18-Jan-2011 [4054x3] | piped? |
basically c input | |
it inputs a vallue for a varriable | |
Maxim 18-Jan-2011 [4057] | ok. |
alemar 18-Jan-2011 [4058] | this >> is input and << is output |
Maxim 18-Jan-2011 [4059x2] | yeah.. I don't use the C++ console ops a lot, I do C mainly. |
but here goes... n: to-integer ask "enter a number" if n < 0 [quit/return 1] sum: 0 i: 0 while [ i <= n ] [ sum: sum + probe (i * i) i: i + 1] print sum | |
alemar 18-Jan-2011 [4061] | basically when you have cin>>n it awaits a vallue to be inputted in the console window |
Maxim 18-Jan-2011 [4062] | rebol returns 0 by default when you are the end of a script... so no need to add it at the end. |
alemar 18-Jan-2011 [4063x4] | aha |
ok so i just dont understand 2 lines of the code | |
sum:0 | |
i:0 | |
Maxim 18-Jan-2011 [4067] | note, I added a little probe which prints the value at each step. I did this to show you one powerfull aspect of REBOL... you can insert functions like *chains* |
alemar 18-Jan-2011 [4068] | wow |
Maxim 18-Jan-2011 [4069] | the probe actually is transparent and returns the value it received while printing it. |
Pekr 18-Jan-2011 [4070] | alemar, in rebol, the assignment is done via :, not = |
alemar 18-Jan-2011 [4071x2] | aaa like delphi |
i get it | |
Pekr 18-Jan-2011 [4073] | hmm, delphi uses :=, IIRC? But I might be confused by CA Visual OBjects I used in the past :-) |
Maxim 18-Jan-2011 [4074] | just a few functions don't return values (like print) in this case you will get an error, but the error should give you clues |
alemar 18-Jan-2011 [4075x2] | well in delphi you define a value by := and : only explaines the type (boolean,integer.. |
aha gr8 | |
Henrik 18-Jan-2011 [4077] | that's one powerful aspect of REBOL: it figures out the type on its own, mostly. |
alemar 18-Jan-2011 [4078x4] | trying to compile the code now but the compiler from the site is a bit tricky |
** Script error: n has no value ** Where: while ** Near: while [i <= n] [sum: sum + probe (i * i) i: i + 1] | |
:D | |
ok is the Near error a syntax error in the cycle? | |
Maxim 18-Jan-2011 [4082] | the main difference in REBOL is that everything you can manipulate is a value, even functions and objects don't have a special syntax to use... they are values just like anything else. |
Henrik 18-Jan-2011 [4083] | there is no compiler... |
Maxim 18-Jan-2011 [4084] | hehehe... if you paste it directly in the console, the ask will intepret the next line as the answer ;-) |
alemar 18-Jan-2011 [4085] | then what am i using :D |
Pekr 18-Jan-2011 [4086] | :-) |
Maxim 18-Jan-2011 [4087] | so it triggers an error and n is never assigned any value ;-) |
alemar 18-Jan-2011 [4088] | yeah exctly i understood that |
Pekr 18-Jan-2011 [4089] | alemar - rebol is an interpreter ... |
Maxim 18-Jan-2011 [4090] | for the console, just replace the first line by n: 5 |
older newer | first last |