-
Notifications
You must be signed in to change notification settings - Fork 5
/
collapse.lisp
79 lines (60 loc) · 2 KB
/
collapse.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
;;; Lisp 1 vs. Common Lisp vs. Scheme vs. Emacs Lisp
;;; Rainer Joswig, joswig@lisp.de, 2012
;;; The original code is from
;;; THE LISP 1 PROGRAMMER'S MANUAL FROM 1960, PAGE 99FF
; the Lisp 1 Programmer's manual is the first manual for the first Lisp implementation.
; DEFINE then was the operator to define new function.s
; With minor rewriting the code runs largely unchanged in Common Lisp.
; That's what makes Common Lisp one of the main Lisp dialects:
;
; it still contains at its core the original functionality of LISP from 1960.
;
; This makes it possible to understand code from decades back to the 1960s.
;
; One can see below that both Emacs Lisp and Scheme are two other languages
; which can express the same program also with only minor changes.
#|
; THE LISP 1 PROGRAMMER'S MANUAL FROM 1960, PAGE 99FF:
; http://bitsavers.org/pdf/mit/rle_lisp/LISP_I_Programmers_Manual_Mar60.pdf
; Lisp 1 used DEFINE to define functions. The comma character was whitespace
; between the list elements.
DEFINE
(((COLLAPSE,(LAMBDA,(L),(COND,
((ATOM,L),(CONS,L,NIL))
((NULL,(CDR,L)),
(COND,((ATOM,(CAR,L)),L),(T,(COLLAPSE,(CAR,L)))))
(T,(APPEND,(COLLAPSE,(CAR,L)),(COLLAPSE,(CDR,L)))))
))))))
|#
; THE SAME, JUST REFORMATTED, IN COMMON LISP:
(DEFUN COLLAPSE (L)
(COND
((ATOM L) (CONS L NIL))
((NULL (CDR L))
(COND ((ATOM (CAR L)) L)
(T (COLLAPSE (CAR L)))))
(T (APPEND (COLLAPSE (CAR L))
(COLLAPSE (CDR L))))))
#|
CL-USER > (COLLAPSE '(((A B) ((C))) ((D (E F)) (G) ((H)))))
(A B C D E F G H)
|#
; Scheme
(define collapse
(lambda (l)
(cond
((atom? l) (cons l '()))
((null? (cdr l))
(cond ((atom? (car l)) l)
(else (collapse (car l)))))
(else (append (collapse (car l))
(collapse (cdr l)))))))
; Emacs Lisp
(defun collapse (l)
(cond
((atom l) (cons l nil))
((null (cdr l))
(cond ((atom (car l)) l)
(t (collapse (car l)))))
(t (append (collapse (car l))
(collapse (cdr l))))))