As an example of R programming style and of many of the user-level constructs described above, here is the first significant test program, sch.r, in its entirety. The character `` ;'' indicates a comment that runs to the end of the line.
This program simulates the quantum-mechanical behavior of an electron oscillating at near the speed of light in a 1-dimensional parabolic potential well about 1 Å ngstrom ( m) wide. It takes about 1 minute to complete each second long simulation step under the PENDVM Pendulum virtual machine emulation program, running on a SPARC 20.
An interesting feature of this program is that although it is perfectly reversible, its outer loop can run for indefinitely long periods, without either slowing down or filling up the memory with garbage data.
The current version of the compiler successfully compiles this program to correct (though not optimally efficient) Pendulum code. When run, the compiled program produces exactly the correct output.
;;;--------------------------------------------------------- ;;; ;;; Schroedinger Wave Equation simulation program. ;;; The first major test of R (the reversible language)! ;;; ;;;--------------------------------------------------------- ;;; Currently all data must come before the code that uses ;;; it, so that the compiler will recognize these ;;; identifiers as names of static data items rather than as ;;; dynamic variables. ;; epsilon = hbar*dt/m*dx^2. DX=7.8125e-13m, DT=5e-22s (defword epsilon 203667001) ; 0.0948398 radians. ;; Parabolic potential well with 128 points. (defarray alphas 458243442 456664951 455111319 453582544 452078627 450599569 449145369 447716027 446311542 444931917 443577149 442247239 440942188 439661994 438406659 437176182 435970563 434789802 433633899 432502854 431396668 430315339 429258869 428227257 427220503 426238607 425281569 424349389 423442068 422559605 421701999 420869252 420061363 419278332 418520159 417786845 417078388 416394790 415736049 415102167 414493143 413908977 413349669 412815220 412305628 411820895 411361019 410926002 410515843 410130542 409770099 409434515 409123788 408837920 408576909 408340757 408129463 407943027 407781450 407644730 407532868 407445865 407383720 407346432 407334003 407346432 407383720 407445865 407532868 407644730 407781450 407943027 408129463 408340757 408576909 408837920 409123788 409434515 409770099 410130542 410515843 410926002 411361019 411820895 412305628 412815220 413349669 413908977 414493143 415102167 415736049 416394790 417078388 417786845 418520159 419278332 420061363 420869252 421701999 422559605 423442068 424349389 425281569 426238607 427220503 428227257 429258869 430315339 431396668 432502854 433633899 434789802 435970563 437176182 438406659 439661994 440942188 442247239 443577149 444931917 446311542 447716027 449145369 450599569 452078627 453582544 455111319 456664951) ;; This is the shape of the initial wavefunction; amplitude ;; doesn't matter. ;; Real part. (defarray psiR 2072809 3044772 4418237 6333469 8968770 12546502 17338479 23669980 31921503 42527251 55969298 72766411 93456735 118573819 148615999 184009768 225068513 271948808 324607187 382760978 445857149 513053161 583213481 654924586 726530060 796185813 861933650 921789572 973841548 1016350163 1047844835 1067208183 1073741824 1067208183 1047844835 1016350163 973841548 921789572 861933650 796185813 726530060 654924586 583213481 513053161 445857149 382760978 324607187 271948808 225068513 184009768 148615999 118573819 93456735 72766411 55969298 42527251 31921503 23669980 17338479 12546502 8968770 6333469 4418237 3044772 2072809 1393998 926112 607804 394060 252382 159681 99804 61622 37586 22647 13480 7926 4604 2642 1497 838 463 253 136 73 38 20 10 5 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) ;;Imaginary part. (defarray psiI 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) ;; This subroutine changes a point in the real wave DEST ;; according to the curvature in the corresponding ;; neighborhood in the real wave SRC, and the potential at ;; the given point. (defsub pfunc (dest src i alphas epsilon) ((dest _ i) += ((alphas _ i) */ (src _ i))) ((dest _ i) -= (epsilon */ (src _ ((i + 1) & 127)))) ((dest _ i) -= (epsilon */ (src _ ((i - 1) & 127))))) ;; Take turns updating the two components of the wave in a ;; way such that they will chase each other around in ;; (higher-dimensional) circles. (defsub schstep (psiR psiI alphas epsilon) ;; psiR += f(psiI) (for i = 0 to 127 ;; psiR[i] += pfunc(psiI,i) (call pfunc psiR psiI i alphas epsilon)) ;; psiI -= f(psiR) (for i = 0 to 127 ;; psiI[i] -= pfunc(psiR,i) (rcall pfunc psiI psiR i alphas epsilon))) ;; Print the current wave to the output stream. (defsub printwave (wave) (for i = 0 to 127 (printword (wave _ i))) (println)) ;; Main program, goes by the name of SCHROED. (defmain schroed (for i = 1 to 1000 ;Time for electron to fall to well bottom. (call schstep psiR psiI alphas epsilon) ;; Print both wave components. (call printwave psiR) (call printwave psiI)))