4 ms·
I do understand, but I'll also point out that that first expression will often (with longer variables or expressions in particular) be broken out like: (* (+
by Jtsummers 11y ago
I do understand, but I'll also point out that that first expression will often (with longer variables or expressions in particular) be broken out like:
(* (+ a b)
(+ c d))
Which is readable, though not necessarily compact.
Also, * and + in the former, aren't strictly the same as in the latter. * and + take an arbitrary number of parameters in CL. From [0], `(* )` => `1`. I can't test, but I believe `(* 2)` => `2` (the spec doesn't describe the case of a single parameter, unless I'm missing it). `+` is the same, but `(+)` => `0` instead, it's identity value.
Order of operations is made more explicit, and, I've found, it's more useful to think of `+` and `*` as `sum` and `product` rather than `plus` and `times`.
[0] http://www.lispworks.com/documentation/HyperSpec/Body/f_st.htm http://www.lispworks.com/documentation/HyperSpec/Body/f_st.h...
[1] http://www.lispworks.com/documentation/HyperSpec/Body/f_pl.htm http://www.lispworks.com/documentation/HyperSpec/Body/f_pl.h...
- 21 11y agoMath does also have `sum` and `product` in the form of Sigma and Pi. Of course, not exactly the same thing (since they operate over a set, not discrete elements). I would venture to say that the reason infix notation is naturally preferred is related to our psychology, the same way most human languages are SVO (Subject Verb Object) or SOV. VSO languages (Lisp like) are less prevalent. In general my opinion is that when a majority vastly prefers one alternative, there is usually a strong reason for it (even if it may be irrational) and it's foolish to go against the grain.
- Jtsummers 11y agoInfix is mostly used, in programming, within mathematical and logical expressions. But the majority of my code spends its time in some kind of chain of function calls, which has the verb first. Maybe if I did more with OO-languages I'd see it differently?
- AnimalMuppet 11y agoInteresting. OO syntax often is object.function(arguments), which is subject-verb-object order. I never thought of it that way before. You can throw some adverbs in among the arguments, too.
- knughit 11y agoThat's correspondent with how Java and especially Obj-C and super especially AppleScript programmers try to write code that reads like COBOL, er, English.
- stan_rogers 11y agoI seem to recall that SOV (reverse Polish notation) is marginally more prevalent than SVO among the world's languages... though it is true that most creoles are SVO, which does at least seem to indicate that it's a default of sorts.
- knughit 11y agoLisp is VOO, not SVO. Java is SVO. C is VOO too.
- rol 11y agoIf you have Quicklisp[1] installed you can install the "infix" package and get infix notation in Common Lisp[2]: $ sbcl This is SBCL 1.2.4.debian, an implementation of ANSI Common Lisp. More information about SBCL is available at <http://www.sbcl.org/>. > (ql:quickload 'infix) ; Loading package (INFIX) > #i(1 + 1) ; addition 2 > #i(2^^128) ; exponentiation 340282366920938463463374607431768211456 > (defun factorial (x) #i(if x == 0 then 1 else x * factorial(x-1))) ; infix function call FACTORIAL > (factorial 5) 120 > #i(factorial(5) / factorial(6)) 1/6 > '#i((a + b) * (c + d)) ; Put a ' before the #i() to see what code is generated (* (+ A B) (+ C D)) -- [1] - https://www.quicklisp.org/beta/ https://www.quicklisp.org/beta/ [2] - Don't know if there is a similar package for Scheme.
- adwf 11y agoI wouldn't recommend using infix libraries if you really want to get into Common Lisp though. They're a bit of a crutch for people coming from other languages, but that's it. Pretty much the whole language is based on Polish notation. The sooner you realise that + - * / are just function names like any other, the better you'll do. For example: (+ 1 2 3) in plain symbols is just: (function parameter parameter parameter) But if I were to write my own addition function: (addition 1 2 3) it would also be: (function parameter parameter parameter) and so is: (http-request "http://www.google.com") (function parameter) If you use infix notation, you're writing half your code in a competely different semantic to the other half. I can't imagine it helping people really get a proper grasp of how Common Lisp works.
- jrapdx3 11y ago> Don't know if there is a similar package for Scheme. I have to agree with others in the thread that infix in Lisp/Scheme is not the convention, and IMO an awkward fit. Don't recall encountering infix in any published/shared code I've seen, it may exist, but to learn Scheme becoming comfortable with s-expr notation is definitely necessary. However, there is SRFI 105[0] which describes "curly infix expressions". It's implemented in Guile 2.x, possibly available in a few others but evidently not had a lot of uptake among Schemes. [0] http://srfi.schemers.org/srfi-105/srfi-105.html http://srfi.schemers.org/srfi-105/srfi-105.html Edit: added URL