joy.texi 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. \input texinfo
  2. @c -*-texinfo-*-
  3. @c %**start of header
  4. @setfilename joy.info
  5. @documentencoding UTF-8
  6. @settitle Guile Joy Reference Manual
  7. @c %**end of header
  8. @include version.texi
  9. @copying
  10. Copyright @copyright{} 2016, 2017, 2018, 2020 Eric Bavier
  11. Permission is granted to copy, distribute and/or modify this document
  12. under the terms of the GNU Free Documentation License, Version 1.3 or
  13. any later version published by the Free Software Foundation; with no
  14. Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
  15. copy of the license is included in the section entitled ``GNU Free
  16. Documentation License''.
  17. @end copying
  18. @dircategory Software development
  19. @direntry
  20. * joy: (joy). Compiler for the Joy language.
  21. @end direntry
  22. @titlepage
  23. @title Joy Reference Manual
  24. @subtitle A compiler for the Joy programming language
  25. @author Eric Bavier
  26. @page
  27. @vskip 0pt plus 1fill
  28. Edition @value{EDITION} @*
  29. @value{UPDATED} @*
  30. @insertcopying
  31. @end titlepage
  32. @contents
  33. @c **********************************************************************
  34. @node Top
  35. @top Joy
  36. This document describes Joy version @value{VERSION}, an implementation
  37. of the Joy programming language.
  38. @menu
  39. * Introduction:: What is Joy?
  40. * Installation:: Installing Joy.
  41. * Invoking joy:: Running the compiler.
  42. * Programming in Joy:: The Joy of programming.
  43. * Interactive Joy:: Programming interactively with Joy.
  44. * API Reference:: Joy batteries included.
  45. * GNU Free Documentation License:: The license of this manual.
  46. @end menu
  47. @c **********************************************************************
  48. @node Introduction
  49. @chapter Introduction
  50. Joy is an implementation of the purely functional, concatenative
  51. programming language originally invented by Manfred Thun. It is
  52. implemented using Guile's multi-language support. This implementation
  53. strays a bit from the reference implementation written in C but is
  54. mostly compatible, except for the notable lack of support for ``sets''.
  55. @node Installation
  56. @chapter Installation
  57. Installing Joy from source uses the same build procedure as that for GNU
  58. software. See the files @file{README} and @file{INSTALL} in the source
  59. tree for additional details. It requires
  60. @url{http://gnu.org/software/guile/, GNU Guile}.
  61. After building Joy successfully, it is a good idea to run the test
  62. suite. Reporting any failures is a good way to help improve the
  63. software. To run the test suite, type:
  64. @example
  65. make check
  66. @end example
  67. Test cases can be run in parallel using the @code{-j} option of
  68. GNU@tie{}make.
  69. If any failures occur, please email @email{bavier@@member.fsf.org} and
  70. attach the @file{testsuite.log} file.
  71. @c **********************************************************************
  72. @node Invoking joy
  73. @chapter Invoking joy
  74. The following command-line options are supported:
  75. @table @code
  76. @item --help
  77. @itemx -h
  78. Show a help message and then exit.
  79. @item --version
  80. @itemx -V
  81. Display Joy's version number and then exit.
  82. @item --include=@var{dir}
  83. @itemx -I @var{dir}
  84. Add @var{dir} to the list of directories that are searched when using
  85. the @code{include} operator.
  86. @item --stack @var{atom} @dots{}
  87. @itemx -S @var{atom} @dots{}
  88. Initialize the stack with @var{atom} @dots{}. Each @var{atom} may be
  89. either a number or string.
  90. @example
  91. $ cat hello.joy
  92. "base" include
  93. "Hello " swoncat putchars
  94. $ joy hello.joy --stack John
  95. Hello John
  96. $ cat double.joy
  97. dup + .
  98. $ joy double.joy --stack 3
  99. 6
  100. @end example
  101. @end table
  102. @c **********************************************************************
  103. @node Programming in Joy
  104. @chapter Programming in Joy
  105. In Joy, everything is an operator that takes a stack as an argument
  106. and returns a (possibly modified) stack. Operators may be further
  107. classified by the way they manipulate their input stack to produce
  108. their output. E.g. some operators simply push something to the top of
  109. the stack; some remove one or more items; some remove one or more
  110. items and push another; and others, typically called ``combinators'',
  111. cause some stack items to executed in some way.
  112. Joy is called a ``concatenative'' language because in a purely
  113. functional sense, the concatenation of two Joy programs can be
  114. understood as the composition of their equivalent functions.
  115. @c **********************************************************************
  116. @node Interactive Joy
  117. @chapter Interactive Joy
  118. Editing text files then running them with @code{joy} is useful, and
  119. the programs are then committed to a file, but sometimes it can be
  120. helpful during development to be able to experiment quickly with some
  121. code before you know what works yet. Joy supports ``interactive''
  122. development by providing a @acronym{REPL, read-eval-print-loop}. If
  123. @code{joy} is invoked with no arguments, then it enteres a REPL.
  124. @example
  125. > joy
  126. @dots{}
  127. joy-repl@@(guile-user)> 1 2 10
  128. [10 2 1]
  129. joy-repl@@(guile-user)> swap - +
  130. [9]
  131. joy-repl@@(guile-user)> "base" include
  132. [9]
  133. joy-repl@@(guile-user)> 7 [8 *]
  134. [[8 *] 7 9]
  135. joy-repl@@(guile-user)> DEFINE foo == dip +
  136. [[8 *] 7 9]
  137. joy-repl@@(guile-user)> foo
  138. [79]
  139. joy-repl@@(guile-user)> newstack
  140. []
  141. @end example
  142. One may experiment this way before committing a working sequence to
  143. their program. After each expression is evaluated the current stack
  144. is displayed, so the effect that expression had on the stack can be
  145. seen.
  146. The Joy REPL is implemented as a Guile language @xref{Other
  147. Languages,,Support for Other Languages,guile}. This means Joy's REPL
  148. can be accessed through Guile, albeit without the pretty-printing:
  149. @example
  150. > guile
  151. @dots{}
  152. scheme@@(guile-user)> (display "Hello Guile!")(newline)
  153. Hello Guile!
  154. scheme@@(guile-user)> ,L joy-repl
  155. Happy hacking with Joy (REPL)! To switch back type `,L scheme'.
  156. joy-repl@@(guile-user)> 2 3 dup +
  157. $1 = (6 2)
  158. joy-repl@@(guile-user)> ,L scheme
  159. Happy hacking with Scheme! To switch back type `,L joy-repl'.
  160. scheme@@(guile-user)> (length $1)
  161. $2 = 3
  162. scheme@@(guile-user)> (car $1)
  163. $3 = 6
  164. @end example
  165. The Joy language itself can also be used in Guile. But because every
  166. Joy expression is an operator (i.e. a Guile procedure) that takes a
  167. stack as input and produces another stack as output, one must apply
  168. manually, from Scheme, every expression/operator entered in the Joy
  169. language to a stack argument. Despite the overhead, this might still
  170. be useful in some circumstances.
  171. @example
  172. > guile
  173. @dots{}
  174. scheme@@(guile-user)> ,L joy
  175. Happy hacking with Joy! To switch back type `,L scheme'.
  176. joy@@(guile-user)> dup + +
  177. $1 = #<procedure 86ba230 S>
  178. joy@@(guile-user)> ,L scheme
  179. Happy hacking with Scheme! To switch back type `,L joy'.
  180. scheme@@(guile-user)> (apply $1 '(6 10))
  181. $2 = (22)
  182. scheme@@(guile-user)> (apply $1 '(5 3))
  183. $3 = (13)
  184. scheme@@(guile-user)> (apply $1 '(1 10 19))
  185. $4 = (11 19)
  186. @end example
  187. @c **********************************************************************
  188. @node API Reference
  189. @chapter API Reference
  190. We discuss here the various Joy operators@footnote{The term
  191. ``operator'' is used consistently here to refer to any term that
  192. affects the stack when executed. Indeed, one may consider every Joy
  193. term to be an operator, even literals, e.g.@: @code{2}, because it
  194. affects the stack by pushing itself onto the top of the stack. We do
  195. not distinguish in our discussion ``combinators'' from ``operators''
  196. as the literature does.} provided for general use. Operator
  197. signatures are denoted by the result they have on the top of stack,
  198. where the rightmost element is the top element. Any element of the
  199. stack not included in an operator signature is untouched.
  200. Do not be confused by the ordering of the operator name and the
  201. operator signature in the following. Remember that Joy uses postfix
  202. notation, so in practice an operator defined like
  203. @deffn {Example Operator} frob [B] [A] @result{} [C]
  204. This operator frobs @code{A} and @code{B} into @code{C}.
  205. @end deffn
  206. @noindent
  207. would be used in a joy program like
  208. @example
  209. [B] [A] frob
  210. @result{} [C]
  211. @end example
  212. @menu
  213. * Primitives:: Essential operators.
  214. * Standard Library:: Generally useful operators.
  215. * Lisp Aliases::
  216. * Forth Aliases::
  217. @end menu
  218. @node Primitives
  219. @section Primitives
  220. This section summarizes the primitive operators that will be used
  221. almost everywhere. These primitives are written directly in Guile for
  222. performance, and all other operators are written in terms of them.
  223. @deffn {Joy Operator} dup A @result{} A A
  224. This operator places a copy of the top element onto the stack.
  225. @end deffn
  226. @deffn {Joy Operator} pop A @result{}
  227. This operator removes the top element of the stack.
  228. @end deffn
  229. @deffn {Joy Operator} swap B A @result{} A B
  230. This operator swaps the position of the top two elements.
  231. @end deffn
  232. @deffn {Joy Operator} cons B [A @dots{}] @result{} [B A @dots{}]
  233. This operator inserts @var{B} into the front of a quotation.
  234. @end deffn
  235. @deffn {Joy Operator} uncons [B A @dots{}] @result{} B [A @dots{}]
  236. This operator removes @var{B} from the front of a quotation and places
  237. it, followed by the rest of the quotation, back on the stack.
  238. @end deffn
  239. @deffn {Joy Operator} choice C B A @result{} D
  240. This operator leaves @var{D} on the top of the stack, where @var{D} is
  241. @var{B} if @var{C} is @var{true}, otherwise @var{A}.
  242. @end deffn
  243. @deffn {Joy Operator} stack @result{} [S]
  244. This operator pushes the entire current stack as a quotation.
  245. @end deffn
  246. @deffn {Joy Operator} unstack @dots{} [S0 S1 @dots{} SN] @result{} S0 S1 @dots{} SN
  247. This operator replaces the entire stack with the contents of the
  248. quotation at the top.
  249. @end deffn
  250. @deffn {Joy Operator} infra [S] [A] @result{} [S']
  251. This operator executes the quotation @var{[A]}, using the list
  252. @var{[S]} as the stack, then takes the resulting stack and pushes it
  253. as a list onto the original stack.
  254. @example
  255. 1 2 [3 4 5] [+] infra
  256. @result{} [1 2 [3 9]]
  257. @end example
  258. @end deffn
  259. @deffn {Joy Operator} + B A @result{} C
  260. Removes the two topmost integers and pushes their sum. The current
  261. implementation does not fully support floating point numbers.
  262. @end deffn
  263. @deffn {Joy Operator} - B A @result{} C
  264. Removes the two topmost integers @var{A} and @var{B} and pushes
  265. @math{@var{B} - @var{A}}.
  266. @end deffn
  267. @deffn {Joy Operator} < B A @result{} C
  268. Removes the two topmost integers @var{A} and @var{B} and pushes
  269. @code{true} if @math{@var{B} < @var{A}}, otherwise @code{false}.
  270. @end deffn
  271. @deffn {Joy Operator} > B A @result{} C
  272. Removes the two topmost integers @var{A} and @var{B} and pushes
  273. @code{true} if @math{@var{B} > @var{A}}, otherwise @code{false}.
  274. @end deffn
  275. @deffn {Joy Operator} = B A @result{} C
  276. Removes the two topmost integers @var{A} and @var{B} and pushes
  277. @code{true} if @math{@var{B} == @var{A}}, otherwise @code{false}.
  278. @end deffn
  279. @deffn {Joy Operator} logical A @result{} B
  280. Removes the topmost stack element and if it is either @code{true} or
  281. @code{false} pushes @code{true}, otherwise @code{false}.
  282. @end deffn
  283. @deffn {Joy Operator} char A @result{} B
  284. Removes the topmost stack element and pushes @code{true} if it is a
  285. character, otherwise @code{false}.
  286. @end deffn
  287. @deffn {Joy Operator} integer A @result{} B
  288. Removes the topmost stack element and pushes @code{true} if it is an
  289. integer, otherwise @code{false}.
  290. @end deffn
  291. @deffn {Joy Operator} string A @result{} B
  292. Removes the topmost stack element and pushes @code{true} if it is a
  293. string, otherwise @code{false}.
  294. @end deffn
  295. @deffn {Joy Operator} list A @result{} B
  296. Removes the topmost stack element and pushes @code{true} if it is a
  297. list/quotation, otherwise @code{false}.
  298. @end deffn
  299. @deffn {Joy Operator} putch A @result{}
  300. This operator expects a character as the topmost stack element. It
  301. removes it and writes it to standard out.
  302. @end deffn
  303. @deffn {Joy Operator} putchars A @result{}
  304. This operator expects a string (of characters) as the topmost stack
  305. element. It removes it and write it to standard out.
  306. @end deffn
  307. @deffn {Joy Operator} write A @result{}
  308. @deffnx {Joy Operator} . A @result{}
  309. This operator removes the topmost stack element and writes it to
  310. stdout followed by a newline.
  311. @example
  312. 1 'a [1 2] "foo" . . . .
  313. @print{} "foo"
  314. @print{} [1 2]
  315. @print{} 'a
  316. @print{} 1
  317. @result{} []
  318. @end example
  319. @end deffn
  320. @deffn {Joy Operator} def [name] [term] @result{}
  321. This operator binds in the global scope a variable @var{name} to the
  322. value @var{term}. It is equivalent to using @code{DEFINE}. Indeed,
  323. @code{DEFINE} is implemented as syntactic sugar for @code{def}.
  324. @example
  325. DEFINE foo == 2 + ; @equiv{} [foo] [2 +] def
  326. @end example
  327. @end deffn
  328. @deffn {Joy Operator} include filename @result{}
  329. This operator expects a string as the topmost stack element. A file
  330. with that name is searched for in the standard Joy installation
  331. directories as well as any directories given with the -I command-line
  332. option. If the filename ends in ``.joy'' it may be left off. If
  333. found, this file will be compiled and executed. Typically files
  334. included with this operator contain only operator definitions.
  335. @end deffn
  336. @deffn {Joy Operator} exit A @result{}
  337. This operator expects an integer as the topmost stack element.
  338. Immediately stops execution and exits with the integer status at the
  339. top of the stack.
  340. @example
  341. [1 2 >] ["success" putchars] [1 exit] ifte .
  342. @result{}
  343. @end example
  344. @end deffn
  345. @node Standard Library
  346. @section Standard Library
  347. @subsection Literals
  348. @deffn {Joy Operator} true @result{} true
  349. Pushes the logical @code{true} on top of the stack.
  350. @end deffn
  351. @deffn {Joy Operator} false @result{} false
  352. Pushes the logical @code{false} on top of the stack.
  353. @end deffn
  354. @subsection Stack Manipulation
  355. @deffn {Joy Operator} newstack @dots{} @result{}
  356. Remove all stack elements, leaving an empty stack.
  357. @end deffn
  358. @deffn {Joy Operator} popd B A @result{} A
  359. Remove the penultimate stack element.
  360. @end deffn
  361. @deffn {Joy Operator} dupd B A @result{} B B A
  362. Push a copy of the penultimate stack element under the topmost
  363. element.
  364. @end deffn
  365. @deffn {Joy Operator} swapd C B A @result{} B C A
  366. Swap the second and third stack elements.
  367. @end deffn
  368. @deffn {Joy Operator} dup2 B A @result{} B A B A
  369. Push copies of the top two stack elements.
  370. @end deffn
  371. @deffn {Joy Operator} pop2 B A @result{}
  372. @deffnx {Joy Operator} poppop B A @result{}
  373. Discard the top two stack elements.
  374. @end deffn
  375. @deffn {Joy Operator} dig1 B A @result{} A B
  376. @deffnx {Joy Operator} dig2 C B A @result{} B A C
  377. @deffnx {Joy Operator} dig3 D C B A @result{} C B A D
  378. @deffnx {Joy Operator} dig4 E D C B A @result{} D C B A E
  379. Reaches under @var{n} elements and ``digs'' up the next element to the
  380. top of the stack.
  381. @end deffn
  382. @deffn {Joy Operator} bury1 B A @result{} A B
  383. @deffnx {Joy Operator} bury2 C B A @result{} A C B
  384. @deffnx {Joy Operator} bury3 D C B A @result{} A D C B
  385. @deffnx {Joy Operator} bury4 E D C B A @result{} A E D C B
  386. Takes the topmost stack element and ``buries'' it under @var{n} elements.
  387. @end deffn
  388. @deffn {Joy Operator} flip2 B A @result{} A B
  389. @deffnx {Joy Operator} flip3 C B A @result{} A B C
  390. @deffnx {Joy Operator} flip4 D C B A @result{} A B C D
  391. Flips the order of the top @var{n} elements.
  392. @end deffn
  393. @deffn {Joy Operator} rollup C B A @result{} A C B
  394. Equivalent to @code{bury2}.
  395. @end deffn
  396. @deffn {Joy Operator} rolldown C B A @result{} B A C
  397. Equivalent to @code{dig2}.
  398. @end deffn
  399. @deffn {Joy Operator} rotate C B A @result{} A B C
  400. Equivalent to @code{flip3}.
  401. @end deffn
  402. @subsection List/Quotation Manipulation
  403. @deffn {Joy Operator} first [A0 @dots{}] @result{} A0
  404. Replaces the quotation at the top of the stack with its first element.
  405. @end deffn
  406. @deffn {Joy Operator} second [A0 A1 @dots{}] @result{} A1
  407. Replaces the quotation at the top of the stack with its second
  408. element.
  409. @end deffn
  410. @deffn {Joy Operator} third [A0 A1 A2 @dots{}] @result{} A2
  411. Replaces the quotation at the top of the stack with its third element.
  412. @end deffn
  413. @deffn {Joy Operator} rest [A0 A1 @dots{}] @result{} [A1 @dots{}]
  414. Replaces the quotation on the top of the stack with a copy of itself
  415. without its first element.
  416. @end deffn
  417. @subsection Quotation Evaluation
  418. These operators will cause the evaluation of one or more quotations.
  419. Using them, one can achieve something similar to anonymous function
  420. definitions. The effect most of these operators has on the stack
  421. depends on the contents of the quotation executed.
  422. @deffn {Joy Operator} i [A] @result{} @dots{}
  423. @deffnx {Joy Operator} dip0 [A] @result{} @dots{}
  424. Dequotes and executes the quotation at the top of the stack.
  425. @end deffn
  426. @deffn {Joy Operator} dip B [A] @result{} @dots{} B
  427. @deffnx {Joy Operator} dip1 B [A] @result{} @dots{} B
  428. Dequotes and executes the topmost quotation after temporarily removing
  429. @var{B} from the stack, then replaces @var{B} at the top of the stack.
  430. @end deffn
  431. @deffn {Joy Operator} dipd C B [A] @result{} @dots{} C B
  432. @deffnx {Joy Operator} dip2 C B [A] @result{} @dots{} C B
  433. Dequotes and executes the topmost quotation after temporarily removing
  434. @var{B} and @var{C} from the stack, then replaces them.
  435. @end deffn
  436. @deffn {Joy Operator} dip3 D C B [A] @result{} @dots{} D C B
  437. @deffnx {Joy Operator} dip4 E D C B [A] @result{} @dots{} E D C B
  438. Dequotes and executes the topmost quotation after temporarily removing
  439. the @var{n} stack elements below, then replaces them.
  440. @end deffn
  441. @deffn {Joy Operator} nullary [A] @result{} R
  442. Removes the topmost quotation, saves the state of the stack, then
  443. dequotes and executes the quotation, which leaves @var{R} on top of the
  444. stack. The saved stack is restored and @var{R} is pushed. That is,
  445. executing the quotation consumes no other stack elements.
  446. @end deffn
  447. @deffn {Joy Operator} unary B [A] @result{} R
  448. Removes the topmost quotation, saves the state of the stack, then
  449. dequotes and executes the quotation, which leaves @var{R} on the top
  450. of the stack. @var{R} is then pushed after restoring the saved stack
  451. and discarding the topmost element. That is, executing the quotation
  452. consumes exactly one stack element.
  453. @end deffn
  454. @deffn {Joy Operator} i2 D C [B] [A] @result{} @dots{}
  455. Executes @var{[B]} with @var{D} on top of the stack, then executes
  456. @var{[A]} with @var{C} on top of the result.
  457. @example
  458. 1 2 3 [+] [*] i2 .
  459. @result{} [9]
  460. @end example
  461. @end deffn
  462. @subsection General Operators
  463. @deffn {Joy Operator} branch C [B] [A] @result{} @dots{}
  464. If C is @code{true} this operator executes @var{[B]}, otherwise
  465. @var{[A]}.
  466. @end deffn
  467. @deffn {Joy Operator} ifte [I] [T] [E] @result{} @dots{}
  468. This operator saves the state of the stack then executes quoted
  469. predicate @var{[I]}. If the top of the stack is @code{true}, it then
  470. restores the stack and executes @var{[T]}, otherwise it restores the
  471. stack and executes @var{[E]}. In other words, the predicate @var{[I]}
  472. may manipulate the stack in any way, and it will be restored prior to
  473. executing @var{[T]} or @var{[E]}.
  474. @example
  475. 2 ['a 'b 'c]
  476. [size <] [pop 1 +] [['d] concat] ifte .
  477. @result{} [3]
  478. @end example
  479. @end deffn
  480. @deffn {Joy Operator} step [L] [A] @result{} @dots{}
  481. For each element in the list @var{[L]}, place it on top of the stack,
  482. then execute quotation @var{[A]}.
  483. @end deffn
  484. @subsection Recursion Operators
  485. Recursion may be achieved in Joy in the typical way by defining an
  486. operator that refers to itself in its body. It is, however, sometimes
  487. useful to define anonymous recursive operators. The following
  488. operators assist in doing this.
  489. @deffn {Joy Operator} tailrec [I] [T] [E] @result{} @dots{}
  490. Much like the @code{ifte} operator, this operator executes either
  491. @var{[T]} or @var{[E]} depending on the result of executing @var{[I]}
  492. but if @var{[E]} is executed this operator recurses.
  493. @example
  494. 0 [10 =] [] [dup 1 +] tailrec .
  495. @result{} [0 1 2 3 4 5 6 7 8 9 10]
  496. @end example
  497. @end deffn
  498. @deffn {Joy Operator} linrec [I] [T] [E1] [E2] @result{} @dots{}
  499. Like @code{tailrec} but upon completion of the recursive execution,
  500. executes @var{[E2]}.
  501. @example
  502. 1 [10 =] [] [dup 1 +] [*] linrec .
  503. @result{} [3628800]
  504. @end example
  505. @example
  506. [1 2 3] [null] [] [uncons] [cons] linrec .
  507. @result{} [[1 2 3]]
  508. @end example
  509. @example
  510. ['a 'b 'c 'd] [null] [pop 0] [rest 1 swap] [+] linrec .
  511. @result{} [4]
  512. @end example
  513. @end deffn
  514. @node Lisp Aliases
  515. @section Lisp Aliases
  516. Several operator aliases are provided for those familiar with
  517. programming in Lisp-like languages.
  518. @deffn {Joy Operator} car [A0 @dots{}] @result{} A0
  519. This is an alias for @code{first}.
  520. @end deffn
  521. @deffn {Joy Operator} cadr [A0 A1 @dots{}] @result{} A1
  522. This is an alias for @code{second}.
  523. @end deffn
  524. @deffn {Joy Operator} caddr [A0 A1 A2 @dots{}] @result{} A2
  525. This is an alias for @code{third}.
  526. @end deffn
  527. @deffn {Joy Operator} cdr [A0 A1 @dots{}] @result{} [A1 @dots{}]
  528. This is an alias for @code{rest}.
  529. @end deffn
  530. @deffn {Joy Operator} cddr [A0 A1 A2 @dots{}] @result{} [A2 @dots{}]
  531. This is equivalent to @code{rest rest}.
  532. @end deffn
  533. @deffn {Joy Operator} 1+ Z @result{} Z'
  534. This operator is equivalent to @code{succ}.
  535. @end deffn
  536. @deffn {Joy Operator} 1- Z @result{} Z'
  537. This operator is equivalent to @code{pred}.
  538. @end deffn
  539. @node Forth Aliases
  540. @section Forth Aliases
  541. Several operators are provided for those familiar with programming in
  542. the FORTH language. They may be accessed with:
  543. @example
  544. "forth" include .
  545. @end example
  546. @deffn {Joy Operator} drop A @result{}
  547. This is an alias for @code{pop}.
  548. @end deffn
  549. @deffn {Joy Operator} over B A @result{} B A B
  550. Push a copy of the second stack item.
  551. @end deffn
  552. @deffn {Joy Operator} nip B A @result{} A
  553. Remove the second item on the stack. Equivalent to @code{swap pop},
  554. and an alias for @code{popd}.
  555. @end deffn
  556. @deffn {Joy Operator} tuck B A @result{} A B A
  557. Insert a copy of the top stack item underneath the current second
  558. item. Equivalent to @code{swap over} or @code{dup bury2}.
  559. @end deffn
  560. @deffn {Joy Operator} rot C B A @result{} B A C
  561. An alias for @code{dig2} and @code{rolldown}. Not to be confused with
  562. @code{rotate}.
  563. @end deffn
  564. @deffn {Joy Operator} -rot C B A @result{} A C B
  565. An alias for @code{bury2}.
  566. @end deffn
  567. @deffn {Joy Operator} pick Xn @dots{} X0 n @result{} Xn @dots{} X0 Xn
  568. Retrieves a copy of the @var{n}@sup{th} stack item and places it on
  569. top of the stack. @code{0 pick} is equivalent to @code{dup}, @code{1
  570. pick} to @code{over}, etc.
  571. @end deffn
  572. @deffn {Joy Operator} /mod Z @result{} R Q
  573. This is an alias for @code{divmod}.
  574. @end deffn
  575. @deffn {Joy Operator} within X Y Z @result{} t/f
  576. Pushes @code{true} onto the stack if the integer inequality @var{Y}
  577. @leq{} @var{X} < @var{Z} holds, otherwise @code{false}.
  578. @end deffn
  579. @deffn {Joy Operator} within? X Y Z @result{} t/f
  580. Pushes @code{true} onto the stack if the integer inequality @var{Y}
  581. @leq{} @var{X} @leq{} @var{Z} holds, otherwise @code{false}.
  582. @end deffn
  583. @deffn {Joy Operator} cr A @result{} A
  584. Causes a newline to be printed to stdout. Equivalent to
  585. @code{newline}.
  586. @end deffn
  587. @deffn {Joy Operator} bl @result{} '\032
  588. Places a ``space'' character onto the stack.
  589. @end deffn
  590. @deffn {Joy Operator} emit A @result{}
  591. Print the character on top of the stack to stdout. Equivalent to
  592. @code{putch}.
  593. @end deffn
  594. @c *********************************************************************
  595. @node GNU Free Documentation License
  596. @appendix GNU Free Documentation License
  597. @include fdl-1.3.texi
  598. @bye
  599. @c Local Variables:
  600. @c ispell-local-dictionary: "american";
  601. @c End: