Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #19116 > unrolled thread

Definition of 'DIGIT'

Started byjzakiya@gmail.com
First post2013-01-24 22:49 -0800
Last post2013-01-25 16:47 +0000
Articles 18 — 10 participants

Back to article view | Back to comp.lang.forth


Contents

  Definition of 'DIGIT' jzakiya@gmail.com - 2013-01-24 22:49 -0800
    Re: Definition of 'DIGIT' Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-01-25 03:39 -0600
      Re: Definition of 'DIGIT' Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2013-01-26 14:53 +0000
        Re: Definition of 'DIGIT' Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-01-26 13:18 -0600
    Re: Definition of 'DIGIT' Paul Rubin <no.email@nospam.invalid> - 2013-01-25 01:47 -0800
      Re: Definition of 'DIGIT' jzakiya@gmail.com - 2013-01-25 06:39 -0800
        Re: Definition of 'DIGIT' Bernd Paysan <bernd.paysan@gmx.de> - 2013-01-25 17:05 +0100
          Re: Definition of 'DIGIT' jzakiya@gmail.com - 2013-01-25 10:43 -0800
            Re: Definition of 'DIGIT' jzakiya@gmail.com - 2013-01-25 11:01 -0800
              Re: Definition of 'DIGIT' jzakiya@gmail.com - 2013-01-26 08:09 -0800
                Re: Definition of 'DIGIT' "A. K." <akk@nospam.org> - 2013-01-26 17:55 +0100
                Re: Definition of 'DIGIT' Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-01-26 13:21 -0600
                Re: Definition of 'DIGIT' Mark Wills <markrobertwills@yahoo.co.uk> - 2013-01-27 01:00 -0800
                  Re: Definition of 'DIGIT' Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-01-27 03:39 -0600
      Re: Definition of 'DIGIT' Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-01-25 09:05 -0600
      Re: Definition of 'DIGIT' albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-01-25 15:39 +0000
        Re: Definition of 'DIGIT' Coos Haak <chforth@hccnet.nl> - 2013-01-26 16:43 +0100
    Re: Definition of 'DIGIT' stephenXXX@mpeforth.com (Stephen Pelc) - 2013-01-25 16:47 +0000

#19116 — Definition of 'DIGIT'

Fromjzakiya@gmail.com
Date2013-01-24 22:49 -0800
SubjectDefinition of 'DIGIT'
Message-ID<1538d130-3c98-4e9d-bf09-d88c41dfa623@googlegroups.com>
Can someone provide me the high-level definition of the word DIGIT.
I need to create this for a forth that doesn't have it.

: DIGIT ( c b -- n ? | ?) ;

It takes an ascii character 'c' and returns its numerical value and true 
flag if it's a digit for the base system 'b', or a false flag otherwise.

Example:

: num ( c -- n ? | ?) KEY 10 DIGIT ;

[toc] | [next] | [standalone]


#19129

FromAndrew Haley <andrew29@littlepinkcloud.invalid>
Date2013-01-25 03:39 -0600
Message-ID<0emdnQQhQc5Mz5_MnZ2dnUVZ_q6dnZ2d@supernews.com>
In reply to#19116
jzakiya@gmail.com wrote:
> Can someone provide me the high-level definition of the word DIGIT.
> I need to create this for a forth that doesn't have it.
> 
> : DIGIT ( c b -- n ? | ?) ;
> 
> It takes an ascii character 'c' and returns its numerical value and true 
> flag if it's a digit for the base system 'b', or a false flag otherwise.
> 
> Example:
> 
> : num ( c -- n ? | ?) KEY 10 DIGIT ;

That's a really ugly stack effect, and hard to do nicely.

: digit ( c b -- n ? | ?)
   swap [char] 0 - dup 9 > if 
      [ char 0 char a - 10 + ] literal + then
   tuck > dup 0= if  nip  then ;

Andrew.

[toc] | [prev] | [next] | [standalone]


#19167

