# Re: Starting Forth, starting out.

"Colin MacIntyre" <colin@xxxxxxxxxxxxxxxx> wrote in message news:43ba8be6\$0\$15195\$6d36acad@xxxxxxxxxxxxxxxxxxxxx
```Hi, I'm pretty new to programming, and I figured I'd start out in Forth.
Working through Starting Forth, I've encountered this problem:```

```Write the definition for the word WITHIN which expects three arguments:
( n lo-limit hi-limit -- )
and leaves a "true" flag only if "n" is within the range
low-limit <= n < hi-limit
I don't have the original book, but am working from the online version, so
the answer it gives me isn't in words I understand (yet), such as R> <U or
whatever. Below is what I've figured out, but I'm wondering if it can be
made more efficient? By the way, math was my weakest subject, so even this
starter's tutorial has taxed my brain to bleeding. Fun though.```

```: AWITHIN ( n lo hi -- )
ROT DUP ROT SWAP > ROT ROT < AND
IF ." This number fits."
ELSE ." Doesn't fit." THEN ;
```
`Here's a much simpler version:`

```: within ( n lo hi - t )   -rot over    \ hi n lo n
>= -rot                                     \ t hi n
> and ;                                      \ t```

Your definition has unnecessarily complex stack handling. Also, when a spec says "leave a flag" it means a truth value on the stack, which is more useful in code than displayed strings.

-rot is equivalent to rot rot, and >= is equivalent to < 0= but both words are available in most Forths, usually implemented in code (and hence much cheaper than their high-level equivalents).

```On another subject, I'm using Win32Forth. My goal is to one day create a
fullscreen roguelike in Windows. Is this a good implementation to use? One
thing I've noticed is it already has words that the book has asked me to
define, like 3DUP and even WITHIN.
```

As to which implementation to use, I encourage you to try several and make your own decision based on ease of use, quality of documentation, and other issues that matter to you. In addition to Win32Forth there are free evaluation versions of commercial Forths, including my company's SwiftForth (www.forth.com) and MPE's VFX.

My company also offers some other books for sale, including "Forth Application Techniques" which is a more modern tutorial than Starting Forth. It also asks you to define some words which already exist; the reason is that these are good practice in stack management, and it's sometimes easier to define a word whose behavior you already understand.

```Cheers,
Elizabeth```

```--
==================================================
Elizabeth D. Rather   (US & Canada)   800-55-FORTH
FORTH Inc.                         +1 310-491-3356
5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
Hawthorne, CA 90250
http://www.forth.com```

"Forth-based products and Services for real-time
applications since 1973."
==================================================

`.`