5 ms·
An early Lisp program (circa Feb. 1960)
- fogus 16y ago(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_program_circa_feb_1960/ http://www.reddit.com/r/lisp/comments/fpfnf/an_early_lisp_pr...
- mahmud 16y agoBetter known as FLATTEN.
- sjs 16y agoNo 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.
- S_A_P 16y agoThat 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?
- lispm 16y agoIf you still use punch cards (this Lisp code was written in punch cards) to feed programs into your computer, then it is a good way. If not, you can use a text editor and format your code differently.
- weavejester 16y agoNot 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 16y agoDidn't know about tree-seq. Cool. I suspect I will find a lot of uses for it, now that I know about it.
- rayiner 16y agoIt's scary because it's old and ALL IN CAPS. Modern Lisp code is much easier to read, especially in a proper editor with syntax highlighting and braces matching. Eg, fib in C: int fib(int n) { if(n < 2) return n; return fib(n - 1) + fib(n - 2); } (defun fib (n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) The parens aren't really that annoying once you get used to them, and I personally find them a lot less distracting than the profusion of commas and curly braces that C-syntax languages use in their place. Eg: {{"apples", RED}, {"bananas", YELLOW}, {"oranges", ORANGE}} '(("apples" RED) ("bananas" YELLOW) ("oranges" ORANGE)) Also, don't forget that abstraction makes everything more readable: (dolist (var '(1 2 3 4)) (princ a)) for(list::iterator itr = list.begin(); itr != list.end(); ++itr) { cout << *itr; }
- derleth 16y agoTo be excruciatingly explicit: That code is 51 years old. Nobody writing Lisp now writes code that looks like that. Lisp environments are no longer based around punch cards and computers the size of a row of filing cabinets with the computational power of a fifteen-year-old cell phone. Trying to judge what Lisp is from that code is akin to trying to judge what airplanes are from something the Wright brothers built in their bicycle shop.
- daniel-cussen 16y agoThe 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 16y agoMy 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 16y agoCommon Lisp has MAPCAN: (defun flatten (list) (mapcan (lambda (item) (if (listp item) (flatten item) (list item))) list))
- brlewis 16y ago“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.
- zandorg 16y agoI 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.
- jaltucher 16y agoInteresting to note that the first ".com" domain was symbolics.com I used a symbolics machine to code list back in the 80s.