Enumerating Haiku
#lang haiku-enum | package:haiku-enum |
#lang haiku-enum |
(starters |
(adjective noun)) |
(rule noun -> |
verb) |
(rule verb -> |
#:comma adjective) |
(rule adjective -> |
noun) |
@adjective[1]{black fierce gray plump wet} |
@adjective[2]{achy agile dormant mangy sober wrinkled} |
@noun[1]{ant owl stag wasp} |
@noun[2]{duckling panda songbird swordfish} |
@verb[1]{aches barks claws climbs roves snarls weeps} |
@verb[2]{awaits beckons gallops gambols slobbers} |
random-haiku: a function that accepts no arguments and returns a random haiku (as a string),
how-many-haiku:, a natural number that indicates how many different haiku this program generates,
nth-haiku: a function that accepts a natural number less than how-many-haiku and returns the corresponding haiku, and
enum/e: a one-way enumeration of the same haiku that nth-haiku returns
structured-haiku/e: a two-way enumeration that maps the naturals to an sexpression representing the choices made to construct a particular haiku
> (display (nth-haiku 0))
black stag aches, black stag
aches, black stag aches, black stag aches,
black stag aches, black stag
> (display (nth-haiku (- how-many-haiku 1)))
wrinkled duckling weeps,
wrinkled duckling slobbers, fierce
duckling slobbers, fierce
> (display (nth-haiku (quotient (- how-many-haiku 1) 2)))
plump duckling climbs, wet
ant claws, fierce ant roves, gray stag
barks, plump ant snarls, plump
> (display (nth-haiku 145095608713658901635917363016718021515))
snow gnomes hide, dishrag
coughs, rough blind dog falls, soft moons
fight, sting lithe knives drown
> (display (nth-haiku 5842574289046493184639147839447824095))
snow men crawl, bleak lame
earth scolds, fiends dabble, hog shrieks,
sad cow flops, shapeless
syntax
(rule haiku-state-id word-class-id -> next-state ...)
(rule haiku-state-id -> next-state ...)
next-state = maybe-lastness maybe-comma haiku-state-id maybe-lastness =
| #:only-last | #:not-last maybe-comma =
| #:comma
If #:only-last appears, then this state is entered only when it would be the last word in the haiku. If #:not-last appears, then this state is entered only when it would not be the last word in the haiku.
If #:comma appears, then a comma is generated in the haiku before continuing into the next state.
In addition to defining a new rule, a use of rule defines word-class-id (if the first case is used) or haiku-state-id (if the second case is used). It is defined to accept a natural number and a sequence of strings that are assumed to be words that all have the given number of syllables. The name can be used multiple times with different numbers of syllables.
(rule adjective -> noun)
(adjective 1 "tall" "fast") (adjective 2 "three-point" "driven") (adjective 3 "athletic")
(noun 4 "Michael_Jordan")
syntax
(starters (word-class-id haiku-state-id) ...)
Each clause indicates one way that a haiku can start, namely by generating a word from word-class-id and then moving into the state haiku-state-id.