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


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

Looking for some help with this gforth program. Thanks!

Started bymurphy.darin@gmail.com
First post2016-05-11 13:54 -0700
Last post2016-05-16 05:35 +0000
Articles 11 on this page of 91 — 18 participants

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


Contents

  Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-11 13:54 -0700
    Re: Looking for some help with this gforth program. Thanks! all2001@spambog.com (Wolfgang Allinger) - 2016-05-11 17:59 -0300
      Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-11 19:16 -0700
        Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-11 19:40 -0700
          Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-11 23:22 -0700
            Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-12 00:01 -0700
              Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-12 00:08 -0700
                Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-12 12:09 -0700
              Re: Looking for some help with this gforth program. Thanks! rickman <gnuarm@gmail.com> - 2016-05-12 20:48 -0400
                Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-12 18:04 -0700
                  Re: Looking for some help with this gforth program. Thanks! rickman <gnuarm@gmail.com> - 2016-05-12 21:21 -0400
                  Re: Looking for some help with this gforth program. Thanks! Cecil Bayona <cbayona@cbayona.com> - 2016-05-12 20:27 -0500
                    Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-12 18:46 -0700
                      Re: Looking for some help with this gforth program. Thanks! Cecil Bayona <cbayona@cbayona.com> - 2016-05-12 23:28 -0500
                        Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-13 00:52 -0700
                          Re: Looking for some help with this gforth program. Thanks! Pablo Hugo Reda <pabloreda@gmail.com> - 2016-05-13 04:07 -0700
                          Re: Looking for some help with this gforth program. Thanks! rickman <gnuarm@gmail.com> - 2016-05-14 00:42 -0400
                            Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-14 00:05 -0700
                              Re: Looking for some help with this gforth program. Thanks! rickman <gnuarm@gmail.com> - 2016-05-15 00:10 -0400
                                Re: Looking for some help with this gforth program. Thanks! anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-15 10:34 +0000
                                  Re: Looking for some help with this gforth program. Thanks! rickman <gnuarm@gmail.com> - 2016-05-15 09:43 -0400
                                    Re: Looking for some help with this gforth program. Thanks! anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-16 12:21 +0000
                                      Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-16 07:38 -0700
                                        Re: Looking for some help with this gforth program. Thanks! anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-16 14:56 +0000
                                      Re: Looking for some help with this gforth program. Thanks! rickman <gnuarm@gmail.com> - 2016-05-16 12:07 -0400
                                      Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-16 09:18 -0700
                                      Re: Looking for some help with this gforth program. Thanks! Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-05-16 13:19 -0500
                                        Re: Looking for some help with this gforth program. Thanks! anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-17 12:39 +0000
                                          Re: Looking for some help with this gforth program. Thanks! Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-05-17 10:34 -0500
                                            Re: Looking for some help with this gforth program. Thanks! anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-17 15:54 +0000
                                              Re: Looking for some help with this gforth program. Thanks! mhx@iae.nl - 2016-05-17 12:44 -0700
                                  Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-15 11:03 -0700
                                    Re: Looking for some help with this gforth program. Thanks! rickman <gnuarm@gmail.com> - 2016-05-15 21:56 -0400
                                      Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-15 19:02 -0700
                                        Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-15 20:03 -0700
                                        Re: Looking for some help with this gforth program. Thanks! rickman <gnuarm@gmail.com> - 2016-05-16 02:13 -0400
                                          Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-16 00:37 -0700
                                            Re: Looking for some help with this gforth program. Thanks! anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-16 13:41 +0000
                                              Re: Looking for some help with this gforth program. Thanks! Bernd Paysan <bernd.paysan@gmx.de> - 2016-05-16 21:42 +0000
                                                Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-16 16:23 -0700
                                                Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-16 16:41 -0700
                                                  Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-16 20:32 -0700
                                                  Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-16 20:47 -0700
                                                  Re: Looking for some help with this gforth program. Thanks! anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-17 06:32 +0000
                                                    Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-17 07:56 -0700
                                                  Re: Looking for some help with this gforth program. Thanks! Bernd Paysan <bernd.paysan@gmx.de> - 2016-05-17 23:05 +0000
                                                    Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-17 21:16 -0700
                                                      Re: Looking for some help with this gforth program. Thanks! Bernd Paysan <bernd.paysan@gmx.de> - 2016-06-04 22:44 +0000
                                                Re: Looking for some help with this gforth program. Thanks! anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-17 13:42 +0000
                                                  Re: Looking for some help with this gforth program. Thanks! Bernd Paysan <bernd.paysan@gmx.de> - 2016-05-17 22:31 +0000
                                                    Re: Looking for some help with this gforth program. Thanks! anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-18 16:43 +0000
                                        Re: Looking for some help with this gforth program. Thanks! Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-05-16 10:38 -0500
                                          Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-16 10:10 -0700
                                            Re: Looking for some help with this gforth program. Thanks! Cecil Bayona <cbayona@cbayona.com> - 2016-05-16 12:29 -0500
                                            Re: Looking for some help with this gforth program. Thanks! Cecil Bayona <cbayona@cbayona.com> - 2016-05-16 12:34 -0500
                                            Re: Looking for some help with this gforth program. Thanks! Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-05-16 13:27 -0500
                                              Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-17 16:11 -0700
                                                Re: Looking for some help with this gforth program. Thanks! Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-05-18 04:31 -0500
                                        Re: Looking for some help with this gforth program. Thanks! Albert van der Horst <albert@cherry.spenarnc.xs4all.nl> - 2016-05-16 18:46 +0000
                            Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-14 00:07 -0700
                              Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-14 00:20 -0700
                                Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-14 01:23 -0700
                                  Re: Looking for some help with this gforth program. Thanks! rickman <gnuarm@gmail.com> - 2016-05-15 00:13 -0400
                                    Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-14 21:55 -0700
    Re: Looking for some help with this gforth program. Thanks! Paul Rubin <no.email@nospam.invalid> - 2016-05-11 17:08 -0700
      Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-11 17:40 -0700
        Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-11 18:54 -0700
          Re: Looking for some help with this gforth program. Thanks! Doug Hoffman <glidedog@gmail.com> - 2016-05-13 06:55 -0400
            Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-13 11:07 -0700
            Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-13 20:42 -0700
              Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-13 20:57 -0700
              Re: Looking for some help with this gforth program. Thanks! Doug Hoffman <glidedog@gmail.com> - 2016-05-14 05:54 -0400
                Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-14 15:20 -0700
                  Re: Looking for some help with this gforth program. Thanks! Doug Hoffman <glidedog@gmail.com> - 2016-05-15 05:45 -0400
                    Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-15 11:21 -0700
                      Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-15 11:39 -0700
      Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-11 20:45 -0700
        Re: Looking for some help with this gforth program. Thanks! hughaguilar96@gmail.com - 2016-05-11 23:05 -0700
          Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-11 23:15 -0700
            Re: Looking for some help with this gforth program. Thanks! rickman <gnuarm@gmail.com> - 2016-05-12 22:34 -0400
        Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-11 23:11 -0700
    Re: Looking for some help with this gforth program. Thanks! november.nihal@gmail.com - 2016-05-11 19:00 -0700
      Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-11 19:14 -0700
    Re: Looking for some help with this gforth program. Thanks! humptydumpty <ouatubi@gmail.com> - 2016-05-12 01:48 -0700
      Re: Looking for some help with this gforth program. Thanks! Doug Hoffman <glidedog@gmail.com> - 2016-05-12 07:43 -0400
        Re: Looking for some help with this gforth program. Thanks! humptydumpty <ouatubi@gmail.com> - 2016-05-12 10:21 -0700
      Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-12 12:11 -0700
    Re: Looking for some help with this gforth program. Thanks! "Ala'a" <amalawi@gmail.com> - 2016-05-12 14:34 -0700
      Re: Looking for some help with this gforth program. Thanks! murphy.darin@gmail.com - 2016-05-12 16:20 -0700
    Re: Looking for some help with this gforth program. Thanks! Marcos Cruz <use_the_contact_page@programandala.net> - 2016-05-14 12:27 +0000
    Re: Looking for some help with this gforth program. Thanks! "WJ" <w_a_x_man@yahoo.com> - 2016-05-16 05:35 +0000

Page 5 of 5 — ← Prev page 1 2 3 4 [5]


#47033

Frommurphy.darin@gmail.com
Date2016-05-11 23:11 -0700
Message-ID<e07258dd-72d9-448f-975b-84db7ceeb49f@googlegroups.com>
In reply to#47031
Thanks hughag for showing me a better example of penny-exchange, so I can see how further the program can be factored.  Your's looks so much smaller and clearer than mine, though I am not up to some of the terms yet. I understand after looking at your version I understand more clearly that I have a lot more reading and experimenting to go, but since I am able to read most of it, I feel very confident that I will get there.  I'll be keeping a copy of yours with my original so I can look back at them as my understanding and experience increases.  Thanks again hughag, Paul, novembe and wolfgang for all of your helpful and useful examples, suggestions and advice.  I look forward to learning more from you all again soon! Take care now.

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


#47027

Fromnovember.nihal@gmail.com
Date2016-05-11 19:00 -0700
Message-ID<db6b481a-1e09-4f1f-899f-6d002508e41c@googlegroups.com>
In reply to#47022
Hi Murphy, 

dup is  ( x -- x x ) &
?dup is ( x -- 0 | x x )

I think you would be better of using just 'dup' ( & ignoring '?dup' for now ) until you are more comfortable about whats being left on the stack ?

