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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
|
\documentclass[12pt]{article}
\usepackage[letterpaper,margin=.75in]{geometry}
\usepackage{alltt}
\addtolength{\parskip}{\baselineskip} % causes a blank line between paragraphs
\setlength{\parindent}{0in}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0in}
\rfoot{\small Page \thepage\ of \pageref{lastpage}}
\cfoot{}
\lfoot{\small rev. 12/26/14}
\begin{document}
\centerline{\Huge Oaklisp Summary}
\centerline{Blake McBride (blake@mcbride.name)}
\medskip
{\large \bfseries Startup:}
\smallskip
\begin{verbatim}
oaklisp [--world <file>] [--dump <file>] [-G]
where:
--world indicates what world to load. Default is oakworld.bin
that comes with the system
--dump provides a file name where a new world will be dumped
when the system exits
-G option to perform a GC prior to dumping a world
\end{verbatim}
{\large \bfseries Exiting the system:}
\begin{verbatim}
(exit)
Use ^D to exit error states
\end{verbatim}
{\large \bfseries File types:}
\smallskip
\hspace{.75in}
\hbox{\begin{tabular}{|l|l|}\hline
File ending & Description \\\hline
oak & Oaklisp source file \\
&\\
oa & compiled Oaklisp file (fasl) \\
&\\
bin & Oaklisp world --- binary representation of\\
& an entire Oaklisp system representing all \\
& objects in the running environment.\\\hline
\end{tabular}}
\smallskip
{\large \bfseries Loading and compiling files:}
\smallskip
\begin{verbatim}
(load "myfile") ; will load "myfile.oa" or "myfile.oak"
(compile-file #*current-locale "myfile") ; compiles myfile.oak to myfile.oa
\end{verbatim}
\newpage
{\Large \bfseries Data Types}
Use {\ttfamily (get-type x)} to get the type of an item.
\hspace{.75in}
\hbox{\begin{tabular}{|l|l|l|}\hline
Type & Comment & Example \\\hline
null-type & null list \& false & nil or \#f or {\ttfamily \symbol{13}}() \\
truths & true & t or \#t \\
&&\\
fixnum & decimal & 48 \\
\ \ \& & binary & \#b1011 \\
bignum & binary & \#2r1011 \\
& octal & \#o755 \\
& hex & \#xD5 \\
&&\\
fraction & decimal & 2/3 \\
& hex & \#xDE/FF \\
& floats are converted to fraction & 3.141 \\
&&\\
character & letter `M' & \#\verb+\+M \\
& & \#\verb+\+Space \\
& & \#\verb+\+Newline \\
& & \#\verb+\+Backspace \\
& & \#\verb+\+Tab \\
& & \#\verb+\+Return \\
& & \#\verb+\+Page \\
&&\\
symbol & case insensitive (up-cased) & {\ttfamily \symbol{13}}symb \\
& case sensitive & {\ttfamily \symbol{13}}$\mid$symb$\mid$ \\
& embedded `(' & {\ttfamily \symbol{13}}sy\verb+\+(mb \\
&&\\
string & with embedded \verb+"+ & ``hello\verb+\"+ there'' \\
&&\\
list && {\ttfamily \symbol{13}}(hello there) \\
&&\\
simple-vector && \#(3 4 hello) \\
&&\\
type & root of all types & type \\
& root of all objects & object \\
\hline
\end{tabular}}
\newpage
{\Large \bfseries List Operations}
\bigskip
\hbox{\begin{tabular}{|l|l|l|l|l|l|}\hline
Type & Comment & Sp & D & Example & Result \\\hline
car & first element & fast & no & (car {\ttfamily \symbol{13}}(A B C)) & A \\
cdr & rest of list & fast & no & (cdr {\ttfamily \symbol{13}}(A B C)) & (B C) \\
c????r & shorthand for & fast & no & & \\
& multiple car \& cdr's & & & & \\
& & & & & \\
cons & add a list node & fast & no & (cons {\ttfamily \symbol{13}}A {\ttfamily \symbol{13}}(B C)) & (A B C) \\
list & create a list & fast & no & (list {\ttfamily \symbol{13}}A {\ttfamily \symbol{13}}B {\ttfamily \symbol{13}}C) & (A B C) \\
append & append lists & slow & no & (append {\ttfamily \symbol{13}}(A B) {\ttfamily \symbol{13}}(D) {\ttfamily \symbol{13}}(G H)) & (A B D G H) \\
length & & slow & no & (length {\ttfamily \symbol{13}}(A B C)) & 3 \\
nth & & slow & no & (nth {\ttfamily \symbol{13}}(A B C) 1) & B \\
first & same as (nth 0 x) & & & & \\
second & same as (nth 1 x) & & & & \\
third & same as (nth 2 x) & & & & \\
etc. & & & & & \\
& & & & & \\
last & & slow & no & (last {\ttfamily \symbol{13}}(A B C)) & (C) \\
& & & & & \\
& Given: & & & & \\
& (setq lst {\ttfamily \symbol{13}}(A B C)) & & & & \\
& replace car cell & fast & yes & (set! (car lst) {\ttfamily \symbol{13}}X) & (X B C) \\
& replace cdr cell & fast & yes & (set! (cdr lst) {\ttfamily \symbol{13}}X) & (A . X) \\
\hline
\end{tabular}}
\bigskip
\begin{description}
\item{Sp} = Speed
\item{D} = Destructive
\end{description}
\newpage
{\Large \bfseries Comments}
\smallskip
Semi-colon `;' introduces a comment. Comments extend from the semi-colon to the end of the line.
\bigskip
{\Large \bfseries Evaluation and Quotes}
\smallskip
Lists are recursively evaluated. The first element of a list is the function and the remainder are arguments to the function. For example:
\begin{verbatim}
(func arg1 arg2 ...)
\end{verbatim}
Quoting stops evaluation, so
\begin{verbatim}
(quote (abc def ghi))
\end{verbatim}
would not attempt to evaluate \emph{abc}, \emph{def}, or \emph{ghi}. The ``{\ttfamily \symbol{13}}'' symbol acts as a shorthand for \emph{quote}, so the following is equivelent to the previous statement.
\hspace*{.3in}{\ttfamily \symbol{13}}\verb+(abc def ghi)+
\bigskip
{\Large \bfseries Predicates}
\bigskip
\hbox{\begin{tabular}{|l|l|}\hline
function & Returns \emph{NIL}, or \emph{\#T} for \\\hline
null? & null \\
symbol? & symbols \\
atom? & non-list items \\
list? & list items (including \emph{NIL}) \\
number? & all numbers \\
integer? & integers \\
string? & strings \\
vector? & vectors \\
eq? & same object \\
equal? & structurally similar objects \\
& or same number\\
\hline
\end{tabular}}
\newpage
{\Large \bfseries Logical Operators}
\bigskip
\hbox{\begin{tabular}{|l|l|l|l|}\hline
function & Returns & Example & Comment \\\hline
not & nil or \#T & (not exp) & Logical inverse \\
and & last exp or nil & (and exp1 exp2 ...) & Stops evalution on first null expression \\
or & first non-nil exp & (or exp1 exp2 ...) & Stops evalution on first non-null expression \\
\hline
\end{tabular}}
\emph{and} and \emph{or} can be used as conditionals too. For example:
\begin{verbatim}
(and exp1 exp2 exp3)
\end{verbatim}
is the same as
\begin{verbatim}
(if exp1
(if exp2 exp3))
\end{verbatim}
\bigskip
{\Large \bfseries Block}
\bigskip
\parbox[t]{3.4in}{
(block\\
\hspace*{2em} exp1\\
\hspace*{2em} exp2\\
\hspace*{2em} ...)
}\parbox[t]{3.5in}{
Execute each expression and return the value of the last expression.
}
\bigskip
{\Large \bfseries Local Variables}
\bigskip
\parbox[t]{3.4in}{
(let ((var1 val1) ; initialize \emph{var1} to \emph{val1}\\
\hspace*{2.5em}(var2 nil) ; initialize \emph{var2} to \emph{nil}\\
\hspace*{2.5em}...)\\
\hspace*{4em} exp1\\
\hspace*{4em} exp2\\
\hspace*{4em} ...)
}\parbox[t]{3.5in}{
\emph{LET:}\ \ Creates local variables and executes the expressions in their context. Returns the value of the last expression.
}
\parbox[t]{3.4in}{
(let* (vars) exp1 exp2 ...)
}\parbox[t]{3.5in}{
\emph{LET*:}\ \ Sames as \emph{LET} except the variables are assigned sequentially. Previously defined variables may be used in subsequent variable initializations.
}
\newpage
{\Large \bfseries Defining Functions}
\bigskip
\parbox[t]{3.4in}{
(define (function-name arg1 arg2 arg3 ...)\\
\hspace*{4em} exp1\\
\hspace*{4em} exp2\\
\hspace*{4em} ...)\\
\\
(define function-name\\
\hspace*{4em}(lambda (arg1 arg2 arg3 ...)\\
\hspace*{4em} exp1\\
\hspace*{4em} exp2\\
\hspace*{4em} ...))
}\parbox[t]{3.5in}{
\emph{DEFINE:}\ \ Define function named \emph{function-name} with specified arguments, and run the expressions in that context.
}
\bigskip
{\Large \bfseries Conditionals}
\smallskip
In Oaklisp, conditions are considered true if they are any value other
than \emph{NIL} or \emph{\#F}. Therefore, \emph{NIL} and \emph{\#F} are considered as \emph{false},
and all other values are treated as \emph{true} conditions.
\parbox[t]{3.4in}{
(if test\\
\hspace*{2em} exp1\\
\hspace*{2em} [exp2])
}\parbox[t]{3.5in}{
\emph{IF:}\ \ If \emph{test} expression is \emph{true}, return \emph{exp1}.
\emph{exp2} is optional. If \emph{test} is \emph{false}, return \emph{exp2} or \emph{undefined-value}.
}
\parbox[t]{3.4in}{
(cond\\
\hspace*{2em} (test1 exp1 exp2 ...)\\
\hspace*{2em} (test2 exp1 exp2 ..)\\
\hspace*{2em} ...\\
\hspace*{2em} (else exp1 exp2 ...))
}\parbox[t]{3.5in}{
\emph{COND:}\ \ Evalute each \emph{testN} until one is \emph{true}.
If a \emph{true} test is found, its expressions are evaluated and the
result of the last one is returned. The \emph{else} test is always \emph{true}.
}
\bigskip
{\Large \bfseries Math Predicates}
\bigskip
\hbox{\begin{tabular}{|l|l|}\hline
Function & Description \\\hline
(number? n) & Is \emph{n} a number \\
(zero? n) & \\
(odd? n) & \\
(even? n) & \\
(negative? n) & \\
(positive? n) & \\
(= a b) & \\
(!= a b) & \\
(\verb+<+ a b) & \\
(\verb+>+ a b) & \\
(\verb+<+= a b) & \\
(\verb+>+= a b) & \\
\hline
\end{tabular}}
\newpage
{\Large \bfseries Numeric Operators}
\bigskip
\hbox{\begin{tabular}{|l|l|}\hline
Function & Description \\\hline
+ - * / & Math functions \\
(abs n) & Absolute value \\
(1+ n) & \\
(quotient a b) & integers only \\
(minus n) & \\
(floor n) & \\
(ceiling n) & \\
(round n) & \\
(truncate n) & \\
(modulo a b) & \\
(remainder a b) & \\
(expt n p) & \\
(max a b c ...) & \\
(min a b c ...) & \\
(numerator f) & \\
(denominator f) & \\
\hline
\end{tabular}}
\bigskip
{\Large \bfseries String Predicates}
\bigskip
\hbox{\begin{tabular}{|l|l|}\hline
Function & Description \\\hline
(string? s) & is \emph{s} a string \\
(= a b) & \\
(equal? a b) & \\
(!= a b) & \\
(\verb+<+ a b) & \\
(\verb+>+ a b) & \\
(\verb+<+= a b) & \\
(\verb+>+= a b) & \\
\hline
\end{tabular}}
\bigskip
{\Large \bfseries String Operators}
\bigskip
\hbox{\begin{tabular}{|l|l|}\hline
Function & Description \\\hline
(length s) & \\
(nth s i) & 0 origin\\
(upcase s) &\\
(downcase s)&\\
(reverse s)&\\
(append s1 s2)&return an appended copy\\
(copy s)&\\
(subseq s beg len)&\\
\hline
\end{tabular}}
\newpage
{\Large \bfseries Macros}
\smallskip
\begin{alltt}
(define-syntax (add a b)
\symbol{18}(+ ,a ,b))
(gensym \symbol{13}tmp)
\end{alltt}
{\Large \bfseries Named let}
\smallskip
\begin{alltt}
(let loop ((v 0))
(if (!= v 10)
(block
(print v standard-output)
(newline)
(loop (1+ v)))))
\end{alltt}
{\Large \bfseries Printing}
\smallskip
\begin{alltt}
(print x standard-output)
(newline)
(format <stream> <fmt> <arg1> <arg2> ...)
<stream> = #T = standard-output
#F = return a string
\end{alltt}
\newpage
{\Large \bfseries Classes and Types}
In classical object-oriented terminology, the following terms apply:
\bigskip
\hbox{\begin{tabular}{|l|l|}\hline
Name & Description \\\hline
Class & describes the structure and functionality associaed with instances of it \\
& (e.g.\ instance variables and instance methods) \\
Meta Class & describes the structure and functionality of the class object \\
& (e.g.\ class variables and class methods) \\
Instance & a unique instance of a class \\
Object & a class, metaclass, or instance \\
\hline
\end{tabular}}
In Oaklisp, classes and types are the same thing. So, every type in
Oaklisp, such as list, symbol, string, number, fraction, etc., are all
classes. Also, all types and instances are objects. In Oaklisp, all
types and objects are first-class, this means they can be passed to
functions, assigned to variables, etc.. They are all treated the
same.
Oaklisp has two root classes/types named
\emph{Object} and \emph{Type}. \emph{Type} is the root of all types, and \emph{Object} is the
root of all objects.
For purposes of this and following examples, the following naming scheme shall be used:
\bigskip
\hbox{\begin{tabular}{|l|l|}\hline
Name & Description \\\hline
Type & the Type type \\
type & a type \\
Object & the Object type \\
object & an object \\
iv* & instance variable \\
cv* & class variable \\
i* & an instance object \\
im* & instance method \\
cm* & class method \\
\hline
\end{tabular}}
\bigskip
{\large \bfseries Classes -- Simple}
By ``simple'' we mean without meta classes.
\textbf{Creating a type or class:}
Format:
\begin{alltt}
(define <type-name>
(make Type <instance variable names> <superclass list>))
\end{alltt}
\newpage
Example:
\begin{alltt}
(define MyClass (make Type \symbol{13}(iv1 iv2) (list Object)))
\end{alltt}
\textbf{Creating an instance of a type:}
Format:
\begin{alltt}
(make <your-type>)
\end{alltt}
Example:
\begin{alltt}
(define i1 (make MyClass))
\end{alltt}
\textbf{Inspecting an object:}
Format:
\begin{alltt}
(describe <object>)
\end{alltt}
Example:
\begin{alltt}
(describe MyClass)
(describe i1)
\end{alltt}
\textbf{Creating instance methods:}
Format:
\begin{alltt}
(define-instance <method-name> operation)
(add-method (<method-name>
(<type name> <instance variables this method will access>)
self <method argument list>)
<method code>
...)
\end{alltt}
\newpage
Example:
\begin{alltt}
(define-instance imSet-iv1 operation)
(add-method (imSet-iv1 (MyClass iv1) self a)
(set! iv1 a))
(define-instance imGet-iv1 operation)
(add-method (imGet-iv1 (MyClass iv1) self)
iv1)
(imSet-iv1 i1 33)
(imGet-iv1 i1)
\end{alltt}
\textbf{Instance constructor / initialization:}
Format:
\begin{alltt}
(add-method (initialize (<type name> <instance variables being accessed>)
self <argument list>)
<initilization code>
...
self)
\end{alltt}
Example:
\begin{alltt}
(add-method (initialize (MyClass iv1 iv2) self arg1)
(set! iv1 arg1)
(set! iv2 44)
self)
(define i2 (make MyClass 88))
; in this case
; i2->iv1 would be 88
; i2->iv2 would be 44
\end{alltt}
\newpage
{\large \bfseries Classes -- Complex}
By ``complex'' we mean with the addition of meta classes. This gives us class variables and class methods.
\textbf{Creating a type or class:}
Creating a type with a meta-type involves the creation of two types; one the meta type and the other the type.
The type created must be an instance of the meta type.
Adding initializers and instance methods are as before. The only difference is we now have the ability
to define and use class methods and variables. Those will be shown.
Format:
\begin{alltt}
(define <meta-type-name>
(make Type <class variable names> <meta superclass list>))
(define <type-name>
(make <meta-type-name> <instance variable names> <superclass list>))
\end{alltt}
Example:
\begin{alltt}
(define metaMyClass2 (make Type \symbol{13}(cv1 cv2) (list Type)))
(define MyClass2 (make metaMyClass2 \symbol{13}(iv1 iv2) (list Object)))
\end{alltt}
\textbf{Class methods:}
Format:
\begin{alltt}
(define-instance <method-name> operation)
(add-method (<method-name>
(<meta type name> <class variables this method will access>)
self <method argument list>)
<method code>
...)
\end{alltt}
\newpage
Example:
\begin{alltt}
(define-instance cmSet-cv1 operation)
(add-method (cmSet-cv1 (metaMyClass2 cv1) self a)
(set! cv1 a))
(define-instance cmGet-cv1 operation)
(add-method (cmGet-cv1 (metaMyClass2 cv1) self)
cv1)
(cmSet-cv1 MyClass2 33)
(cmGet-cv1 MyClass2)
(describe MyClass2) ; to see the contents of the class
\end{alltt}
\label{lastpage}
\end{document}
|