FromGerry Jackson <gerry@jackson9000.fsnet.co.uk>
Date2013-01-26 14:53 +0000
Message-ID<ke0qki$pv1$1@dont-email.me>
In reply to#19129
On 25/01/2013 09:39, Andrew Haley wrote:
> jzakiya@gmail.com wrote:
>> Can someone provide me the high-level definition of the word DIGIT.
>> I need to create this for a forth that doesn't have it.
>>
>> : DIGIT ( c b -- n ? | ?) ;
>>
>> It takes an ascii character 'c' and returns its numerical value and true
>> flag if it's a digit for the base system 'b', or a false flag otherwise.
>>
>> Example:
>>
>> : num ( c -- n ? | ?) KEY 10 DIGIT ;
>
> That's a really ugly stack effect, and hard to do nicely.
>
> : digit ( c b -- n ? | ?)
>     swap [char] 0 - dup 9 > if
>        [ char 0 char a - 10 + ] literal + then
>     tuck > dup 0= if  nip  then ;
>
> Andrew.
>

Surely you're not serious, that definition is seriously deficient. Using 
a base of 10 (decimal) your definition returns TRUE with a negative 
number for:
   - all characters from 0 to '/'
   - characters from ':' to 'V'
and TRUE with a positive number for 'W' to '`'

A definition that works for all characters, including upper and lower 
case for alphabetic characters, up to a base of 36 and with no IFs is

: digit  ( ch base -- n true | false )
    swap 1- dup bl > and bl or   \ A-Z -> a-z, CTRL -> BL
    [char] / - dup 9 u>
    [ char a char 0 - 10 - ] literal and -
    tuck u> ?dup and
;

If control characters are impossible omit the 'dup bl > and'

(ASCII characters & two's complement of course)

-- 
Gerry

[toc] | [prev] | [next] | [standalone]


#19174

FromAndrew Haley <andrew29@littlepinkcloud.invalid>
Date2013-01-26 13:18 -0600
Message-ID<jLKdnUAMgZBztpnMnZ2dnUVZ_s-dnZ2d@supernews.com>
In reply to#19167
Gerry Jackson <gerry@jackson9000.fsnet.co.uk> wrote:
> On 25/01/2013 09:39, Andrew Haley wrote:
>> jzakiya@gmail.com wrote:
>>> Can someone provide me the high-level definition of the word DIGIT.
>>> I need to create this for a forth that doesn't have it.
>>>
>>> : DIGIT ( c b -- n ? | ?) ;
>>>
>>> It takes an ascii character 'c' and returns its numerical value and true
>>> flag if it's a digit for the base system 'b', or a false flag otherwise.
>>>
>>> Example:
>>>
>>> : num ( c -- n ? | ?) KEY 10 DIGIT ;
>>
>> That's a really ugly stack effect, and hard to do nicely.
>>
>> : digit ( c b -- n ? | ?)
>>     swap [char] 0 - dup 9 > if
>>        [ char 0 char a - 10 + ] literal + then
>>     tuck > dup 0= if  nip  then ;
> 
> Surely you're not serious, that definition is seriously deficient. Using 
> a base of 10 (decimal) your definition returns TRUE with a negative 
> number for:
>   - all characters from 0 to '/'
>   - characters from ':' to 'V'
> and TRUE with a positive number for 'W' to '`'

It was left as an exercise for the reader.  Well done!  ;-)

Andrew.

[toc] | [prev] | [next] | [standalone]


#19130

FromPaul Rubin <no.email@nospam.invalid>
Date2013-01-25 01:47 -0800
Message-ID<7xfw1p4msq.fsf@ruckus.brouhaha.com>
In reply to#19116
jzakiya@gmail.com writes:
> : DIGIT ( c b -- n ? | ?) ;

Per "redefine the problem" I think a preferable stack effect is:

  : DIGIT ( c b -- n flag ) ;

when flag is false, n can be zero.

[toc] | [prev] | [next] | [standalone]


#19131

Fromjzakiya@gmail.com
Date2013-01-25 06:39 -0800
Message-ID<40d1fd1a-331c-40d9-9a9a-bebe59c03593@googlegroups.com>
In reply to#19130
On Friday, January 25, 2013 4:47:01 AM UTC-5, Paul Rubin wrote:
> jzakiya@gmail.com writes:
> 
> > : DIGIT ( c b -- n ? | ?) ;
> 
> 
> 
> Per "redefine the problem" I think a preferable stack effect is:
> 
> 
> 
>   : DIGIT ( c b -- n flag ) ;
> 
> 
> 
> when flag is false, n can be zero.