Hope that helps

NN

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


#47028

Frommurphy.darin@gmail.com
Date2016-05-11 19:14 -0700
Message-ID<128cd597-ee3f-4292-a81d-b607c85ef7d2@googlegroups.com>
In reply to#47027
Thanks novembe for guidance I really appreciate it. Also I did correct the last bug with:

: pennies-exchange   ( n -- ) dup 1 < if ." ERROR - Must enter a number greater than 0!" drop else 10000-cent then ;

Anyway, I know I have a very, very long way to go to reach my goal, but thanks to everyone here willing to help me understand and look at things clearer, I know I'll get there!  Take care now and thanks again.

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


#47038

Fromhumptydumpty <ouatubi@gmail.com>
Date2016-05-12 01:48 -0700
Message-ID<5c9e0b11-ddf1-444b-bd37-308cad5d117b@googlegroups.com>
In reply to#47022
On Wednesday, May 11, 2016 at 11:54:08 PM UTC+3, murphy...@gmail.com wrote:
> Sorry if this isn't appropriate question on this forum, but I am unable to find one specifically for "gforth" questions, but I am trying to learn forth and gforth is the only free, and actively supported by the developer forth that runs under on my Google Pixel C tablet.  Anyway here's the program:
> 
> : penny   ( n -- ) dup 1 > if . ." pennies" else dup 0 > if . ." penny" then then ;
> : nickel   ( n -- n ) 5 /mod ?dup 0= if penny else . ." nickel" then ?dup 0 > if ." , " penny then ;
> : dime   ( n -- n ) 10 /mod ?dup 0= if nickel else dup 1 > if . ." dimes" else . ." dime" then then ?dup 0 > if ." , " nickel then ;
> : quarter ( n -- n ) 25 /mod ?dup 0= if dime else dup 1 > if . ." quarters" else . ." quarter" then then ?dup 0 > if ." , " dime then ;
> : dollar ( n -- n ) 100 /mod ?dup 0= if quarter else dup 1 > if . ." dollars" else . ." dollar" then then ?dup 0 > if ." , " quarter then ;
> : 5-dollar   ( n -- n ) 500 /mod ?dup 0= if dollar else dup 1 > if . ." fives" else . ." five" then then ?dup 0 > if ." , " dollar then ;
> : 10-dollar   ( n -- n ) 1000 /mod ?dup 0= if 5-dollar else dup 1 > if . ." tens" else . ." ten" then then ?dup 0 > if ." , " 5-dollar then ;
> : 20-dollar   ( n -- n ) 2000 /mod ?dup 0= if 10-dollar else dup 1 > if . ." twenties" else . ." twenty" then then ?dup 0 > if ." , " 10-dollar then ;
> : 50-dollar   ( n -- n ) 5000 /mod ?dup 0= if 20-dollar else dup 1 > if . ." fifties" else . ." fifty" then then ?dup 0 > if ." , " 20-dollar then ;
> : 100-dollar  ( n -- n ) 10000 /mod ?dup 0= if 50-dollar else dup 1 > if . ." hundreds" else . ." hundred" then then ?dup 0 > if ." , " 50-dollar then ;
> 
> : pennies-exchange   ( n -- ) ?dup 0= if ." ERROR - Must enter a number greater than 0!" else 100-dollar then ;
> 
> I've rewritten this program about a dozen times and have removed a elusive 0 that was being left on the stack, but now it sometimes works fine, while other times I get garbage and a "stack underflow" error.  I am also very new to programming in forth period, so I'm sure this program could be factored more efficiently, but I'm learning.  Anyway, if any of you would look at this program, maybee run it in and what I am doing wrong or any other advice on how to improve it, I would be very, very grateful.  I think forth is such an amazing and underrate language and really want to understand and utilitize it for all my programming needs.  Additionally I do understand the basics of programming (i.e., sequential, branching and iterative ), though I'm still very much a newbie in anything else. Thanks in advance for any assistance, opinions, advice given. Take care now.

