"0= if" or simply "if" ??? - Programming Languages

This is a discussion on "0= if" or simply "if" ??? - Programming Languages ; I'm back at doing tutorial examples. I wrote these 2 words: : ?metric ( ? -- ? ) 10 mod if ." not metric..." else ." multiple of ten!" then cr ; : ?metric2 ( ? -- ? ) 10 ...

+ Reply to Thread
Page 1 of 11 1 2 3 ... LastLast
Results 1 to 10 of 101

"0= if" or simply "if" ???

  1. Default "0= if" or simply "if" ???

    I'm back at doing tutorial examples. I wrote these 2 words:

    : ?metric ( ? -- ? )
    10 mod
    if ." not metric..."
    else ." multiple of ten!"
    then
    cr ;

    : ?metric2 ( ? -- ? )
    10 mod 0=
    if ." multiple of ten!"
    else ." not metric..."
    then
    cr ;

    Which is the better form? Which is least expensive? Personally I find
    the latter more readable and self-explanatory.

    Also, what should the "stack effect" include? Should it reflect *just*
    user input, or user input and the "10" above? On the return side, should
    the conditional flag be taken into consideration? TIA...
    --
    duke | A: Yes. |
    | >Q: Are you sure? |
    | >>A: Because it reverses the logical flow of conversation. |
    | >>>Q: Why is top posting frowned upon? |

  2. Default Re: "0= if" or simply "if" ???

    Duke Normandin wrote:[color=blue]
    > I'm back at doing tutorial examples. I wrote these 2 words:
    >
    > : ?metric ( ? -- ? )
    > 10 mod
    > if ." not metric..."
    > else ." multiple of ten!"
    > then
    > cr ;
    >
    > : ?metric2 ( ? -- ? )
    > 10 mod 0=
    > if ." multiple of ten!"
    > else ." not metric..."
    > then
    > cr ;
    >
    > Which is the better form? Which is least expensive? Personally I find
    > the latter more readable and self-explanatory.[/color]

    It depends on what you're doing. Usually you're much more interested in
    one case or the other, so that should be the 'true' side even if it
    takes a 0= to do it. It's also poor style to have an empty IF clause
    and something happening in the ELSE; better to do 0= and omit the ELSE.

    Personally, I like to see the test in the same 'phrase' as the IF, and
    the consequences indented below:

    : ?metric ( ? -- ? )
    10 mod if ." not metric..."
    else ." multiple of ten!"
    then cr ;

    or

    : ?metric ( ? -- ? )
    10 mod 0= if
    ." multiple of ten!"
    else ." not metric..."
    then cr ;
    [color=blue]
    > Also, what should the "stack effect" include? Should it reflect *just*
    > user input, or user input and the "10" above? On the return side, should
    > the conditional flag be taken into consideration? TIA...[/color]

    The stack effect is intended to show what the word expects and leaves on
    the data stack. Since the 10 is supplied internally, that isn't an
    expectation of the word itself. And these definitions don't leave
    anything at all on the data stack. So, I'd show it as ( n -- ).

    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
    [url]http://www.forth.com[/url]

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

  3. Default Re: "0= if" or simply "if" ???

    On Mar 22, 2:42 pm, Duke Normandin <merr...@telus.net> wrote:
    ....[color=blue]
    > Which is the better form? Which is least expensive? Personally I find
    > the latter more readable and self-explanatory.[/color]

    Of course you should use:

    10 mod 0if
    ." multiple of ten!"
    else
    ." not metric..."
    then
    cr ;

    which is why Reva has "0if". But that is all a digression. You
    should, at this point, not worry too much about "expense" but rather
    be concerned with readability and maintainability.

    Generally, the more words the more code is generated (that is not
    always true, but it's a pretty good rule-of-thumb. But again, just
    aim for code you can understand now and that you'll be able to
    understand in a month or six.



  4. Default Re: "0= if" or simply "if" ???

    On 2007-03-22, Ron Aaron <rambamist@gmail.com> wrote:[color=blue]
    > On Mar 22, 2:42 pm, Duke Normandin <merr...@telus.net> wrote:
    > ...[color=green]
    >> Which is the better form? Which is least expensive? Personally I find
    >> the latter more readable and self-explanatory.[/color]
    >
    > Of course you should use:
    >
    > 10 mod 0if
    > ." multiple of ten!"
    > else
    > ." not metric..."
    > then
    > cr ;
    >
    > which is why Reva has "0if". But that is all a digression. You
    > should, at this point, not worry too much about "expense" but rather
    > be concerned with readability and maintainability.
    >
    > Generally, the more words the more code is generated (that is not
    > always true, but it's a pretty good rule-of-thumb. But again, just
    > aim for code you can understand now and that you'll be able to
    > understand in a month or six.
    >
    >[/color]

    Nice trick, Ron! ) ...as if I'm not confused enough as it is

    Seriously (Elizabeth and Ron) I'm currently knee-deep in Brodie's
    "Starting Forth" at:

    "...the fact that an arithmetic zero is identical in meaning to a "false" flag
    leads to some interesting results."

    That was what my first example was supposed to show.

    and

    "...All comparison operators return well-formed flags"

    Would my ?metric2 be an example of using a "well-formed" flag? If it is,
    then IMHO that's the way to go, because "well-formed" == "easily
    groked". Thanks!
    --
    duke | A: Yes. |
    | >Q: Are you sure? |
    | >>A: Because it reverses the logical flow of conversation. |
    | >>>Q: Why is top posting frowned upon? |

  5. Default Re: "0= if" or simply "if" ???

    Duke Normandin wrote:[color=blue]
    > I'm back at doing tutorial examples. I wrote these 2 words:
    >
    > : ?metric ( ? -- ? )
    > 10 mod
    > if ." not metric..."
    > else ." multiple of ten!"
    > then
    > cr ;
    >
    > : ?metric2 ( ? -- ? )
    > 10 mod 0=
    > if ." multiple of ten!"
    > else ." not metric..."
    > then
    > cr ;
    >
    > Which is the better form? Which is least expensive? Personally I find
    > the latter more readable and self-explanatory.
    >
    > Also, what should the "stack effect" include? Should it reflect *just*
    > user input, or user input and the "10" above? On the return side, should
    > the conditional flag be taken into consideration? TIA...[/color]

    I think it's a beginner hangup to write "if x .ne. 0" in BASIC, C, or
    Fortran instead of "if x". They mean the same thing and should be seen
    that way. Same for Forth.

    Jerry
    --
    Engineering is the art of making what you want from things you can get.
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

  6. Default Re: "0= if" or simply "if" ???

    Duke Normandin wrote:
    ....[color=blue]
    > Seriously (Elizabeth and Ron) I'm currently knee-deep in Brodie's
    > "Starting Forth" at:
    >
    > "...the fact that an arithmetic zero is identical in meaning to a "false" flag
    > leads to some interesting results."
    >
    > That was what my first example was supposed to show.
    >
    > and
    >
    > "...All comparison operators return well-formed flags"
    >
    > Would my ?metric2 be an example of using a "well-formed" flag? If it is,
    > then IMHO that's the way to go, because "well-formed" == "easily
    > groked". Thanks![/color]

    I agree with Jerry. You need to imprint on your brain that IF (and also
    WHILE and UNTIL, when you get to them) care only about zero=false and
    non-zero=true. Don't clutter your code with meaningless tests.

    The reason the comparison operators return well-formed flags is so you
    can use the Boolean combination operators AND OR XOR with them.

    : test ( n -- ) dup 100 < swap 10 mod 0= and if
    ." Yay!"
    else ." Boo!"
    then ;

    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
    [url]http://www.forth.com[/url]

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

  7. Default Re: "0= if" or simply "if" ???

    Duke Normandin wrote:[color=blue]
    > On 2007-03-22, Ron Aaron <rambamist@gmail.com> wrote:[color=green]
    >> On Mar 22, 2:42 pm, Duke Normandin <merr...@telus.net> wrote:
    >> ...[color=darkred]
    >>> Which is the better form? Which is least expensive? Personally I find
    >>> the latter more readable and self-explanatory.[/color]
    >> Of course you should use:
    >>
    >> 10 mod 0if
    >> ." multiple of ten!"
    >> else
    >> ." not metric..."
    >> then
    >> cr ;
    >>
    >> which is why Reva has "0if". But that is all a digression. You
    >> should, at this point, not worry too much about "expense" but rather
    >> be concerned with readability and maintainability.
    >>
    >> Generally, the more words the more code is generated (that is not
    >> always true, but it's a pretty good rule-of-thumb. But again, just
    >> aim for code you can understand now and that you'll be able to
    >> understand in a month or six.
    >>
    >>[/color]
    >
    > Nice trick, Ron! ) ...as if I'm not confused enough as it is
    >
    > Seriously (Elizabeth and Ron) I'm currently knee-deep in Brodie's
    > "Starting Forth" at:
    >
    > "...the fact that an arithmetic zero is identical in meaning to a "false" flag
    > leads to some interesting results."
    >
    > That was what my first example was supposed to show.
    >
    > and
    >
    > "...All comparison operators return well-formed flags"
    >
    > Would my ?metric2 be an example of using a "well-formed" flag? If it is,
    > then IMHO that's the way to go, because "well-formed" == "easily
    > groked". Thanks![/color]

    A well formed flag (WFF for short) in ANS Forth is either 0 or -1. Those
    are the values *produced* by tests, and they allow what some think is
    abominable and I think is neat and simple. Flags *consumed* by IF (or
    UNTIL or ...) needn't be well formed. All that means is that any
    non-zero value may stand in for -1 without altering the outcome.

    Jerry

    P.S. Suppose I want a word that leaves positive numbers unchanged and
    sets negative numbers to zero.

    : rectifier ( n -- n|0 ) DUP 0> AND ; / abominable, or neat?
    --
    Engineering is the art of making what you want from things you can get.
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

  8. Default Re: "0= if" or simply "if" ???

    Jerry Avins escreveu:
    [snipped]>[color=blue]
    > I think it's a beginner hangup to write "if x .ne. 0" in BASIC, C, or
    > Fortran instead of "if x". They mean the same thing and should be
    > seen that way. Same for Forth.
    >[/color]
    I think is a good practice that begginers are forced to abandon due
    'experts' calling it "hangup" until they need to code in a professional
    environment and have to 're-learn' to write code that is more readable
    (== maintainable).

  9. Default Re: "0= if" or simply "if" ???

    Cesar Rabak wrote:[color=blue]
    > Jerry Avins escreveu:
    > [snipped]>[color=green]
    >> I think it's a beginner hangup to write "if x .ne. 0" in BASIC, C, or
    >> Fortran instead of "if x". They mean the same thing and should be
    >> seen that way. Same for Forth.
    >>[/color]
    > I think is a good practice that begginers are forced to abandon due
    > 'experts' calling it "hangup" until they need to code in a professional
    > environment and have to 're-learn' to write code that is more readable
    > (== maintainable).[/color]

    Obviously we differ. To me it is easier to maintain "If X" than "If X
    exists" simply because it clearly means the same thing and uses fewer
    words. When I need to understand code you write your way, I will suffer
    through it in silence.

    Jerry
    --
    Engineering is the art of making what you want from things you can get.
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

  10. Default Re: "0= if" or simply "if" ???

    On 2007-03-23, Elizabeth D Rather <eratherXXX@forth.com> wrote:[color=blue]
    > Duke Normandin wrote:
    > ...[color=green]
    >> Seriously (Elizabeth and Ron) I'm currently knee-deep in Brodie's
    >> "Starting Forth" at:
    >>
    >> "...the fact that an arithmetic zero is identical in meaning to a "false" flag
    >> leads to some interesting results."
    >>
    >> That was what my first example was supposed to show.
    >>
    >> and
    >>
    >> "...All comparison operators return well-formed flags"
    >>
    >> Would my ?metric2 be an example of using a "well-formed" flag? If it is,
    >> then IMHO that's the way to go, because "well-formed" == "easily
    >> groked". Thanks![/color]
    >
    > I agree with Jerry. You need to imprint on your brain that IF (and also
    > WHILE and UNTIL, when you get to them) care only about zero=false and
    > non-zero=true. Don't clutter your code with meaningless tests.[/color]

    I'm just doing the examples and exercises in Brodie's book. Is there a
    better tutorial that I could use IYO?
    [color=blue]
    > The reason the comparison operators return well-formed flags is so you
    > can use the Boolean combination operators AND OR XOR with them.
    >
    >: test ( n -- ) dup 100 < swap 10 mod 0= and if
    > ." Yay!"
    > else ." Boo!"
    > then ;[/color]

    OK!! So am I to conclude that generally it's NOT a good idea to use
    arithmetic zero results (eg mod) to perform double duty as flags? Or am
    I totally confused?
    --
    duke | A: Yes. |
    | >Q: Are you sure? |
    | >>A: Because it reverses the logical flow of conversation. |
    | >>>Q: Why is top posting frowned upon? |

+ Reply to Thread
Page 1 of 11 1 2 3 ... LastLast

Similar Threads

  1. email-display: "N", "D", " ": yeah; but "O"? How to MARK "O"?
    By Application Development in forum Mutt
    Replies: 0
    Last Post: 10-30-2007, 07:13 AM
  2. Replies: 3
    Last Post: 06-28-2007, 01:35 AM
  3. How to simply set up "random" lights etc. upon "Leaving" home (X10)
    By Application Development in forum Home Automation
    Replies: 5
    Last Post: 06-06-2007, 05:21 AM
  4. Replies: 0
    Last Post: 03-21-2007, 01:26 PM
  5. """""""""""""""""""""Visual C++ 2005 Express"""""""""""""""""
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 03-12-2006, 03:55 AM