1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
|
\documentclass{monograph}
\usepackage{scheme}
\iflatex
\usepackage{fullpage}
\pagestyle{plain}
\fi
\ifhtml
\documenttitle[stex.css]{Introduction to stex}
\fi
\begin{document}
\title{Introduction to stex}
\author{R. Kent Dybvig and Oscar Waddell}
\date{April 2016}
\maketitle
\def\copyrightnotice{\copyright~1998--2016 R. Kent Dybvig and Oscar Waddell}
\ifhtml
\tableofcontents
\fi
\iflatex
% block paragraphs
\setlength\parskip{4pt plus 1pt}
\setlength\parindent{0pt}
\renewenvironment{quotation}
{\list{}{\listparindent 1.5em%
\itemindent 0pt%
\rightmargin \leftmargin
\parsep 0pt plus 1pt}%
\item\relax}
{\endlist}
\fi
\section{Overview}
The \emph{stex} package consists of two main programs and some supporting
items, such as make files, make-file templates, class files, and style
files.
The two main programs are \textbf{scheme-prep} and \textbf{html-prep}.
\textbf{scheme-prep} performs a conversion from ``stex''-formatted files
into latex-formatted files, while \textbf{html-prep} converts (some)
latex-formatted files into html-formatted files.
An stex file is really just a latex file extended with a handful of
commands for including Scheme code (or pretty much any other kind of code,
as long as you don't plan to use the Scheme-specific transcript support)
in a document, plus a couple of additional features rather arbitrarily
thrown in.
The subset of latex-formatted files \textbf{html-prep} is capable of
handling is rather small but has nevertheless been useful for our
purposes, which include producing html versions of a couple of books
(\emph{The Scheme Programming Language}, Editions 2--4 and the Chez Scheme
User's Guides for Versions 6--9), the scheme.com web site, class websites,
class assignments, and various other documents.
\section{Installation}
A prerequisite to building and using stex is to have Chez Scheme or
Petite Chez Scheme installed on your system.
You'll also need pdflatex, dvips, ghostscript, and netbpm.
We've run stex under Linux and OS X but have not tried to run it under
Windows.
The simplest way to install stex for your personal use is to clone the
stex directory into your home directory, cd into the stex directory,
and run make:
\schemedisplay
make BIN=\var{bindir}
\endschemedisplay
where \var{bindir} is the directory where make will find the scheme or
petite executables.
This will create a subdirectory, named for the installed Chez Scheme
machine type, containing binary versions of the programs.
You can also use ``make install'' to make stex available for other users.
\schemedisplay
sudo make install BIN=\var{bindir} LIB=\var{libdir}
\endschemedisplay
where \var{bindir} is as described above, and \var{libdir} is the
directory where the stex library directory should be installed.
\section{Usage notes}
The simplest way to get started with stex is to get this document to
build (in the doc directory) and create your own document by cloning
this document's source file (stex.stex) and make file (Makefile).
If you've installed stex in your home directory, you should be able to
build this document by running ``make'' without arguments in the doc
directory.
If you've installed stex elsewhere, you'll first have to modify the
include for Mf-stex to reflect its installed location.
Makefile is where you declare your stex sources and various other things,
like bibtex and graphics files.
If you don't have anything much more complicated than this document,
you might just need to change the line that declares the main stex entry
point, i.e., the line that reads \scheme{x = stex}, to reflect the name
of your document.
The make is orchestrated by Mf-stex, which knows how to run
\textbf{scheme-prep}, \textbf{html-prep}, \textbf{pdflatex} (multiple
times), and various other commands to produce both pdf and html versions
of the stex document.
You can also consult the more elaborate stex source and make files for the
Chez Scheme User's Guide in the csug directory of a Chez Scheme release.
\section{Basic stex commands}
\subsection{Inline code}
An stex document includes inline Scheme (or other) code via the
\scheme{\scheme} command, e.g.:
\begin{quotation}
\schemeverbatim
When called with two arguments, \scheme{cons} creates a pair of the two
arguments, e.g., \scheme{(cons 3 4)} produces \scheme{(3 . 4)}.
\endschemeverbatim
\end{quotation}
produces:
\begin{quotation}
When called with two arguments, \scheme{cons} creates a pair of the two
arguments, e.g., \scheme{(cons 3 4)} produces \scheme{(3 . 4)}.
\end{quotation}
\subsection{Code displays}
An stex document includes out-of-line Scheme (or other) code via
\scheme{\schemedisplay} and \scheme{\endschemedisplay}, e.g.:
\begin{quotation}
\schemeverbatim
\schemedisplay
(define fact
(lambda (x)
"a light year is a measure of distance"))
(define fib
(lambda (x)
"a light year is a measure of time"))
\endschemedisplay
\endschemeverbatim
\end{quotation}
produces:
\begin{quotation}
\schemedisplay
(define fact
(lambda (x)
"a light year is a measure of distance"))
(define fib
(lambda (x)
"a light year is a measure of time"))
\endschemedisplay
\end{quotation}
Within a Scheme display, \scheme{;=>} is converted into a double right arrow
($\Rightarrow$), \scheme{;->} into a single right arrow ($\rightarrow$), and
\scheme{;==} into a phantom of the same size.
This is useful for showing what a piece of code translates or evaluates to,
e.g.:
\begin{quotation}
\schemeverbatim
A \scheme{let} expression expands into a call to a \scheme{lambda}
expression, e.g.:
\schemedisplay
(let ([a 17]) ;-> ((lambda (a) (+ a a))
(+ a a)) ;== 17)
\endschemedisplay
A \scheme{let} expression first evaluates the right-hand-side
expression, then evaluates the body in an environment that binds
the left-hand-side variable to the resulting value, e.g.:
\schemedisplay
(let ([a 17]) ;=> 17
(+ a a))
\endschemedisplay
\endschemeverbatim
\end{quotation}
produces
\begin{quotation}
A \scheme{let} expression expands into a call to a \scheme{lambda}
expression, e.g.:
\schemedisplay
(let ([a 17]) ;-> ((lambda (a) (+ a a))
(+ a a)) ;== 17)
\endschemedisplay
A \scheme{let} expression first evaluates the right-hand-side
expression, then evaluates the body in an environment that binds
the left-hand-side variable to the resulting value, e.g.:
\schemedisplay
(let ([a 17]) ;=> 17
(+ a a))
\endschemedisplay
\end{quotation}
\subsection{Variables}
Code can include emphasized variables via the \scheme{\raw{{\schbackslash}var}} command, e.g.:
\begin{quotation}
\schemeverbatim
\scheme{(let ([\var{x} \var{e}]) \var{body})} binds the variable \var{x} to
the value of \var{e} in \var{body}.
\endschemeverbatim
\end{quotation}
produces:
\begin{quotation}
\scheme{(let ([\var{x} \var{e}]) \var{body})} binds the variable \var{x} to
the value of \var{e} in \var{body}.
\end{quotation}
If the text within a \scheme{\raw{{\schbackslash}var}} form contains an underscore, the
following character or bracketed subform is converted into a subscript,
e.g., \scheme{\raw{{\schbackslash}var{\schlbrace}abc\_3{\schrbrace}}}
produces \var{abc_3}, and \scheme{\raw{{\schbackslash}var{\schlbrace}7e5\_{16}{\schrbrace}}}
produces \var{7e5_{16}}.
\scheme{\raw{{\schbackslash}var}} forms may appear within a \scheme{\scheme} form, within a Scheme display
formed by \scheme{\schemedisplay} and \scheme{\endschemedisplay} commands,
or by itself outside of either.
\subsection{Raw text in code}
Raw text can be included in code via the \scheme{\raw{{\schbackslash}raw}} command.
For example:
\begin{quotation}
\schemeverbatim
\schemedisplay
(sqrt \raw{$x$}) \is \raw{$\sqrt{x}$}.
\endschemedisplay
\endschemeverbatim
\end{quotation}
produces:
\begin{quotation}
\schemedisplay
(sqrt \raw{$x$}) ;=> \raw{$\sqrt{x}$}.
\endschemedisplay
\end{quotation}
\subsection{Generated output}
Output generated by a Scheme program can be inserted into the output via
\scheme{\generated} and \scheme{\endgenerated} commands, e.g.:
\begin{quotation}
\schemeverbatim
\generated
(let ()
(define fibs
(lambda (x y n)
(if (= n 0)
'()
(cons x (fibs y (+ x y) (- n 1))))))
(let ([n 5])
(printf "first ~r primes: ~{~s~^, ~}\n" n (fibs 0 1 n))))
\endgenerated
\endschemeverbatim
\end{quotation}
produces:
\begin{quotation}
\generated
(let ()
(define fibs
(lambda (x y n)
(if (= n 0)
'()
(cons x (fibs y (+ x y) (- n 1))))))
(let ([n 5])
(printf "first ~r primes: ~{~s~^, ~}\n" n (fibs 0 1 n))))
\endgenerated
\end{quotation}
\subsection{Verbatim Scheme displays}
When special features, like \scheme{\raw{{\schbackslash}var}} forms, need to be
suppressed within a Scheme display, a document can use
\scheme{\schemeverbatim} and \scheme{\endschemeverbatim} instead of
\scheme{\schemedisplay} and \scheme{\endschemedisplay}.
This document makes extensive use of this feature.
\section{Scheme transcripts}
\subsection{Automatic transcript generation}
The \textbf{scheme-prep} package supports a
\scheme{\transcript} command for automatically generating Scheme transcripts
from input supplied in the document source.
All text from the \scheme{\transcript} marker up to and including the
\scheme{\endtranscript} marker is replaced with a transcript generated
by supplying the intervening text as the input to a Scheme caf\'e (REPL).
If the Scheme transcript needs to contain the sequence
\scheme{\endtranscript}, a different terminator may be specified as an
optional argument to \scheme{\transcript}.
The terminator must be a backslash followed by one or more alphabetic
characters, and is specified without the backslash in the optional argument.
Three pairs of commands may be redefined to customize the typesetting
of different elements within generated transcripts.
To modify the typesetting of error messages, redefine \scheme{\transerr}
and \scheme{\endtranserr}.
To modify the typesetting of user input read from the current input port
of the caf\'e, redefine \scheme{\transin} and \scheme{\endtransin}.
To modify the typesetting of program output written to the current
output port of new caf\'e, redefine \scheme{\transout} and
\scheme{\endtransout}.
For example, the following:
\begin{quotation}
\schemeverbatim
\transcript
(define f
(lambda (x) ; indentation and comments are
(if (zero? x) ; preserved in the transcript
1
(* x (f (- x 1))))))
(values f (f 0) (f 5) (f 20))
(trace f)
(f 4)
\endtranscript
\endschemeverbatim
\end{quotation}
produces:
\begin{quotation}
\transcript
(define f
(lambda (x) ; indentation and comments are
(if (zero? x) ; preserved in the transcript
1
(* x (f (- x 1))))))
(values f (f 0) (f 5) (f 20))
(trace f)
(f 4)
\endtranscript
\end{quotation}
The following example shows how to specify a different transcript
terminator and shows the default formatting imposed by \scheme{\transerr},
\scheme{\transin}, and \scheme{\transout}.
\begin{quotation}
\schemeverbatim
\transcript[\stopthistranscript]
#e4.5
(begin (display "Enter a character: ") (read-char))
(begin (display "Enter a character: ") (read-char))Z
(begin (clear-input-port) (display "Enter a character: ") (read-char))
Z
(list (read-char) (read-char) (read-char))abc def
(define silly-repl
(lambda (prompt)
(display prompt)
(let ([x (read)])
(unless (eof-object? x)
(let ([result (eval x)])
(unless (eq? result (void))
(pretty-print result))
(silly-repl prompt))))))
(silly-repl "Enter a Scheme expression: ")
(list 1
2
3)
(silly-repl "Now what? ")
(define interview
(lambda ()
(let* ([fname (begin (display "First name: ") (read))]
[lname (begin (display "Last name: ") (read))])
(printf "Hello ~a ~a!~%" fname lname))))
(interview)
john
doe
#!eof
(printf "good to be back~%")
#!eof
\stopthistranscript
\endschemeverbatim
\end{quotation}
produces:
\begin{quotation}
\transcript[\stopthistranscript]
#e4.5
(begin (display "Enter a character: ") (read-char))
(begin (display "Enter a character: ") (read-char))Z
(begin (clear-input-port) (display "Enter a character: ") (read-char))
Z
(list (read-char) (read-char) (read-char))abc def
(define silly-repl
(lambda (prompt)
(display prompt)
(let ([x (read)])
(unless (eof-object? x)
(let ([result (eval x)])
(unless (eq? result (void))
(pretty-print result))
(silly-repl prompt))))))
(silly-repl "Enter a Scheme expression: ")
(list 1
2
3)
(silly-repl "Now what? ")
(define interview
(lambda ()
(let* ([fname (begin (display "First name: ") (read))]
[lname (begin (display "Last name: ") (read))])
(printf "Hello ~a ~a!~%" fname lname))))
(interview)
john
doe
#!eof
(printf "good to be back~%")
#!eof
\stopthistranscript
\end{quotation}
Transcripts do not include a trailing prompt by design.
This is done in such a way that an explicitly displayed string that
happens to look like the prompt is not suppressed.
For example:
\begin{quotation}
\schemeverbatim
\transcript
(begin (display "> \n") (exit))
\endtranscript
\endschemeverbatim
\end{quotation}
should leave the apparent prompt alone since it is generated as program
output.
\begin{quotation}
\transcript
(begin (display "> \n") (exit))
\endtranscript
\end{quotation}
Prompt suppression works even with
changes to \scheme{waiter-prompt-string}.
For example:
\begin{quotation}
\schemeverbatim
\transcript
(waiter-prompt-string "antelope? ")
"no thanks"
\endtranscript
\endschemeverbatim
\end{quotation}
produces no trailing "antelope? " prompt:
\begin{quotation}
\transcript
(waiter-prompt-string "antelope? ")
"no thanks"
\endtranscript
\end{quotation}
\subsection{Loading initialization code}
The \scheme{stex} commands \scheme{\schemeinit} and \scheme{\endschemeinit}
are used to bracket Scheme expressions that should be evaluated without
generating a transcript of the results.
This is useful, for example, when writing the description of a programming
assignment. The solutions can be loaded via \scheme{\schemeinit} and a
transcript showing how the solutions behave can be generated using the
\scheme{\transcript} command.
For example, the following text:
\begin{quotation}
\schemeverbatim
\schemeinit
(waiter-prompt-string ">") ; restore the original prompt setting so we
; don't get "antelope?" as the prompt
(define compute-length
(lambda (x)
(cond
[(list? x) (length x)]
[(vector? x) (vector-length x)]
[(string? x) (string-length x)]
[else (errorf 'compute-length "cannot handle ~s" ls)])))
\endschemeinit
\emph{The \scheme{compute-length} procedure behaves as follows:}
\transcript
(compute-length '())
(compute-length '(a b c))
(compute-length "abcd")
(compute-length (vector 1 2 3 4 5 6))
(compute-length compute-length)
\endtranscript
\endschemeverbatim
\end{quotation}
produces the output shown below.
\begin{quotation}
\schemeinit
(waiter-prompt-string ">") ; restore the original prompt setting so we
; don't get "antelope?" as the prompt
(define compute-length
(lambda (x)
(cond
[(list? x) (length x)]
[(vector? x) (vector-length x)]
[(string? x) (string-length x)]
[else (errorf 'compute-length "cannot handle ~s" x)])))
\endschemeinit
\emph{The \scheme{compute-length} procedure behaves as follows:}
\transcript
(compute-length '())
(compute-length '(a b c))
(compute-length "abcd")
(compute-length (vector 1 2 3 4 5 6))
(compute-length compute-length)
\endtranscript
\end{quotation}
the last line of input intentionally causes an error, which is
displayed just as it would be displayed in a caf\'e.
\section{html-prep support for the \texttt{tabular} environment}
Support for tables comes with a few caveats:
\begin{enumerate}
\item \scheme{@\schlbrace\schrbrace} directives within \scheme{tabular}
column specifiers are flat-out ignored for the time being.
\item \scheme{|} directives within \scheme{tabular}
column specifiers are essentially ignored. Their only effect is
to globally enable borders for the entire HTML table.
\item \scheme{\hrule} and \scheme{\cline} are not yet implemented.
\item no warranty is expressed or implied.
\end{enumerate}
The following code:
\begin{quotation}
\begin{verbatim}
\begin{tabular}{rcl|r}
y &=& f(x) & without loss of generality \\
z & \multicolumn{2}{r}{whee} & this is fun? \\
\multicolumn{4}{c}{
\begin{tabular}{cc}
1 & 2 \\
3 & 4
\end{tabular}
} \\
a & b & c & d \\
12345 & z & \multicolumn{2}{l}{\scheme{(define x "foo")}}
\end{tabular}
\end{verbatim}
\end{quotation}
generates this table:
\begin{quotation}
\begin{tabular}{rcl|r}
y &=& f(x) & without loss of generality \\
z & \multicolumn{2}{r}{whee} & this is fun? \\
\multicolumn{4}{c}{
\begin{tabular}{cc}
1 & 2 \\
3 & 4
\end{tabular}
} \\
a & b & c & d \\
12345 & z & \multicolumn{2}{l}{\scheme{(define x "foo")}}
\end{tabular}
\end{quotation}
\end{document}
|