DIGIT as shown, is in Win32Forth, SwiftForth, and VFX.

If 'c' is not a digit of the base system 'b' it returns a 'false' flag,
if it's a digit from '0..b-1' it returns the appropriate digit and true flag.
So, if b=16 it returns the numerical value of digits '0..9a..f'.

I need to reproduce this behavior to make my life easier in transporting
code to systems that don't have DIGIT, eg. gforth.

Thanks.

[toc] | [prev] | [next] | [standalone]


#19139

FromBernd Paysan <bernd.paysan@gmx.de>
Date2013-01-25 17:05 +0100
Message-ID<2651804.ZOrIU1bWaL@sunwukong.fritz.box>
In reply to#19131
jzakiya@gmail.com wrote:
> I need to reproduce this behavior to make my life easier in
> transporting code to systems that don't have DIGIT, eg. gforth.

Gforth has something similar, called DIGIT? ( char -- n t | f ).  It 
assumes that BASE contains the base.

-- 
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://bernd-paysan.de/

[toc] | [prev] | [next] | [standalone]


#19145

Fromjzakiya@gmail.com
Date2013-01-25 10:43 -0800
Message-ID<c708e488-c146-4136-a5b0-97c6016e3217@googlegroups.com>
In reply to#19139
On Friday, January 25, 2013 11:05:35 AM UTC-5, Bernd Paysan wrote:
> jzakiya@gmail.com wrote:
> 
> > I need to reproduce this behavior to make my life easier in
> 
> > transporting code to systems that don't have DIGIT, eg. gforth.
> 
> 
> 
> Gforth has something similar, called DIGIT? ( char -- n t | f ).  It 
> 
> assumes that BASE contains the base.
> 
> 
> 
> -- 
> 
> Bernd Paysan
> 
> "If you want it done right, you have to do it yourself"
> 
> http://bernd-paysan.de/

This is the code for DIGIT? in gforth:

see digit? 
: digit?  
  toupper 48 - dup 9 u> 
  IF     7 - dup 9 u<= 
         IF     drop false EXIT 
         THEN 
  THEN 
  dup useraddr <56>  @ u>= 
  IF     drop false EXIT 
  THEN 
  true ; ok

where 'useraddr <56>' is the variable 'base'
and 'touppper' changes lowercase chars 'a..z' into values for 'A..Z'
or returns the original character otherwise.

Here is a simple highlevel definition of 'toupper'

: toupper ( c - c|C) dup [char] a [char] z 1+ within if 32 - exit then ;

So, to get to what I want to do in gforth I can do:

: digit ( c b -- n t | f
  >r toupper 48 - dup 9 u> 
  IF     7 - dup 9 u<= 
         IF  drop r> drop false EXIT
         THEN 
  THEN 
  dup r> u>= 
  IF     drop false EXIT 
  THEN 
  true ;

But 'u<=' and 'u>=' though common aren't ANS Forth, so I can do this:

: digit ( c -- n t | f
  >r toupper 48 - dup 9 u> 
  IF     7 - dup 10 u< 
         IF  drop r> drop false EXIT
         THEN 
  THEN 
  dup r> 1- u> 
  IF     drop false EXIT 
  THEN 
  true ;

[toc] | [prev] | [next] | [standalone]


#19148

Fromjzakiya@gmail.com
Date2013-01-25 11:01 -0800
Message-ID<034521fc-1f11-4c55-8c32-cc5e92592e05@googlegroups.com>
In reply to#19145
On Friday, January 25, 2013 1:43:17 PM UTC-5, jza...@gmail.com wrote:
> On Friday, January 25, 2013 11:05:35 AM UTC-5, Bernd Paysan wrote:
> 
> > jzakiya@gmail.com wrote:
> 
> > 
> 
> > > I need to reproduce this behavior to make my life easier in
> 
> > 
> 
> > > transporting code to systems that don't have DIGIT, eg. gforth.
> 
> > 
> 
> > 
> 
> > 
> 
> > Gforth has something similar, called DIGIT? ( char -- n t | f ).  It 
> 
> > 
> 
> > assumes that BASE contains the base.
> 
> > 
> 
> > 
> 
> > 
> 
> > -- 
> 
> > 
> 
> > Bernd Paysan
> 
> > 
> 
> > "If you want it done right, you have to do it yourself"
> 
> > 
> 
> > http://bernd-paysan.de/
> 
> 
> 
> This is the code for DIGIT? in gforth:
> 
> 
> 
> see digit? 
> 
> : digit?  
> 
>   toupper 48 - dup 9 u> 
> 
>   IF     7 - dup 9 u<= 
> 
>          IF     drop false EXIT 
> 
>          THEN 
> 
>   THEN 
> 
>   dup useraddr <56>  @ u>= 
> 
>   IF     drop false EXIT 
> 
>   THEN 
> 
>   true ; ok
> 
> 
> 
> where 'useraddr <56>' is the variable 'base'
> 
> and 'touppper' changes lowercase chars 'a..z' into values for 'A..Z'
> 
> or returns the original character otherwise.
> 
> 
> 
> Here is a simple highlevel definition of 'toupper'
> 
> 
> 
> : toupper ( c - c|C) dup [char] a [char] z 1+ within if 32 - exit then ;
> 
> 
> 
> So, to get to what I want to do in gforth I can do:
> 
> 
> 
> : digit ( c b -- n t | f
> 
>   >r toupper 48 - dup 9 u> 
> 
>   IF     7 - dup 9 u<= 
> 
>          IF  drop r> drop false EXIT
> 
>          THEN 
> 
>   THEN 
> 
>   dup r> u>= 
> 
>   IF     drop false EXIT 
> 
>   THEN 
> 
>   true ;
> 
> 
> 
> But 'u<=' and 'u>=' though common aren't ANS Forth, so I can do this:
> 
> 
> 
> : digit ( c -- n t | f
> 
>   >r toupper 48 - dup 9 u> 
> 
>   IF     7 - dup 10 u< 
> 
>          IF  drop r> drop false EXIT
> 
>          THEN 
> 
>   THEN 
> 
>   dup r> 1- u> 
> 
>   IF     drop false EXIT 
> 
>   THEN 
> 
>   true ;

This may be a little more clear and concise:

: digit ( c -- n t | f
  >r toupper [char] 0 - dup 9 u> 
  IF     7 - dup #10 u< 
         IF  r> 2drop false exit THEN
  THEN 
  dup r> 1- u> 
  IF  drop false exit THEN
  true ;

[toc] | [prev] | [next] | [standalone]


#19170

Fromjzakiya@gmail.com
Date2013-01-26 08:09 -0800
Message-ID<1aea234c-ae29-46a5-9847-f6e97bee3aee@googlegroups.com>
In reply to#19148
On Friday, January 25, 2013 2:01:31 PM UTC-5, jza...@gmail.com wrote:
> On Friday, January 25, 2013 1:43:17 PM UTC-5, jza...@gmail.com wrote:
> 
> > On Friday, January 25, 2013 11:05:35 AM UTC-5, Bernd Paysan wrote:
> 
> > 
> 
> > > jzakiya@gmail.com wrote:
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > I need to reproduce this behavior to make my life easier in
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > transporting code to systems that don't have DIGIT, eg. gforth.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > Gforth has something similar, called DIGIT? ( char -- n t | f ).  It 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > assumes that BASE contains the base.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > -- 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > Bernd Paysan
> 
> > 
> 
> > > 
> 
> > 
> 
> > > "If you want it done right, you have to do it yourself"
> 
> > 
> 
> > > 
> 
> > 
> 
> > > http://bernd-paysan.de/
> 
> > 
> 
> > 
> 
> > 
> 
> > This is the code for DIGIT? in gforth:
> 
> > 
> 
> > 
> 
> > 
> 
> > see digit? 
> 
> > 
> 
> > : digit?  
> 
> > 
> 
> >   toupper 48 - dup 9 u> 
> 
> > 
> 
> >   IF     7 - dup 9 u<= 
> 
> > 
> 
> >          IF     drop false EXIT 
> 
> > 
> 
> >          THEN 
> 
> > 
> 
> >   THEN 
> 
> > 
> 
> >   dup useraddr <56>  @ u>= 
> 
> > 
> 
> >   IF     drop false EXIT 
> 
> > 
> 
> >   THEN 
> 
> > 
> 
> >   true ; ok
> 
> > 
> 
> > 
> 
> > 
> 
> > where 'useraddr <56>' is the variable 'base'
> 
> > 
> 
> > and 'touppper' changes lowercase chars 'a..z' into values for 'A..Z'
> 
> > 
> 
> > or returns the original character otherwise.
> 
> > 
> 
> > 
> 
> > 
> 
> > Here is a simple highlevel definition of 'toupper'
> 
> > 
> 
> > 
> 
> > 
> 
> > : toupper ( c - c|C) dup [char] a [char] z 1+ within if 32 - exit then ;
> 
> > 
> 
> > 
> 
> > 
> 
> > So, to get to what I want to do in gforth I can do:
> 
> > 
> 
> > 
> 
> > 
> 
> > : digit ( c b -- n t | f
> 
> > 
> 
> >   >r toupper 48 - dup 9 u> 
> 
> > 
> 
> >   IF     7 - dup 9 u<= 
> 
> > 
> 
> >          IF  drop r> drop false EXIT
> 
> > 
> 
> >          THEN 
> 
> > 
> 
> >   THEN 
> 
> > 
> 
> >   dup r> u>= 
> 
> > 
> 
> >   IF     drop false EXIT 
> 
> > 
> 
> >   THEN 
> 
> > 
> 
> >   true ;
> 
> > 
> 
> > 
> 
> > 
> 
> > But 'u<=' and 'u>=' though common aren't ANS Forth, so I can do this:
> 
> > 
> 
> > 
> 
> > 
> 
> > : digit ( c -- n t | f
> 
> > 
> 
> >   >r toupper 48 - dup 9 u> 
> 
> > 
> 
> >   IF     7 - dup 10 u< 
> 
> > 
> 
> >          IF  drop r> drop false EXIT
> 
> > 
> 
> >          THEN 
> 
> > 
> 
> >   THEN 
> 
> > 
> 
> >   dup r> 1- u> 
> 
> > 
> 
> >   IF     drop false EXIT 
> 
> > 
> 
> >   THEN 
> 
> > 
> 
> >   true ;
> 
> 
> 
> This may be a little more clear and concise:
> 
> 
> 
> : digit ( c -- n t | f
> 
>   >r toupper [char] 0 - dup 9 u> 
> 
>   IF     7 - dup #10 u< 
> 
>          IF  r> 2drop false exit THEN
> 
>   THEN 
> 
>   dup r> 1- u> 
> 
>   IF  drop false exit THEN
> 
>   true ;

An observation on DIGIT, et al.

Though VFX, SwiftForth, and Win32Forth have the word DIGIT, it performs differently on each system.

For VFX (which I was developing code in) the behavior is this:

: DIGIT ( c base - n t|f) .. ;

For Gforth, DIGIT? provides this behavior.

For Win32Forth the behavior is like this:

: DIGIT ( c base - n t| c f) .. ; 

and only works with uppercase character 'A..Z' but not lowercase ones.

For SwiftForth the behavior is totally different. It appears to take a value off the stack and return the character digit for it within some range.

So, since there seems to be no common practice regarding DIGIT, or the behavior I was looking for, I decided to just eliminate using it in the code I was developing, which lead me to produce a more direct and simple solution to what I really wanted to do in the first place.

However, the exercise was good (and revealing) in exposing the plight of trying to write code that is supposedly ANS Forth compliant ***AND*** transportable across various Forths.

This exercise also highlighted me the deficiency of the Forth community in  not developing an Open Source Forth project, that accumulates and implements all the best practices into one version that people (new users, not just developers) can be confident WILL WORK in most environments (32/64-bit, multi-core/threads, various video cards, etc).

But again, this is just an observation, and a wish.

We really do need to get past the truth of the refrain:

"If you've seen on forth, you've seen one forth."

[toc] | [prev] | [next] | [standalone]


#19171

From"A. K." <akk@nospam.org>
Date2013-01-26 17:55 +0100
Message-ID<51040a9a$0$6576$9b4e6d93@newsspool3.arcor-online.net>
In reply to#19170
On 26.01.2013 17:09, jzakiya@gmail.com wrote:

> However, the exercise was good (and revealing) in exposing the plight of trying to write code that is supposedly ANS Forth compliant ***AND*** transportable across various Forths.
>
> This exercise also highlighted me the deficiency of the Forth community in  not developing an Open Source Forth project, that accumulates and implements all the best practices into one version that people (new users, not just developers) can be confident WILL WORK in most environments (32/64-bit, multi-core/threads, various video cards, etc).
>
> But again, this is just an observation, and a wish.
>
> We really do need to get past the truth of the refrain:
>
> "If you've seen on forth, you've seen one forth."
>


Okay, then
a) make one, or propose one reference Forth,
b) put it online,
c) then ask for a consensus here.  ;-)

Seriously, perhaps the easier way would be for you to participate in the 
Forth 20xx standardization endeavour....

[toc] | [prev] | [next] | [standalone]


#19175

FromAndrew Haley <andrew29@littlepinkcloud.invalid>
Date2013-01-26 13:21 -0600
Message-ID<jLKdnUMMgZAjsZnMnZ2dnUVZ_s-dnZ2d@supernews.com>
In reply to#19170
jzakiya@gmail.com wrote:

> So, since there seems to be no common practice regarding DIGIT, or
> the behavior I was looking for, I decided to just eliminate using it
> in the code I was developing, which lead me to produce a more direct
> and simple solution to what I really wanted to do in the first
> place.
> 
> However, the exercise was good (and revealing) in exposing the
> plight of trying to write code that is supposedly ANS Forth
> compliant ***AND*** transportable across various Forths.

How does that follow?  It's not a standard word.  It can be written in
Standard Forth.

Andrew.

[toc] | [prev] | [next] | [standalone]


#19187

FromMark Wills <markrobertwills@yahoo.co.uk>
Date2013-01-27 01:00 -0800
Message-ID<c4c03fca-dcec-4444-8edc-63cc2c8eaf4e@b11g2000yqh.googlegroups.com>
In reply to#19170
On Jan 26, 4:09 pm, jzak...@gmail.com wrote:
> On Friday, January 25, 2013 2:01:31 PM UTC-5, jza...@gmail.com wrote:
> > On Friday, January 25, 2013 1:43:17 PM UTC-5, jza...@gmail.com wrote:
>
> > > On Friday, January 25, 2013 11:05:35 AM UTC-5, Bernd Paysan wrote:
>
> > > > jzak...@gmail.com wrote:
>
> > > > > I need to reproduce this behavior to make my life easier in
>
> > > > > transporting code to systems that don't have DIGIT, eg. gforth.
>
> > > > Gforth has something similar, called DIGIT? ( char -- n t | f ).  It
>
> > > > assumes that BASE contains the base.
>
> > > > --
>
> > > > Bernd Paysan
>
> > > > "If you want it done right, you have to do it yourself"
>
> > > >http://bernd-paysan.de/
>
> > > This is the code for DIGIT? in gforth:
>
> > > see digit?
>
> > > : digit?
>
> > >   toupper 48 - dup 9 u>
>
> > >   IF     7 - dup 9 u<=
>
> > >          IF     drop false EXIT
>
> > >          THEN
>
> > >   THEN
>
> > >   dup useraddr <56>  @ u>=
>
> > >   IF     drop false EXIT
>
> > >   THEN
>
> > >   true ; ok
>
> > > where 'useraddr <56>' is the variable 'base'
>
> > > and 'touppper' changes lowercase chars 'a..z' into values for 'A..Z'
>
> > > or returns the original character otherwise.
>
> > > Here is a simple highlevel definition of 'toupper'
>
> > > : toupper ( c - c|C) dup [char] a [char] z 1+ within if 32 - exit then ;
>
> > > So, to get to what I want to do in gforth I can do:
>
> > > : digit ( c b -- n t | f
>
> > >   >r toupper 48 - dup 9 u>
>
> > >   IF     7 - dup 9 u<=
>
> > >          IF  drop r> drop false EXIT
>
> > >          THEN
>
> > >   THEN
>
> > >   dup r> u>=
>
> > >   IF     drop false EXIT
>
> > >   THEN
>
> > >   true ;
>
> > > But 'u<=' and 'u>=' though common aren't ANS Forth, so I can do this:
>
> > > : digit ( c -- n t | f
>
> > >   >r toupper 48 - dup 9 u>
>
> > >   IF     7 - dup 10 u<
>
> > >          IF  drop r> drop false EXIT
>
> > >          THEN
>
> > >   THEN
>
> > >   dup r> 1- u>
>
> > >   IF     drop false EXIT
>
> > >   THEN
>
> > >   true ;
>
> > This may be a little more clear and concise:
>
> > : digit ( c -- n t | f
>
> >   >r toupper [char] 0 - dup 9 u>
>
> >   IF     7 - dup #10 u<
>
> >          IF  r> 2drop false exit THEN
>
> >   THEN
>
> >   dup r> 1- u>
>
> >   IF  drop false exit THEN
>
> >   true ;
>
> An observation on DIGIT, et al.
>
> Though VFX, SwiftForth, and Win32Forth have the word DIGIT, it performs differently on each system.
>
> For VFX (which I was developing code in) the behavior is this:
>
> : DIGIT ( c base - n t|f) .. ;
>
> For Gforth, DIGIT? provides this behavior.
>
> For Win32Forth the behavior is like this:
>
> : DIGIT ( c base - n t| c f) .. ;
>
> and only works with uppercase character 'A..Z' but not lowercase ones.
>
> For SwiftForth the behavior is totally different. It appears to take a value off the stack and return the character digit for it within some range.
>
> So, since there seems to be no common practice regarding DIGIT, or the behavior I was looking for, I decided to just eliminate using it in the code I was developing, which lead me to produce a more direct and simple solution to what I really wanted to do in the first place.
>
> However, the exercise was good (and revealing) in exposing the plight of trying to write code that is supposedly ANS Forth compliant ***AND*** transportable across various Forths.
>
> This exercise also highlighted me the deficiency of the Forth community in  not developing an Open Source Forth project, that accumulates and implements all the best practices into one version that people (new users, not just developers) can be confident WILL WORK in most environments (32/64-bit, multi-core/threads, various video cards, etc).
>
> But again, this is just an observation, and a wish.
>
> We really do need to get past the truth of the refrain:
>
> "If you've seen on forth, you've seen one forth."

But DIGIT isn't a standard word, is it? Therefore, the portability
argument isn't really valid in the context of DIGIT.

It might be a good candidate for standardisation, if not already
standardised in the 2012 standard.

I would argue that it should be called >DIGIT ("to digit") which would
give an opportunity to give it a (slightly) different name, thus
avoiding collision with different implementations in different systems.

[toc] | [prev] | [next] | [standalone]


#19190

FromAndrew Haley <andrew29@littlepinkcloud.invalid>
Date2013-01-27 03:39 -0600
Message-ID<rpWdnZZ2HZ51aJnMnZ2dnUVZ_h2dnZ2d@supernews.com>
In reply to#19187
Mark Wills <markrobertwills@yahoo.co.uk> wrote:
> But DIGIT isn't a standard word, is it? Therefore, the portability
> argument isn't really valid in the context of DIGIT.
> 
> It might be a good candidate for standardisation, if not already
> standardised in the 2012 standard.
> 
> I would argue that it should be called >DIGIT ("to digit") which would
> give an opportunity to give it a (slightly) different name, thus
> avoiding collision with different implementations in different systems.

It's converting from a digit to a number; >DIGIT hardly seems to me to
be approriate.  DIGIT isn't a terrible name.  But it's not totally
clear to me what would be gained by standardizing it.

Andrew.

[toc] | [prev] | [next] | [standalone]


#19134

FromAndrew Haley <andrew29@littlepinkcloud.invalid>
Date2013-01-25 09:05 -0600
Message-ID<xNOdnfFMk4mkAp_MnZ2dnUVZ_h2dnZ2d@supernews.com>
In reply to#19130
Paul Rubin <no.email@nospam.invalid> wrote:
> jzakiya@gmail.com writes:
>> : DIGIT ( c b -- n ? | ?) ;
> 
> Per "redefine the problem" I think a preferable stack effect is:
> 
>  : DIGIT ( c b -- n flag ) ;
> 
> when flag is false, n can be zero.

All that does is move the problem to the caller.  A typical use of
DIGIT is

   begin   ...  c@  base @ digit while  ...

Andrew.

[toc] | [prev] | [next] | [standalone]


#19136

Fromalbert@spenarnc.xs4all.nl (Albert van der Horst)
Date2013-01-25 15:39 +0000
Message-ID<5102a724$0$6331$e4fe514c@dreader35.news.xs4all.nl>
In reply to#19130
In article <7xfw1p4msq.fsf@ruckus.brouhaha.com>,
Paul Rubin  <no.email@nospam.invalid> wrote:
>jzakiya@gmail.com writes:
>> : DIGIT ( c b -- n ? | ?) ;
>
>Per "redefine the problem" I think a preferable stack effect is:
>
>  : DIGIT ( c b -- n flag ) ;
>
>when flag is false, n can be zero.

Solution for the original

: DIGIT   >R  &0 -     0 R> WITHIN
  \ Now uglify the stack
  DUP 0= IF NIP THEN ;

Paul's better proposal would result in :

: DIGIT   >R  &0 -     0 R> WITHIN ;

Groetjes Albert
P.S.
- Some use `` R> U< '' instead of `` 0 R> WITHIN ''
- &0 must be replaced by CHAR 0 or [CHAR] 0 in portable code
but I never can remember which.

Groetjes Albert
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#19169

FromCoos Haak <chforth@hccnet.nl>
Date2013-01-26 16:43 +0100
Message-ID<9alaehh1rpvj$.19tncn69ls5di$.dlg@40tude.net>
In reply to#19136
Op 25 Jan 2013 15:39:16 GMT schreef Albert van der Horst:

> - &0 must be replaced by CHAR 0 or [CHAR] 0 in portable code
> but I never can remember which.

Compare [CHAR] to ['] and [COMPILE]
All three are immediate words. CHAR and ' are not, as the word COMPILE
wasn't too.

-- 
Coos

CHForth, 16 bit DOS applications
http://home.hccnet.nl/j.j.haak/forth.html 

[toc] | [prev] | [next] | [standalone]


#19143

FromstephenXXX@mpeforth.com (Stephen Pelc)
Date2013-01-25 16:47 +0000
Message-ID<5102b684.7243968@192.168.0.50>
In reply to#19116
On Thu, 24 Jan 2013 22:49:10 -0800 (PST), jzakiya@gmail.com wrote:

>Can someone provide me the high-level definition of the word DIGIT.
>I need to create this for a forth that doesn't have it.
>
>: DIGIT ( c b -- n ? | ?) ;

This should do what you want. The actual VFX Forth version is ugly
because it makes assumptions about the x86 code generator.

: DIGIT         \ char base -- 0 | n true
  swap upc [char] 0 - dup 0<	\ base val -- ; SFP045
  if  2drop 0 exit  then
  dup 9 > if			\ if > 9
    7 - dup #10 <		\ check gap
    if  2drop 0 exit  then
  then
  dup rot <			\ -- val flag
  dup 0= if  nip then
;

Stephen

-- 
Stephen Pelc, stephenXXX@mpeforth.com
MicroProcessor Engineering Ltd - More Real, Less Time
133 Hill Lane, Southampton SO15 5AF, England
tel: +44 (0)23 8063 1441, fax: +44 (0)23 8033 9691
web: http://www.mpeforth.com - free VFX Forth downloads

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web