5 ms·
Yep, that's where I stopped. What I think is going on... Some people think predominantly in abstraction and don't really care about syntax. As Paul Graham once
by phkahler 8y ago
Yep, that's where I stopped. What I think is going on... Some people think predominantly in abstraction and don't really care about syntax. As Paul Graham once said, LISP doesn't really have syntax - you write parse trees directly. That may be fine for people who have achieved that level/style of programming, but it's a tiny minority.
Why should I write (+ p 2) when most people think (p+2)? This difference gets worse with more complex expressions. For the masses (and I mean a lot of very capable people) this is just garbage. For some it's wonderful. I can't say it's wrong, just not my thing. Perhaps it's the Blub paradox - I've never had the LISP epiphany so I can't say. I have had the python epiphany, and it's quite the opposite of this.
- nerdponx 8y agoI like Lisps but I still don't like this. This is like the Perl of Lisps. To my eye, this is not a parse tree, it's syntax vomit on top of a parse tree.
- lispm 8y agoIf you have lots of math expressions to write, you can use an infix reader in Common Lisp. It has a programmable reader. Such a thing - reading infix expressions - was already a standard feature of the MIT Lisp Machine OS in the 80s. In Lisp you actually don't write parse trees. Lisp uses a serialized hierarchical data format of a tokenizer. S-expressions are not Lisp syntax trees, but a data format.
- qop 8y agoI relish comments like these from Lisp fans that actually KNOW Lisp. Someday we'll resurrect it all... Reader macros blew my mind when I first read about them. Being able to change the very laws of the universe from within my simple little programs... Insane!
- kbp 8y ago> LISP doesn't really have syntax - you write parse trees directly. Lisp has plenty of syntax, it's just extensible and largely defined in operators. Commonly used macros like DEFUN, WITH-OPEN-FILE, and LOOP all add their own syntax to the language; that's what macros are for. When you put them together, typical Lisp code ends up looking like (defun count-words (filespec) (with-open-file (f filespec) (loop with counts = (make-hash-table :test 'equal) for line = (read-line f nil) while line do (loop for word in (cl-ppcre:split "\\s+" line) do (incf (gethash word counts 0))) finally (return counts)))) Which I don't think is that alien. It translates pretty cleanly into Python: def count_words(filespec): with open(filespec) as f: counts = collections.defaultdict(int) for line in f: for word in line.split(): counts[word] += 1 return counts Do you find the former significantly harder to understand? I don't see how it's any more like "writing a parse tree directly." > Why should I write (+ p 2) when most people think (p+2)? This difference gets worse with more complex expressions. As mentioned in other comments, there are infix packages available for when you're writing math-heavy code (or if you just need infix operators), but I also think that a lot of people don't write very much code with complicated inline arithmetic in it, and overstate how much the awkward math syntax would affect them (there are of course plenty of projects that are math-heavy and where it does make a large difference). When you're defining variables/functions/classes/etc or calling functions or looping or whatever, there's not much difference between Lisp and most other languages aside from whether the bracket is { or ( and which side of the keyword it goes on; in my experience, that sort of code tends to make up the large bulk of most codebases in most languages. edit: Another place where Lisp's default syntax is significantly different from a lot of languages is something like object.f(x).g(y).h(z), which in Lisp looks like (h (g (f object x) y) z). This can be remedied with common macros like -> that let you write (-> object (f x) (g y) (h z)).
- deleted 8y ago[deleted]
- tigershark 8y agoBoth your snippets are exceedingly long, verbose and painful to understand. In C#, that normally is a quite verbose language, you can write just this: IDictionary<string, int> CountWords(string f) => File.ReadAllText(f).Split().ToLookup(w => w).ToDictionary(kv => kv.Key, kv => kv.Count()); Or a bit more concise in F#: let wordsCount f = File.ReadAllText(f).Split() |> Array.groupBy id |> Array.map (fun x |> fst x, (snd x).Length) |> dict
- kbp 8y agoSure, there's other ways to write it, the point of the post was Lisp's syntax, not the algorithm I used to demonstrate some features of it. You could write examples like yours in Lisp or Python, too. As well, your examples both slurp the whole file into memory, which mine avoided. edit: And your C# version is only 24 non-whitespace characters shorter than the Python version, and 20 of those are because you called your variables 'f' instead of 'filespec' and 'w' instead of 'word'. A 4 non-whitespace character difference makes it exceedingly long and verbose? Or are you just advocating 1-letter variable names and avoiding newlines?
- tigershark 8y agoThe c# version is calling only 4 methods and in C# you obviously have to declare the types. It’s the difference between declarative style versus imperative that I wanted to highlight, obviously Python is more compact than C#, but written in that way it becomes more verbose and more difficult to read and write. Write something like that in Lisp and let’s see how it compares.
- deleted 8y ago[deleted]
- kazinator 8y agoIsn't "kv => kv.Key" a lambda function that is called?
- rezistik 8y agoWould you feel weird writing add(a, 2)? or (add 2 3)?
- phkahler 8y agoI might go for sum(2,3)
- axle_512 8y agoIn lisp, nearly everything is written in the same form. I.e. everything either is a function call or looks like one. Once you know that, you pretty much know lisp syntax. People freak out about the parens. But I think what's the big deal? Functions are called with the paren in front of the function and the args space separated. so f(x, y) in C like syntax becomes: (f x y) - Whoop dee do, not so hard. And since the syntax is extremely consistent, things that are math "operators" in other langs are just functions in lisp. For "(+ 1 2 3)" + is actually the name of the function, so we are invoking + function and 1 2 3 are the arguments. Returns 6. Once you know that, and that function args are evaluated before passed into the function, then you no longer have to worry about operator precedence and parenthesis to control order of operations. Once you get used to it, it's actually a lot simpler. (* 2 (+ 5 5)) => 20
- lispm 8y ago> In lisp, nearly everything is written in the same form. I.e. everything either is a function call or looks like one. Once you know that, you pretty much know lisp syntax. Actually not. Lisp has several types of forms. A function call is one. There are atleast two others: macro forms and special forms. function form with + (+ 1 2) macro form with DEFUN. The syntax for DEFUN is: defun function-name lambda-list [[declaration* | documentation]] form* The function name the is either a symbol or a list of (setf name). The lambda-list has complex syntax with optional, aux, rest and keyword arguments, declaration has a complex declaration syntax, etc... The third type of form would be built-in syntax. LET is an example. The syntax for LET is: let ({var | (var [init-form])}*) declaration* form* => result* This means that (let ((a 1) b) (setf b a)) is a valid program. This is not a valid Lisp program, which Lisp will complain about: * (let ((a 1 2) b) (setf b a)) ; in: LET ((A 1 2) B) ; (A 1 2) ; ; caught ERROR: ; The LET binding spec (A 1 2) is malformed. Reason: Lisp expects that bindings are either symbols, lists with a single symbol or pairs with a symbol and a value form. (a 1 2) thus is not valid. Is Lisp syntax easy? Once you look deeper, it actually isn't. Only a core language with only function calls is relatively easy. * Lisp writes programs on top of a data structure called s-expressions. S-expressions are easy, Lisp not. * Lisp usually has the operator as the first element of a lisp -> uniform appearance. * Lisp has some built-in syntax. Usually as little as possible. * Lisp lets the developer add new syntax via macros -> zillions of macros and the user needs to learn a few patterns to understand Lisp code.
- AnimalMuppet 8y agoFirst of all, the Blub paradox is wrong. It assumes that languages can be ranked on a one-dimensional axis labeled "power". That assumption is wrong. To see why it's wrong, think about Lisp and Haskell. Users of both languages are sure that they're at the top of the power curve, they're sure that they're looking down when they look at the other language, and they're sure why they're looking down. "How can you get anything done in [Haskell|Lisp]? It doesn't even have [macros|a decent type system]!" But if both are sure they're looking down, then languages can't be well-ordered by "power". Next, that syntax. I suspect that that kind of syntax "clicks" with some people, and not with others. (Almost everyone could learn it, but that's not the same thing.) And I think you're right that "the masses" - the vast majority of people - are people to whom it doesn't "click". This may be the real flaw of Lisp (and Haskell) - the syntax is just wrong for the large majority of programmers. Note well: This is my pet theory. I have no data. All predictions guaranteed wrong or your money back.
- nerdponx 8y agoIMO the problem with Haskell isn't syntax, it's jargon. Not only is the jargon dense, heady, and ubiquitous, but (in my limited understanding) it also doesn't necessarily correspond cleanly to math concepts of the same name. I guess you could say that jargon is just another form of syntax.
- AnimalMuppet 8y agoWell, I thought about saying that with Haskell, it was semantics at least as much as syntax. By that, I meant what you meant, but I also meant more: I wonder if functional programming itself is a poor match to the way that most programmers think, and not just because they are untrained on FP. But I didn't say that, because I thought it was a bit of a digression to my point, which was already a digression on phkahler's point, and the digressing has to end somewhere...
- phkahler 8y agoIt's all good. I've rather enjoyed reading the responses. IMHO this type of thing is what really makes languages appealing or not too different people.
- tytytytytytytyt 8y ago> Why should I write (+ p 2) when most people think (p+2)? That's your problem with that code snippet?? Prefix or whatever isn't so bad. I'm more worried about how the rest of the snippet works.
- nikofeyn 8y ago> Why should I write (+ p 2) when most people think (p+2)? just an observation. most people don’t think this naturally. most people learned (p+2). maybe there’s an argument for whatever is more natural, but the point is that people forget that what they view as natural was once unnatural and had to be learned. as a small anecdote, i did a calculator competition in high school. we used those hp rpn calculators. it was unnatural at first but then i flew once i gained the skill in using rpn.
- klibertp 8y ago> This difference gets worse with more complex expressions. Actually, no - it's the other way around: more complex expressions really benefit from the uniform of syntax and using macros to simplify the code. Not all macros are worthwhile, and some are detrimental to readability (and not writing those is one of the first Lisp lessons), but there are also macros (syntax extensions), which are elegant and powerful. The common example is the `->` or `thread-first` macro, which chains a list of function calls, in the following manner: (-> (list 3) (prepend 2) (prepend 1) print) ;; -> '(1 2 3) into: (print (prepend (prepend (list 3) 2) 1)) With longer expressions, or longer chains of function applications, the difference becomes even more visible, with `->` version having way less parens (possibly less than in Python for equivalent expression) and less nesting, which makes it easier to read and modify. But it doesn't end there - this is just one macro, out of many powerful syntax extensions, which make for much clearer code. There are macros for lazy evaluation, both sequences (like generators) or expressions (more like `lazy` in OCaml); for partial evaluation of functions, for changing name resolution policy, for declaring classes, for looping, for pattern-matching, for error handling, for FFI, for logic statements, for transactional memory, for async control flow, and so on and on. Some of these are trivial to implement, but some are hard or just complicated, with many cases covered; so you're not necessarily expected to write them yourself. If a macro's semantics are well-defined - as in most popular examples - importing a package and reading docs is enough to use them to a good effect. You don't really have to use any of them, but they all exist to make certain patterns in code simpler, more readable, and more convenient to work with. Other languages often lack many of them. Anyway, what I want to say is that the more complex the problem, or the larger codebase, the more readable Lisp becomes, at some point surpassing most other languages. This is what makes Lisp fans to be so good at "recursion and condescension", and is also something you can't see in a `(+ p 2)` kind of examples.