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


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

Re: Decoding Roman numerals

Started by"WJ" <w_a_x_man@yahoo.com>
First post2015-10-09 06:09 +0000
Last post2015-10-09 08:05 -0400
Articles 4 — 2 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Decoding Roman numerals "WJ" <w_a_x_man@yahoo.com> - 2015-10-09 06:09 +0000
    Re: Decoding Roman numerals "WJ" <w_a_x_man@yahoo.com> - 2015-10-09 06:19 +0000
    Re: Decoding Roman numerals Doug Hoffman <glidedog@gmail.com> - 2015-10-09 07:36 -0400
      Re: Decoding Roman numerals Doug Hoffman <glidedog@gmail.com> - 2015-10-09 08:05 -0400

#42478 — Re: Decoding Roman numerals

From"WJ" <w_a_x_man@yahoo.com>
Date2015-10-09 06:09 +0000
SubjectRe: Decoding Roman numerals
Message-ID<mv7lls$luf$1@dont-email.me>
WJ wrote:

> Oforth:
> 
> func: char-val {| i |
>   toUpper "IVXLCDM" indexOf --  -> i
>   5 i 2 rem pow  10 i 2 / pow  * }
> 
> func: sign(n, prev) { n prev - ++ sgn  n * }
> 
> func: think( n, prev, total ) { prev n sign   total +   n prev max }
> 
> func: decode {
>   reverse map( #char-val )
>   0 0 rot apply( #think )
>   drop }

Updated for OFORTH version V0.9.21.

: char-val {| i |
  toUpper "IVXLCDM" indexOf 1 -  -> i
  5 i 2 mod pow  10 i 2 / pow  * }

: sign(n, prev) { n prev - 1 +  sgn  n * }

: think( n, prev, total ) { prev n sign   total +   n prev max }

: decode {
  reverse map( #char-val )
  0 0 rot apply( #think )
  drop }

[toc] | [next] | [standalone]


#42480

From"WJ" <w_a_x_man@yahoo.com>
Date2015-10-09 06:19 +0000
Message-ID<mv7m98$njt$1@dont-email.me>
In reply to#42478
WJ wrote:

> : char-val {| i |
>   toUpper "IVXLCDM" indexOf 1 -  -> i
>   5 i 2 mod pow  10 i 2 / pow  * }

Another way:

: char-val {| i |
  toUpper "IVXLCDM" indexOf 1 -  -> i
  5 i isOdd pow  10 i 2 / pow  * }

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


#42490

FromDoug Hoffman <glidedog@gmail.com>
Date2015-10-09 07:36 -0400
Message-ID<5617a6ad$0$291$14726298@news.sunsite.dk>
In reply to#42478
On 10/9/15 2:09 AM, WJ wrote:

> Updated for OFORTH version V0.9.21.
>
> : char-val {| i |
>    toUpper "IVXLCDM" indexOf 1 -  -> i
>    5 i 2 mod pow  10 i 2 / pow  * }
>
> : sign(n, prev) { n prev - 1 +  sgn  n * }
>
> : think( n, prev, total ) { prev n sign   total +   n prev max }
>
> : decode {
>    reverse map( #char-val )
>    0 0 rot apply( #think )
>    drop }

Franck Bensusan has removed the need for matching { ... }
(just end the definition with ;) and has made . polymorphic
so it can be used instead of print.  Also, commas are not
required in the locals input list.  All of these changes will
make Oforth's syntax more familiar to a classic Forther and
need fewer keystrokes.  I added the missing stack comment
for decode:

: char-val | i |
   toUpper "IVXLCDM" indexOf 1 -  -> i
   5 i 2 mod pow  10 i 2 / pow  * ;

: sign( n prev ) n prev - 1 +  sgn  n * ;

: think( n prev total ) prev n sign   total +   n prev max ;

: decode // str --
   reverse map( #char-val )
   0 0 rot apply( #think )
   drop ;

"mcmxxciv" decode .
// => 1984 ok

-Doug

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


#42491

FromDoug Hoffman <glidedog@gmail.com>
Date2015-10-09 08:05 -0400
Message-ID<5617ad71$0$291$14726298@news.sunsite.dk>
In reply to#42490
On 10/9/15 7:36 AM, Doug Hoffman wrote:

Should be:
> : decode // str -- n

[toc] | [prev] | [standalone]


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


csiph-web