home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / LISP / PDLISP.ZIP / EXAMPLES.L < prev    next >
Encoding:
Text File  |  1986-02-13  |  2.2 KB  |  95 lines

  1. ; examples.l - miscellaneous examples
  2. ;
  3. ;
  4. ; rfib - recursive fibonacci function.
  5. (def rfib (lambda (n)
  6.      (cond ((< n 2) 1)
  7.            (t (+ (rfib (- n 1)) (rfib (- n 2)))))))
  8.  
  9. ; tfib - one implementation of fibonacci function.
  10. ; don't use EQ here, or the computation will never terminate.
  11. ;
  12. (defun tfib (x)
  13.     (cond ( (eql x 1) 1)
  14.         ((eql x 2) 1)
  15.         (t (+ (tfib (- x 1))
  16.               (tfib (- x 2))))))
  17. ;
  18. ; ifib - iterative fibonacci function.
  19. ;
  20. ; not done yet - would anybody like to contribute one ?
  21.  
  22. ;
  23. ; ipower - iteratively coded n ^ mth power routine
  24. ; m and m must both be integers.
  25. (def ipower (lambda (m n)
  26.     (prog ((result 1) (exponent n))
  27. ;
  28. ; first, some checking of the parameters:
  29. ;
  30.     (cond
  31.         ((not (integerp m))
  32.         (return (err "***> IPOWER: 1st arg must be an integer")))
  33.         ((not (integerp n))
  34.         (return (err "***> IPOWER: 2nd arg must be an integer"))))
  35. ;
  36. ; main loop:
  37. ;
  38. ploop    (cond ((zerop exponent) (return result)))    ;test
  39.         (setq result (* m result))            ;reset
  40.         (setq exponent (- exponent 1))            ;reset
  41.         (go ploop))))                                ;repeat
  42. ;
  43. ;
  44. ; do_ipower - same as ipower, above,
  45. ; except coded with a DO instead of a PROG.
  46. (def do_ipower (lambda (m n)
  47.     (do
  48.     ((result 1) (exponent n))    ;INIT part of DO
  49.     ((zerop exponent) result)    ;TEST part of DO
  50. ;
  51. ; rest of the function is the BODY of the DO:
  52. ;
  53.     (setq result (* m result))
  54.     (setq exponent (- exponent 1)))))
  55. ;        
  56. ;
  57. ; ifact - iteratively coded factorial
  58. ;
  59. (def ifact (lambda (e)
  60.     (prog (result count)
  61.         (setq result 1)            ;initialize result
  62.         (setq count e)            ;initialize e
  63.  
  64.     ploop
  65.         (cond
  66.         ((<= count 0) (return result)))        ;exit test
  67.         (setq result (* result count))        ;accumulate result
  68.         (setq count (- count 1))        ;reset
  69.         (go ploop))))                ;repeat
  70.  
  71. ;
  72. ; rfact - recursively coded factorial function
  73. ;
  74. (def rfact (lambda (x)
  75.     (cond
  76.     ((zerop x) 1)
  77.     (t (* x (rfact (- x 1)))))))
  78. ;
  79. ; rev & app - slow versions of reverse and append which appeared
  80. ; in BYTE, july 1984, p 288.
  81. ;
  82. (def rev (lambda (x)
  83.     (cond
  84.     ((null x) nil)
  85.     (t (app (rev (cdr x)) (list (car x)))) ) ))
  86. ;
  87. (def app (lambda (x y)
  88.     (cond
  89.     ((null x) y)
  90.     (t (app (rev (cdr (rev x))) (cons (car (rev x)) y))))))
  91. ;
  92. ;
  93. ; end of file