Help me Obi REB Kenobi! You're my only hope!
[1/12] from: edanaii:cox at: 11-May-2002 16:00
In an effort to understand this new (and very interesting) language, I
decided to try something simple; I created a simple guessing game where
the computer picks a number between 1 and 100.
Simply yes? Yea, right! :)
For whatevcr reason: newbie, stupidity, or that my computer just plain
hates me, I can't get the silly thing to work.
Here is the script:
REBOL [
Title: "Guess a number between 1 and 100"
Date: 05/11/2002
Version: 0
Author: "Ed Dana"
Purpose: "An excersize in REBOLion."
Comment: "Just goofing off."
Category: [game]
]
pick: random/seed 100.0
guess: ask {What is your guess? }
while [pick <> guess] [
if [pick < guess] [ print "To low" ]
if [pick > guess] [ print "To high" ]
guess: input
]
print pick
And the results I get:
>> do %guess.r
What is your guess? ** Script Error: trim expected series argument of
type: series port
** Where: ask
** Near: trim either hide [input/hide] [input]
What is it talking about? I haven't a clue.
I've even tried using Guess: Input and it defines Guess as 1.
Any advice is appreciated, including types on suicide so that I may hide
my shame from such a simple newbie mistake. :)
--
Sincerely, |
Ed Dana | Reserve your right to think, for even to think
Software Developer | wrongly is better than not to think at all.
1Ghz Athlon Amiga | -- Hypatia of Alexandria.
[2/12] from: cybarite:sympatico:ca at: 11-May-2002 21:02
You should not re-define "pick" before using ask.
pick is reserved.
Change it to my-pick or some other value.
See notes about protecting yourself from this problems.
[3/12] from: carl::cybercraft::co::nz at: 12-May-2002 13:17
On 12-May-02, Ed Dana wrote:
> In an effort to understand this new (and very interesting) language,
> I decided to try something simple; I created a simple guessing game
<<quoted lines omitted: 30>>
> Any advice is appreciated, including types on suicide so that I may
> hide my shame from such a simple newbie mistake. :)
Hi Ed,
A few REBOL specific mistakes there...
pick: random/seed 100.0
'pick shouldn't be used there, as it's a common REBOL word and I think
may have been the cause of your error message. Anyway, changing it
to 'target prevented the error happening for me. ie...
target: random/seed 100.0
(Adding 'protect-system to your user.r script will prevent this kind
of problem by not allowing you to over-write the default REBOL
words.)
Another problem is with the 'ifs...
if [target < guess] [ print "To low" ]
The "target < guess" shouldn't be in a block there, as 'if doesn't
evaluate the block like 'while does. So, this is the correct syntax
for 'if...
if target < guess [ print "To low" ]
but it still won't work in your case as you're comparing a number
(target) with a string (guess). So, to fix that use...
if target < to-integer guess [ print "To low" ]
Which should almost get your script working for you. (I say almost as
I'm leaving what you really should be ashamed of for you to find out
for yourself;)
HTH.
--
Carl Read
[4/12] from: al:bri:xtra at: 12-May-2002 12:10
Ed write:
> pick: random/seed 100.0
>> help pick
USAGE:
PICK series index
DESCRIPTION:
Returns the value at the specified position in a series.
PICK is an action value.
ARGUMENTS:
series -- (Type: series pair event money date time object port tuple
any-function)
index -- (Type: number logic)
You're giving Rebol's 'pick a new value.
Instead, try:
random/seed now
Choice: random 100
and substitute 'Choice instead of 'Pick in your script.
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
-><- Rebolutionary
[5/12] from: joel:neely:fedex at: 11-May-2002 22:26
Hi, Ed,
You've already received several good suggestions.
Ed Dana wrote:
> pick: random/seed 100.0
> guess: ask {What is your guess? }
<<quoted lines omitted: 4>>
> ]
> print pick
Let me add just a few more suggestions:
- Until you're used to the REBOL built-in vocabulary, I suggest
that you make liberal use of HELP while deciding what to call
your own words. If you had typed
help pick
and
help guess
you'd have gotten the hint that PICK was already taken, but
that GUESS was available for you to use without conflict.
- You don't need to re-seed the pseudo-random-number function
on every use. Changing to a "safe" word SECRET for the
hidden number, you can just say
secret: random 100
- Since ASK and INPUT return strings, go ahead and convert to an
integer before setting GUESS, as in
guess: to-integer ask {What is your guess? }
and
guess: to-integer input
- A purist would point out that inside the body of WHILE you
have already established that GUESS and SECRET differ; instead
of double-comparing, you can simplify to:
while [guess <> secret] [
either guess < secret [
print "Too low"
][
print "Too high"
]
guess: to-integer input
]
- With that simplification in place, you can now factor out the
PRINT and the first part of the string, as in:
while [guess <> secret] [
print [
"Too"
either guess < secret ["low" "high"]
]
guess: to-integer input
]
- Since logic! values can be used by PICK (the predefined one!)
to obtain values from a block, you can further shorten to:
while [guess <> secret] [
print ["Too" pick ["low" "high"] guess < secret]
guess: to-integer input
]
- You can further tighten up by not setting GUESS in multiple
places in the code. Just PRIN (output without newline) the
first-time prompt above the loop, then let the loop handle
the input of all guesses. This brings us down to:
secret: random 100
prin {What is your guess? }
while [secret <> guess: to-integer input] [
print ["Too" pick ["low" "high"] guess < secret]
]
print "You got it!"
- Several of these hints fall under the heading of DRY (Don't
Repeat Yourself). Whenever you find the same expression
occurring multiple times (or even similar expressions), you
might want to look for a way to eliminate the redundancy.
REBOL offers many ways to do so.
Hope this helps!
-jn-
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]
[6/12] from: edanaii:cox at: 12-May-2002 6:42
Joel Neely wrote:
>Hi, Ed,
>
>You've already received several good suggestions.
>
Yes, I have.
Thank you all for your good advice.
I bow my head in shame; I shall not bother this list again.
...
At least, not until the next time I screw up. :)
--
Sincerely, |
Ed Dana | This is the worst kind of discrimination! The
Software Developer | kind against me!
1Ghz Athlon Amiga | -- Bender, Futurama.
[7/12] from: edanaii:cox at: 12-May-2002 9:02
Ed Dana wrote:
> Joel Neely wrote:
>> Hi, Ed,
<<quoted lines omitted: 6>>
> ...
> At least, not until the next time I screw up. :)
OK, OK, I lied...
A couple quick follow-up questions, for my edification. :)
Why can literals be surrounded by "" or {}. Is there a difference in how
REBOL executes these strings?
Just curious about the reason for this.
--
Sincerely, |
Ed Dana | Nearly all men can stand adversity, but if you
Software Developer | want to test a man's character, give him power.
1Ghz Athlon Amiga | -- Abraham Lincoln
[8/12] from: joel:neely:fedex at: 12-May-2002 14:09
Hi, Ed,
Ed Dana wrote:
> I bow my head in shame; I shall not bother this list again.
>
But please do! It's how most of us learned REBOL to begin
with.
> At least, not until the next time I screw up. :)
>
That happens with me about every 30 milliseconds! See you soon!
;-)
-jn-
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]
[9/12] from: joel:neely:fedex at: 12-May-2002 14:28
Hi, again, Ed,
Ed Dana wrote:
> A couple quick follow-up questions, for my edification. :)
>
> Why can literals be surrounded by "" or {}. Is there a difference
> in how REBOL executes these strings?
>
> Just curious about the reason for this.
>
It's very handy to have multiple string delimiters, in case you
want to have strings that include delimiters themselves. For
example:
make-img: func [
src [string!]
/dim xy [pair!]
][
append
append <img> rejoin [{ src="} src {"}]
either dim [
rejoin [{ width="} xy/x {" height="} xy/y {"}]
][
""
]
]
which behaves as:
>> make-img "teeny.gif"
== <img src="teeny.gif">
>> make-img/dim "teeny.gif" 3x5
== <img src="teeny.gif" src="teeny.gif" width="3" height="5">
Niceties aside, the curly brackets are normally used for "longer"
strings and the quotation mark used for "shorter" strings.
>> glop: "123"
== "123"
>> append glop glop
== "123123"
>> append glop glop
== "123123123123"
>> append glop glop
== "123123123123123123123123"
>> append glop glop
== "123123123123123123123123123123123123123123123123"
>> append glop glop
== {12312312312312312312312312312312312312312312312312
3123123123123123123123123123123123123123123123}
But that's just a convention.
More importantly, the curly brackets can be used to create multi-
line strings easily:
>> {this
{ is
{ a
{ very
{ long
{ string
{ }
== {this
is
a
very
long
string
}
while quotation-mark-delimited strings must be on a single line:
>> "this
** Syntax Error: Missing " at "this
** Near: pick ["high" "low"] guess > secret
HTH!
-jn-
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]
[10/12] from: g:santilli:tiscalinet:it at: 12-May-2002 22:29
Hi Ed,
On Sunday, May 12, 2002, 6:02:18 PM, you wrote:
ED> Why can literals be surrounded by "" or {}. Is there a difference in how
ED> REBOL executes these strings?
{You can write
multiline strings
this way}
while this needs to end in one line
{also, you can put a " inside here}
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[11/12] from: greggirwin:mindspring at: 12-May-2002 21:01
Hey Ed!
Keep asking questions. I always learn something, whether it's me or someone
else asking the questions. :)
--Gregg
[12/12] from: ammon:rcslv at: 12-May-2002 11:59
Hi,
Just another note, "protect system" (with out the quotes) will stop you from
renaming any REBOL words. I give this hint, but recomend that you go ahead
run help on the words that you find are REBOL words. (BTW go ahead & run
help on 'protect)
HTH
Ammon
A short time ago, Ed Dana, sent an email stating:
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted