home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgambit-20-compiler-src-p1 / Examples / OPEN ME FIRST! < prev    next >
Encoding:
Text File  |  1994-07-26  |  5.6 KB  |  213 lines  |  [TEXT/gamI]

  1. MacGambit v2.0
  2. --------------
  3.  
  4. * The documentation is now available through the online help system (in the
  5.   apple menu).
  6.  
  7. * Check the "Examples" folder for Scheme code examples and demos.
  8.  
  9. * Here is a transcript of a typical interaction with MacGambit (comments are
  10.   marked with ";;;"):
  11.  
  12.  
  13. MacGambit (v2.0)
  14.  
  15. : (define (fact x)
  16.     (cond ((= x 0) 1)
  17.           ((= x 1) 1)
  18.           ((> x 1) (* (fact (- x 1)) x))
  19.           (else    (error "argument must be positive"))))
  20. fact
  21.  
  22. : (trace fact)  ;;; let's check what fact is doing
  23. fact
  24.  
  25. : (fact 10)
  26. Entry (fact 10)
  27. |Entry (fact 9)
  28. | Entry (fact 8)
  29. |  Entry (fact 7)
  30. |  |Entry (fact 6)
  31. |  | Entry (fact 5)
  32. |  |  Entry (fact 4)
  33. |  |  |Entry (fact 3)
  34. |  |  | Entry (fact 2)
  35. |  |  |  Entry (fact 1)
  36. |  |  |  ==> 1
  37. |  |  | ==> 2
  38. |  |  |==> 6
  39. |  |  ==> 24
  40. |  | ==> 120
  41. |  |==> 720
  42. |  ==> 5040
  43. | ==> 40320
  44. |==> 362880
  45. ==> 3628800
  46. 3628800
  47.  
  48. : (untrace fact)  ;;; remove tracing of fact
  49. fact
  50.  
  51. : (let ((x (runtime))) (let ((y (fact 50))) (cons (- (runtime) x) y)))
  52. (.02 . 30414093201713378043612608166064768844377641568960512000000000000)
  53.  
  54. : (pp fact)
  55. (lambda (x)
  56.   (cond ((= x 0) 1)
  57.         ((= x 1) 1)
  58.         ((> x 1) (* (fact (- x 1)) x))
  59.         (else (error "argument must be positive"))))
  60. #f
  61.  
  62. : (define f fact)   ;;; save value of fact in f
  63. f
  64.  
  65. : (define fact "not the factorial function")
  66. fact
  67.  
  68. : (f 5)
  69. *** ERROR IN f -- Operator is not a PROCEDURE
  70. (fact (- x 1))
  71.  
  72. 1:  ;;; typed: <cmd>-B
  73. 0    f                         (fact (- x 1))
  74. -1   (top level)               (f 5)
  75. -2   ##dynamic-env-bind
  76. -3   ##read-eval-print
  77. -4   ##dynamic-env-bind
  78. -5   ###_kernel.startup
  79.  
  80. 1:  ;;; typed: <cmd>-I
  81. #[procedure f] =
  82. (lambda (x)
  83.   (cond ((= x 0) 1)
  84.         ((= x 1) 1)
  85.         ((> x 1) (* (fact (- x 1)) x))
  86.         (else (error "argument must be positive"))))
  87.  
  88. 1:  ;;; typed: <cmd>-L
  89. x = 5
  90.  
  91. 1: (- x 1)   ;;; we can use x and it will refer to the local x (f's argument)
  92. 4
  93.  
  94. 1: fact   ;;; check value of fact
  95. "not the factorial function"
  96.  
  97. 1: (set! fact f)
  98. #[undefined]
  99.  
  100. 1:  ;;; typed: <cmd>-R  (we want to resume from the error)
  101. Return value: (fact (- x 1))   ;;; this is the value to return
  102. 120
  103.  
  104. : (list (gensym) (gensym) (gensym))
  105. (g1 g2 g3)
  106.  
  107. : (put 'bob 'eyes 'blue)
  108. #f
  109.  
  110. : (put 'mary 'hair 'blond)
  111. #f
  112.  
  113. : (get 'bob 'eyes)
  114. blue
  115.  
  116. : (get 'mary 'eyes)
  117. #f
  118.  
  119. : (define (series term)          ;;; Concurrency is expressed with FUTUREs
  120.     (let ((sum 0) (stop #f))     ;;; as is done in Multilisp
  121.       (FUTURE (let loop ((i 0))
  122.                 (if (not stop)
  123.                   (begin (set! sum (+ sum (term i))) (loop (+ i 1))))))
  124.       (lambda (msg)
  125.         (cond ((eq? msg 'value) sum)
  126.               ((eq? msg 'stop)  (set! stop #t))
  127.               (else             (error "unknown message" msg))))))
  128. series
  129.  
  130. : (define pi  ;;; start a task to compute series expansion for pi
  131.     (series (lambda (i) (/ 4. ((if (odd? i) - +) (+ (* i 2) 1))))))
  132. pi
  133.  
  134. : (pi 'value)   ;;; get current value of series
  135. 3.141419882340216
  136.  
  137. : (pi 'value)   ;;; again... it has changed!
  138. 3.1415194471477133
  139.  
  140. : (pi 'value)
  141. 3.1416300745380195
  142.  
  143. : (pi 'stop)   ;;; now we kill the task
  144. #[undefined]
  145.  
  146. : (pi 'value)
  147. 3.1415815090449892
  148.  
  149. : (pi 'value)
  150. 3.1415815090449892
  151.  
  152. : (load "pi")   ;;; try computing pi another way...
  153. How many digits of pi do you want (0 to exit): 50
  154. 3.14159265358979323846264338327950288419716939937510
  155. How many digits of pi do you want (0 to exit): 0
  156. "pi.scm"
  157.  
  158. : (queens 8 #t)   ;;; Ooops, queens is not defined...
  159. *** ERROR -- Unbound variable: queens
  160.  
  161. 1:   ;;; typed: <cmd>-D
  162. : (load "queens")   ;;; load it!
  163. "queens.O"
  164.  
  165. : (queens 8 #t)  ;;; try it out
  166. (4 2 7 3 6 8 5 1)
  167. (5 2 4 7 3 8 6 1)
  168. (3 6 4 2 8 5 7 1)
  169. (3 5 2 8 6 4 7 1)
  170. (5 7 1 3 8 6 4 2)
  171. (4 6 8 3 1 7 5 2)  ;;; typed: <cmd>-. to interrupt computation
  172.  
  173. *** INTERRUPT
  174.  
  175. 1:   ;;; typed: <cmd>-D
  176. : (load "tree")
  177. "tree.scm"
  178.  
  179. : (tree-display (call-with-input-file "fact.scm" read))
  180.               .                                             
  181.    .----------------------.                                 
  182.    .                      .                                 
  183. define     .-----------------------------.                  
  184.            .                             .                  
  185.          .---.         .-----------------------------------.
  186.          .   .         .                                   .
  187.        fact .--.  .----------.                            ()
  188.             .  .  .          .                              
  189.             n () if  .----------------.                     
  190.                      .                .                     
  191.                     .--.     .-----------------.            
  192.                     .  .     .                 .            
  193.                     < .--.   1        .-----------------.   
  194.                       .  .            .                 .   
  195.                       n .--.   .-------------.         ()   
  196.                         .  .   .             .              
  197.                         2 ()   *       .-----------.        
  198.                                        .           .        
  199.                                    .-------.      .--.      
  200.                                    .       .      .  .      
  201.                                  fact  .--------. n ()      
  202.                                        .        .           
  203.                                       .--.     ()           
  204.                                       .  .                  
  205.                                       - .--.                
  206.                                         .  .                
  207.                                         n .--.              
  208.                                           .  .              
  209.                                           1 ()              
  210. #[undefined]
  211.  
  212. :   ;;; typed: <cmd>-Q
  213.