
An early Lisp program (circa Feb. 1960) - fogus
http://funcall.blogspot.com/2011/02/early-lisp-program.html
======
fogus
(Stolen) from Rainer Joswig's post[1] at Reddit:

Modern version:

    
    
        (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))))))
        
        (collapse '(((A B) ((C))) ((D (E F)) (G) ((H)))))
        ;=> (A B C D E F G H)
    

[1]:
[http://www.reddit.com/r/lisp/comments/fpfnf/an_early_lisp_pr...](http://www.reddit.com/r/lisp/comments/fpfnf/an_early_lisp_program_circa_feb_1960/)

~~~
mahmud
Better known as FLATTEN.

------
brlewis
_“The commas in writing S-expressions may be omitted. This is an accident.”_

I'm grateful for this accident. The absence of commas is one of the key
features that led me to see s-expressions as mapping very nicely into web
templates.

------
daniel-cussen
The modern version of this function, from _On Lisp_ :

    
    
      (defun flatten (x)
        (labels ((rec (x acc)
    	       (cond ((null x) acc)
    		     ((atom x) (cons x acc))
    		     (t (rec (car x) (rec (cdr x) acc))))))
        (rec x nil)))

~~~
fogus
My favorite (although not the most efficient) Clojure version of this is:

    
    
        (defn flat [s]
          (if (sequential? s)
            (apply concat (map flat s))
            (list s)))
        
        (flat '((((1 2 3)) (4 5)) (((((((6 7 [8 9])))))) a b c) {:a 1 :b 2}))
        
        ;=> (1 2 3 4 5 6 7 8 9 a b c {:a 1, :b 2})

~~~
lispm
Common Lisp has MAPCAN:

    
    
       (defun flatten (list)
          (mapcan (lambda (item)
                    (if (listp item) (flatten item) (list item)))
                  list))

------
zandorg
I was confused by this: "The output from the program contains these two lines:
APPLY OPERATOR AS OF SEPTEMBER 1, 1959 THE TIME IS NOW 2/16 1139.5"

It made it sound like the program outputs those 2 lines, which would win any C
obfuscation contest.

------
S_A_P
That code scares me. Someone please tell me there is a way to write lisp that
doesn't make me want to shoot myself in the head. Is this representative of
"proper" lisp syntax?

~~~
weavejester
Not really. I find I very rarely write functions with explicit loops, because
there is usually a higher-level function that encompasses what I want to do.

For example, in Clojure I'd write the flatten function as:

    
    
        (defn flatten [x]
          (remove sequential? (tree-seq sequential? seq x)))
    

Other lisps probably have similar functions.

~~~
jimbokun
Didn't know about tree-seq. Cool. I suspect I will find a lot of uses for it,
now that I know about it.

------
sjs
No wonder Lisp got a bad rep for excess parens. I think Lisp is awesome, but I
would never want to write a significant amount of that code.

~~~
malkia
This looks like the M[] syntax, rather than the current s-exp one.

~~~
lispm
It is the s-expression variant with comma as whitespace.

------
jaltucher
Interesting to note that the first ".com" domain was symbolics.com

I used a symbolics machine to code list back in the 80s.

