|
| 1 | +# Introduction |
| 2 | + |
| 3 | +This exercise introduces conditionals — choosing between two or more |
| 4 | +courses of action based on a value. It builds on the booleans you met |
| 5 | +in [Annalyn's Infiltration][annalyns] and the integer arithmetic from |
| 6 | +[Currency Conversion][currency-conversion]. |
| 7 | + |
| 8 | +## Comparison words |
| 9 | + |
| 10 | +These all live in [`math`][math] (and `kernel` for `=`): |
| 11 | + |
| 12 | +``` |
| 13 | += ( x y -- ? ) ! equal |
| 14 | +< ( x y -- ? ) ! less than |
| 15 | +<= ( x y -- ? ) ! less than or equal |
| 16 | +> ( x y -- ? ) ! greater than |
| 17 | +>= ( x y -- ? ) ! greater than or equal |
| 18 | +``` |
| 19 | + |
| 20 | +```factor |
| 21 | +3 3 = . ! => t |
| 22 | +2 3 < . ! => t |
| 23 | +3 3 <= . ! => t |
| 24 | +``` |
| 25 | + |
| 26 | +## `if`, `when`, `unless` |
| 27 | + |
| 28 | +`if` (in [`kernel`][kernel]) takes a boolean and two quotations. It |
| 29 | +runs the first quotation when the boolean is truthy and the second |
| 30 | +when it is falsy: |
| 31 | + |
| 32 | +``` |
| 33 | +if ( ? then-quot else-quot -- ) |
| 34 | +``` |
| 35 | + |
| 36 | +```factor |
| 37 | +: abs ( x -- y ) dup 0 < [ neg ] [ ] if ; |
| 38 | +``` |
| 39 | + |
| 40 | +`when` runs its quotation only when the boolean is truthy; `unless` |
| 41 | +only when it is falsy: |
| 42 | + |
| 43 | +``` |
| 44 | +when ( ? quot -- ) |
| 45 | +unless ( ? quot -- ) |
| 46 | +``` |
| 47 | + |
| 48 | +## `cond` |
| 49 | + |
| 50 | +When you have several alternative actions to choose between, `cond` |
| 51 | +(in [`combinators`][combinators]) is the natural fit. It takes an |
| 52 | +array of `{ predicate body }` pairs and runs the body of the first |
| 53 | +predicate that yields a truthy value: |
| 54 | + |
| 55 | +```factor |
| 56 | +USING: combinators ; |
| 57 | +
|
| 58 | +: classify ( n -- label ) |
| 59 | + { |
| 60 | + { [ dup 0 < ] [ drop "negative" ] } |
| 61 | + { [ dup 0 = ] [ drop "zero" ] } |
| 62 | + [ drop "positive" ] |
| 63 | + } cond ; |
| 64 | +``` |
| 65 | + |
| 66 | +A few details worth noting: |
| 67 | + |
| 68 | +- The pairs are tried in order. The first match wins. |
| 69 | +- An entry without a predicate (just a single quotation) at the end |
| 70 | + acts as the default. |
| 71 | +- Each predicate inspects the input but should leave the data stack |
| 72 | + the way it found it — `dup ... <test>` is the usual idiom. |
| 73 | +- The body of the chosen pair receives the same stack the predicate |
| 74 | + saw, so it usually starts by `drop`ping the input and pushing the |
| 75 | + result. |
| 76 | + |
| 77 | +[annalyns]: https://exercism.org/tracks/factor/exercises/annalyns-infiltration |
| 78 | +[currency-conversion]: https://exercism.org/tracks/factor/exercises/currency-conversion |
| 79 | +[math]: https://docs.factorcode.org/content/vocab-math.html |
| 80 | +[kernel]: https://docs.factorcode.org/content/vocab-kernel.html |
| 81 | +[combinators]: https://docs.factorcode.org/content/vocab-combinators.html |
0 commit comments