Hi!

A possible way:

    10 CONSTANT #UNITS \ number of units
    CREATE Units 10000 , 5000 , 2000 , 1000 , 500 , 100 , 25 , 10 , 5 , 1 ,

\ Could have an array of strings, but for simplicity let's have an array of chars
\ Each unit name has a maximum 8 letters, so have to store a string 10 x 2 x 8 chars
    8 CONSTANT NAME#
\ Now carefully type a looong string :-)
    CREATE Names S" hundredshundred fifties fifty   twentiestwenty  tens    ten     fives   five    dollars dollar  quartersquarter dimes   dime    nickels nickel  pennies penny    " string,

\ After each definition perform tests
: []"       ( uindex -- a )
    NAME# * Names + 1+
;
: .[]"      ( uindex -- )
    []" NAME# -trailing type space
;
: .money    ( uindex nvalue -- )
    dup 1 < IF 2drop     exit THEN
    dup 1 = IF . 1+ .[]" exit THEN
    . .[]"
;
: .dollars  ( npennies -- )
    dup 1 < IF drop exit THEN
    #UNITS 0 DO
        I cells Units + @ /mod
        I 2 * ( each category of names ) swap
        .money
    LOOP drop
;

: final-test DO cr I .dollars LOOP cr .s ;
100 1 final-test



Have a nice day,
humptydumpty

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


#47041

FromDoug Hoffman <glidedog@gmail.com>
Date2016-05-12 07:43 -0400
Message-ID<57346c72$0$298$14726298@news.sunsite.dk>
In reply to#47038
On 5/12/16 4:48 AM, humptydumpty wrote:

> \ Now carefully type a looong string :-) CREATE Names S"
> hundredshundred fifties fifty   twentiestwenty  tens    ten     fives
> five    dollars dollar  quartersquarter dimes   dime    nickels
> nickel  pennies penny    " string,

No use for fifties, tens, fives, or nickels. Right?

Might be good to point out that string, is non-standard.

Nice solution.

-Doug

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


#47045

Fromhumptydumpty <ouatubi@gmail.com>
Date2016-05-12 10:21 -0700
Message-ID<fe78e55f-f74c-4872-babe-38ff5bf9cab2@googlegroups.com>
In reply to#47041
On Thursday, May 12, 2016 at 2:51:19 PM UTC+3, Doug Hoffman wrote:
> On 5/12/16 4:48 AM, humptydumpty wrote:
> 
> > \ Now carefully type a looong string :-) CREATE Names S"
> > hundredshundred fifties fifty   twentiestwenty  tens    ten     fives
> > five    dollars dollar  quartersquarter dimes   dime    nickels
> > nickel  pennies penny    " string,
>

