home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol166 / demo.lsp < prev    next >
Encoding:
Text File  |  1984-04-29  |  2.1 KB  |  85 lines

  1.  
  2. [DEMO.LSP]
  3. [collection of LISP demonstration programs]
  4. [December 31, 1980]
  5. [December 28, 1983]
  6.  
  7. [[
  8. A collection of LISP demonstration programs:
  9.      (merge (1 2 3) (a b c)) produces (1 a 2 b 3 c)
  10.      (reverse (1 2 3 4 5)) produces (5 4 3 2 1)
  11.      (split (1 a 2 b 3 c)) produces ((1 2 3) (a b c))
  12.      (split* (1 a 2 b 3 c)) produces the same result
  13.      (binsum (1 1 1 0) (1 1)) produces (1 0 0 0 1)
  14. To execute one of these programs, type its name, then
  15. a carriage return. Then type its arguments, in response
  16. to the prompts.
  17. ]]
  18.  
  19.  
  20.  
  21. [calculate a binary sum - use reversed digits]
  22. (b ((lambda (0 1) (r (c (r 0 (list)) (r 1 (list))) (list)) )))
  23.  
  24. [binary sum - sum low order bits, then rest]
  25. (c ((lambda (0 1) (cond
  26.     ((null 0) 1)
  27.     ((null 1) 0)
  28.     ((and) (cons (d (car 0) (car 1))
  29.          (c (list (e (car 0) (car 1))) (c (cdr 0) (cdr 1))) ))
  30.     ))))
  31.  
  32. [sum of two bits]
  33. (d ((lambda (0 1) (cond
  34.     ((eq 0 (quote 0)) 1)
  35.     ((eq 1 (quote 0)) 0)
  36.     ((and) (quote 0))
  37.     ))))
  38.  
  39. [carry bit]
  40. (e ((lambda (0 1) (cond
  41.     ((eq 0 (quote 0)) (quote 0))
  42.     ((eq 1 (quote 0)) (quote 0))
  43.     ((and) (quote 1))
  44.     ))))
  45.  
  46. [make a list of two elements]
  47. (l ((lambda (0 1) (cons 0 (cons 1 (quote ()))))))
  48.  
  49. [merge two lists]
  50. (m ((lambda (0 1) (if (null 0) 1
  51.           (if (null 1) 0
  52.           (cons (car 0) (cons (car 1) (m (cdr 0) (cdr 1))))
  53.           )))))
  54.  
  55. [reverse a list]
  56. (r ((lambda(0 1)(if(eq 0 (quote ()))1(r(cdr 0)(cons(car 0)1)))) ))
  57.  
  58. [split a list into odds and evens]
  59. (s ((lambda (0 1 2) (if (null 0) (l 1 2)
  60.             (if (null (cdr 0)) (l (cons (car 0) 1) 2)
  61.                 (s (cddr 0) (cons (car 0) 1)
  62.                (cons (cadr 0) 2))
  63.             )))))
  64.  
  65. [split a list into odds and evens - variant]
  66. (t ((lambda (0) (if (null 0) (l 0 0)
  67.          (if (null (cdr 0)) (l 0 (quote ()))
  68.          ((lambda (1) (l (cons (car 0) (car 1))
  69.                  (cons (cadr 0) (cadr 1))
  70.                  )) (t (cddr 0)))
  71.          )))))
  72.  
  73. [main program]
  74. (* ((lambda(0)
  75.     (if (eq 0 (quote merge)) (m (read) (read))
  76.     (if (eq 0 (quote reverse)) (r (read) (quote()))
  77.     (if (eq 0 (quote split)) (s (read) (quote ()) (quote ()))
  78.     (if (eq 0 (quote split*)) (t (read))
  79.     (if (eq 0 (quote binsum)) (b (read) (read))
  80.     (quote (wrong option))
  81.     )))))
  82.     )))
  83.  
  84. [end]
  85.