Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Help me Obi REB Kenobi! You're my only hope!

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? } > > while [pick <> guess] [ > if [pick < guess] [ print "To low" ] > if [pick > guess] [ print "To high" ] > guess: input > ] > > 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 ]