Hi! 

> No use for fifties, tens, fives, or nickels. Right?
Yes!

> Might be good to point out that string, is non-standard.
A speedy hack, wrote everything on gforth command line, then
copied into an editor and made to look better.
 
> Nice solution.
Thanks!

> -Doug

Have a nice day,
humptydumpty

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


#47054

Frommurphy.darin@gmail.com
Date2016-05-12 12:11 -0700
Message-ID<4f9a80bb-b24d-495c-b13b-d8bc3b6fef9c@googlegroups.com>
In reply to#47038
Wow thanks humptydumpty for another factored version of "pennies-exchange". I'll be certain to add my collection, so I can learn further about different factoring schemes.

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


#47057

From"Ala'a" <amalawi@gmail.com>
Date2016-05-12 14:34 -0700
Message-ID<bc2ea236-00eb-4a15-9758-2152665669d5@googlegroups.com>
In reply to#47022
Here is a newbie trial, HIH:

: .words? ROT DUP IF . TYPE ELSE DROP 2DROP THEN ;
: .pennies S" penny(ies)" .words? ;
: >nickles 5 /MOD S" nickle(s) " .words? ;
: >quarters 25 /MOD S" quarter(s) " .words? ;
: >$1 100 /MOD S" dollar(s) " .words? ;
: >$5 500 /MOD S" five-dollar bill(s) " .words? ;
: >$10 1000 /MOD S" ten-dollar bill(s) " .words? ;
: >$20 2000 /MOD S" twenty-dollar bill(s) " .words? ;
: >$50 5000 /MOD S" fifty-dollar bill(s) " .words? ;
: >$100 10000 /MOD S" hundred-dollar bill(s) " .words? ;
: xchg >$100 >$50 >$20 >$10 >$5 >$1 >quarters >nickles .pennies ;


The above definitions makes it straightforward for me to define another exchange2 word that only offers $5 bills and $1 bills like:
: exchange2 >$5 >$1 .pennies ;

Notes:
- comma separation + single vs plural was skipped.

Regards,

Ala'a

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


#47062

