Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion concepts/conditionals/about.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# About

Factor's conditionals all take quotations as their branches.
Factor's conditionals all take **quotations** as their branches — code
written in square brackets, `[ ... ]`, that is pushed as a value rather
than run until the conditional selects it. (Quotations themselves are
covered fully in a later exercise.)

The basic three are in `kernel`:

Expand Down
6 changes: 6 additions & 0 deletions concepts/conditionals/introduction.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Introduction

Factor's conditionals choose between **quotations** — snippets of code
written in square brackets, `[ ... ]`. Writing `[ ... ]` pushes the
code onto the stack as a value rather than running it; the conditional
then decides which quotation to run. (Quotations are covered fully in a
later exercise.)

`if` (in [`kernel`][kernel]) is Factor's basic conditional. It
takes a boolean and two quotations, running the first if the
boolean is truthy and the second otherwise.
Expand Down
5 changes: 3 additions & 2 deletions exercises/concept/backyard-birdwatcher/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ A literal array uses `{ }` with whitespace between elements:
{ "a" "b" "c" } . ! => { "a" "b" "c" }
```

Arrays are read-only — words like `suffix` and `remove-nth` return
a *new* sequence rather than mutating the original.
Arrays are fixed-length, so words like `suffix` and `remove-nth` —
which change how many elements there are — return a *new* sequence
rather than growing the original.

## Length and indexing

Expand Down
16 changes: 16 additions & 0 deletions exercises/concept/cars-assemble/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ between? ( x lo hi -- ? ) ! lo <= x <= hi (inclusive)
You'll often see it as a `cond` predicate — `dup 1 4 between?` —
to pick a branch by range rather than by a single value.

## Quotations

The comparison words above produce booleans; to *act* on a boolean you
hand the conditional one or more **quotations**. A quotation is a
snippet of code wrapped in square brackets, `[ ... ]`. Writing it
pushes the code onto the stack as a value instead of running it — a
word like `if` then decides which quotation to run.

```factor
[ neg ] ! a quotation that negates the top of the stack
[ ] ! the empty quotation — does nothing
```

A later exercise covers quotations in full; for now, read
`[ ... ]` as "the code to run for this branch."

## `if`, `when`, `unless`

`if` (in [`kernel`][kernel]) takes a boolean and two quotations. It
Expand Down
Loading