1234567891011121314151617181920212223242526272829303132333435363738 |
- \ -1: true
- \ 0: false
- \ Read further for more info.
- \ Conditionals can only be used inside colon definitions.
- : abs ( n1 -- +n2 )
- dup 0 < if negate endif ;
- \ `if` looks at the topmost cell on the stack. If it
- \ contains a non-zero value, it is interpreted as truthy. If
- \ it is zero, it is interpreted as falsy. Usually -1 is used
- \ for true.
- \ `endif` is equivalent to `then`. The tutorial gives the
- \ following definition for endif:
- ( : endif postpone then ; immediate )
- : abs ( n1 -- +n2 )
- dup 0 < if negate then ;
- \ There is also an if-else-then:
- : min ( n1 n2 -- n )
- 2dup < if drop else nip then ;
- 2 3 min .
- 3 2 min .
- \ Exercise: min without using an else branch.
- : min ( n1 n2 -- n )
- \ Only let the `if` decide whether or not to swap the
- \ arguments.
- 2dup > if swap then
- drop ;
|