Frommurphy.darin@gmail.com
Date2016-05-12 16:20 -0700
Message-ID<71977eb3-ef57-4552-8f82-71f50ed0553f@googlegroups.com>
In reply to#47057
Ala I tip my hat off to you! You make this forth newbie look even more newbie (if that's even possible. LOL). Thanks for your version, I'll add it to my collection and perhaps once I finish both "Starting Forth" and "Thinking Forth" my stuff will start looking less C, python, pascal and starting looking more forthish. Take care now.

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


#47120

FromMarcos Cruz <use_the_contact_page@programandala.net>
Date2016-05-14 12:27 +0000
Message-ID<nh75ji$134n$1@gioia.aioe.org>
In reply to#47022
En/Je/On 2016-05-11, murphy.darin@gmail.com <murphy.darin@gmail.com>
escribió/skribis/wrote:

> Sorry if this isn't appropriate question on this forum,

Of course your question is appropriate here.

> I am unable to find one specifically for "gforth" questions,

It's here:

http://lists.gnu.org/mailman/listinfo/gforth

You can consult its archives by months:

http://lists.gnu.org/archive/html/gforth/

And by threads:

http://www.mail-archive.com/gforth@gnu.org/

The home page of Gforth:

https://www.gnu.org/software/gforth/

-- 
Marcos Cruz
http://programandala.net

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


#47181

From"WJ" <w_a_x_man@yahoo.com>
Date2016-05-16 05:35 +0000
Message-ID<nhbm7n$pmc$1@dont-email.me>
In reply to#47022
murphy.darin@gmail.com wrote:

> : penny   ( n -- ) dup 1 > if . ." pennies"
> else dup 0 > if . ." penny" then then ;
> 
> : nickel   ( n -- n ) 5 /mod ?dup 0= if penny else . ."
> nickel" then ?dup 0 > if ." , " penny then ;
> 
> : dime   ( n -- n ) 10 /mod ?dup 0= if nickel else dup 1 > if
> . ." dimes" else . ." dime" then then ?dup 0 > if ." , "
> nickel then ;
> 
> : quarter ( n -- n ) 25 /mod ?dup 0= if dime else dup 1
> > if . ." quarters" else . ." quarter" then then ?dup 0
> > if ." , " dime then ;
> 
> : dollar ( n -- n ) 100 /mod ?dup 0= if quarter else
> dup 1 > if . ." dollars" else . ." dollar" then then
> ?dup 0 > if ." , " quarter then ;
> 
> : 5-dollar   ( n -- n ) 500 /mod ?dup 0= if dollar else
> dup 1 > if . ." fives" else . ." five" then then ?dup 0
> > if ." , " dollar then ;
> 
> : 10-dollar   ( n -- n ) 1000 /mod ?dup 0= if 5-dollar
> else dup 1 > if . ." tens" else . ." ten" then then
> ?dup 0 > if ." , " 5-dollar then ;
> 
> : 20-dollar   ( n -- n ) 2000 /mod ?dup 0= if 10-dollar
> else dup 1 > if . ." twenties" else . ." twenty" then
> then ?dup 0 > if ." , " 10-dollar then ;
> 
> : 50-dollar   ( n -- n ) 5000 /mod ?dup 0= if 20-dollar
> else dup 1 > if . ." fifties" else . ." fifty" then
> then ?dup 0 > if ." , " 20-dollar then ;
> 
> : 100-dollar  ( n -- n ) 10000 /mod ?dup 0= if
> 50-dollar else dup 1 > if . ." hundreds" else . ."
> hundred" then then ?dup 0 > if ." , " 50-dollar then ;
> 
> : pennies-exchange   ( n -- ) ?dup 0= if ." ERROR -
> Must enter a number greater than 0!" else 100-dollar
> then ;


OCaml:

#load "str.cma" ;;

let units =
 [(10_000, "hundred");
  (5_000, "fifty");
  (2_000, "twenty");
  (1_000, "ten");
  (500, "five");
  (100, "one");
  (25, "quarter");
  (10, "dime");
  (5, "nickel");
  (1, "penny")] ;;

let pluralize s = (Str.replace_first (Str.regexp "y$") "ie" s) ^ "s" ;;

let stringify n s =
  Printf.sprintf "%d %s" n (if n > 1 then pluralize s else s) ;;

let reduce cents n s result =
  let quo = cents / n in
  let rem = cents mod n in
  (rem, (stringify quo s) :: result) ;;

let process cents result =
  let (n, s) =
    List.find
      (fun (n, s) -> n <= cents)
      units
  in reduce cents n s result ;;

let exchange cents =
  let rec loop = function
      (0, result) -> String.concat ", " (List.rev result)
    | (cents, result) -> loop (process cents result)
  in loop (cents, []) ;;


# exchange 99_233;;
- : string =
"9 hundreds, 1 fifty, 2 twenties, 2 ones, 1 quarter, 1 nickel, 3 pennies"
# exchange 99;;
- : string = "3 quarters, 2 dimes, 4 pennies"
# exchange 505;;
- : string = "1 five, 1 nickel"


-- 
Goyim were born only to serve us....  They will work, they will plow, they will
reap. We will sit like an effendi and eat. --- Rabbi Ovadia Yosef
archive.org/download/DavidDuke_videos/TopRabbiExposesJewishRacism-cybsdwjezqi.ogv
web.archive.org/web/20101020044210/http://www.jpost.com/JewishWorld/JewishNews/Article.aspx?id=191782

[toc] | [prev] | [standalone]


Page 5 of 5 — ← Prev page 1 2 3 4 [5]

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


csiph-web