First Decision Rule

Write a publisher's house style rule and explain why 2 lines keep a heading on the page and 1 line moves it.

A publisher's house style decides where a heading near the bottom of a page goes. In this lesson you write that rule. A section heading stays when at least 2 lines are left below it, and a chapter heading always starts a new page. You check 2 lines, 1 line, and a chapter heading, then rewrite the rule to read JSON input.

Read the previous lesson, Data, Conditions, and Decisions, first. Run the examples in the Playground. For a terminal, install Native or npm from Downloads.

Write and run the rule

Save this complete program as house-style.lspx.

LISPEX
(define (heading-placement lines-left chapter-heading?)
  (if (and (>= lines-left 2) (not chapter-heading?))
      '(decision keep)
      '(decision next-page)))

(heading-placement 5 #f)

With Native or npm installed, run it from the directory containing the file.

SH
lispex house-style.lspx

Output

OUTPUT
(decision keep)

The command exits with status 0 and creates no files. In the Playground, paste and run the same program to see the same answer.

The two inputs describe the heading.

  • lines-left is the number of lines left on the page below the heading.
  • chapter-heading? is #t for a chapter heading and #f for a section heading.

This example expects a valid non-negative line count and a boolean. The calling application checks those inputs before calling the rule. If lines-left is not a number, the rule raises an error during execution. Read and Handle Errors shows how to handle such an error with guard.

Five lines leave room for text below the heading, and #f means a section heading. The answer is a list. Its keep symbol tells the application to leave the heading on this page.

Check 2 lines and 1 line

Keep the definition in house-style.lspx. Replace its final call with these three calls, collected in one list.

LISPEX
(list (heading-placement 2 #f)
      (heading-placement 1 #f)
      (heading-placement 5 #t))

Run lispex house-style.lspx again, or run the updated program in the Playground.

Output

OUTPUT
((decision keep) (decision next-page) (decision next-page))

The command again exits with status 0 and creates no files. Read the answers in the same order as the calls.

CaseWhy the rule gives this answerDecision
2 lines, section heading>= includes 2keep
1 line, section heading1 is less than 2next page
5 lines, chapter heading(not chapter-heading?) is falsenext page

Changing >= to > also moves the 2-line case to the next page. That case reveals whether the boundary value is included.

The rule uses next-page for both reasons to move a heading. To distinguish “not enough room” from “chapter heading,” give them separate branches and labels.

How the conditions work

and checks (>= lines-left 2) first. If it is false, and stops. Otherwise, it checks (not chapter-heading?). if evaluates only the selected branch. The quote keeps the chosen decision list as data.

The typesetting application counts lines-left while it lays out the page. It then passes that value to the rule. With the same two values and the same stated execution conditions, this rule produces the same result.

The publisher's editors decide the house style and review the rule. The application checks where its input data came from and decides whether to apply the answer to the page.

Exercise

Keep a section heading on the page only when at least 3 lines are left. Check the smallest count that keeps the heading and the largest count that moves it.

Show one answer

In house-style.lspx, change (>= lines-left 2) to (>= lines-left 3). Replace the final list of calls with this one.

LISPEX
(list (heading-placement 3 #f)
      (heading-placement 2 #f))

Run lispex house-style.lspx again.

Output

OUTPUT
((decision keep) (decision next-page))

The command exits with status 0 and creates no files.

Rewrite the rule to read input

So far you passed the two values in each call. An application usually sends them as JSON, and the rule reads them from the name input.

The JSON array [2, false] arrives as the list (2 #f). (car input) is the number of lines left, and (car (cdr input)) is the chapter-heading flag. A JSON object arrives as a list of pairs, each holding a key and its value.

LISPEX
(let ((lines-left (car input))
      (chapter-heading? (car (cdr input))))
  (if (and (>= lines-left 2) (not chapter-heading?))
      "allow"
      "deny"))

The condition is the same as in heading-placement. lispex rule run runs this file in the decision evaluator, so two things change. The decision evaluator refuses a rule that uses define or quoted data before running it, so this rule names its inputs with let. The decision evaluator reports a string result as the run's decision. So the rule returns "allow" where heading-placement returned (decision keep) and "deny" where it returned (decision next-page).

The Playground and lispex run do not supply input. Run a Decision Rule with JSON Input shows how to save the input in a JSON file and run the rule with lispex rule run. With [2, false], the decision is "allow".

What to do next

Once you can explain the 2-line and 1-line answers, continue to Read and Handle Errors. That lesson uses a diagnostic to find the code to fix and handles errors raised during execution with guard.

For other tasks, choose a guide.