freenode/#lisp - IRC Chatlog
Search
15:57:24
Shinmera
how much shorter do you want it to be? Even if you define a function that's only one character long you save a mere two characters in that example
16:18:44
Jachy
asarch: You could always load an infix lib, e.g. https://github.com/rigetticomputing/cmu-infix
16:42:29
pjb
asarch: (defun ² (x) (* x x)) (defun √ (x) (sqrt x)) (defun theorem (a b) (√ (+ (² a) (² b))))
17:05:33
rocx
makes it nice not having to make a `sum` function when you can just do (aply #'+ '(1 2 3)).
17:12:24
pjb
LdBeth: about RPN, just try to implement a macro (rpn c a ² b ² + √ +) -> (+ c (√ (+ (² a) (² b))))
17:12:53
pjb
LdBeth: also, there would be a problem with side effects and left-to-right evaluation rule of lisp…
17:22:50
LdBeth
asarch: it’s easy from sexp to RPN, but hard from RPN to sexp without prior knowledge about which are operator
17:35:31
pjb
asarch: arity = number of parameters. But in lisp, arithmetic operators take any number of arguments from 0 to call-arguments-limit, and in general, a macro cannot know how many mandatory argument are required for a given operator (function or macro), notably because it may be macroexpanded before the operator is defined!
17:35:57
pjb
This is why we kept using lists: so that macros (and code walkers) could be implemented easily.
17:42:06
aeth
The macro for an RPN-style s-expression would just do something like: `(,(car (last foo)) ,@(butlast foo))
17:43:55
pjb
(defun rreverse (sexp) (if (atom sexp) sexp (mapcar (function rreverse) (reverse sexp)))) (rreverse '(+ c (√ (+ (² a) (² b))))) #| --> ((((b ²) (a ²) +) √) c +) |#
17:44:10
aeth
You don't want true RPN outside of a calculator imo so this is a better way of doing it. And it's not hard to apply to every element. Just every ,@(every #'my-transformation (butlast foo))
17:46:55
aeth
CL's confusing because it has nicer named things that have no difference (like first) and nicer named things that are different
17:49:26
aeth
(defun r-expression-to-s-expression (r) (typecase r (cons `(,(car (last r)) ,@(mapcar #'r-expression-to-s-expression (butlast r)))) (t r))) ; put this in a macro and add a special case for dotted lists if you want to support them in r-expressions
17:51:39
pjb
Of course, you can implement a rpn language, but you will have to explicitely define all your rpn operators. You cannot just call random lisp functions.
17:52:20
pjb
(define-rpn-operator + 2 +) (define-rpn-operator - 2 -) (define-rpn-operator neg 1 -) etc…
18:05:35
aeth
pjb: Just one clarification. I don't think r-expressions would reverse everything, just put the first item last in an evaluated list (which means I need to add a special case for QUOTE (and FUNCTION), which I didn't do). If they're like RPN the only difference is that the operator is at the end, e.g. in my RPN calculator 4 2 / => 2
18:07:25
aeth
So going to an r-expression would be a bit trickier. Naively, this: (let ((foo '(+ 1 2 3))) (append (cdr foo) (list (car foo))))
18:14:39
pjb
aeth: well this means you have to delay the evaluation of variables. Usually in RPN, variables are evaluated and their value is stacked. If you want to use the quote operator after, unless you look ahead, you would have to let allt the operators evaluate the variables. I'd say this would set a bad precedent.
18:18:13
aeth
pjb: The main problem is that ((1 2 3) quote) becomes '(3 1 2) and another problem is that people are used to ' and #' as syntax, but they can't just write '(1 2 3) in r-expressions without a custom reader and writer.
18:18:57
aeth
You can do that, but you're still going to have to deal with QUOTE as a special case to avoid the '(3 1 2) problem
18:19:44
pjb
But the point is that RPN is a new syntax, you have to parse it, and you cannot just convert a kind of rpn sexp into a sexp. It's antithetical to the S-exprs and lisp.
18:20:24
pjb
And the Texas Instrument patent on parentheses in calculators is over since a long time.
18:20:48
aeth
Polish notation would have the same problem. * + 1 1 2 when you can just (* (+ 1 1) 2)
18:22:01
aeth
RPN is a write-only language. A very useful one, but definitely write-only. That's why it's mainly used in calculators and programs like dc. And people accuse Perl of being write-only!
18:23:19
stylewarning
Functions (known as “words”) are orders of magnitude shorter than their Lisp equivalents
18:26:13
aeth
I use nested brackets even with my infix arithmetic. Never trust your knowledge of precedence to always work. And never trust the reader to understand all of those rules, too. And this example would need it in infix, anyway. (1 + 1) * 2
18:27:35
pjb
C has 27 precedence levels. C++ even more. Smalltalk is quite unintuitive too. Infix is bullshit.
18:27:35
stylewarning
Brackets can also blur cases where you actually don’t care about order, as with associative operators
18:28:11
aeth
Mathematics is two dimensional. It's a lot easier to express something clearly on paper or generated from LaTeX than it is to express it in one line of ASCII that pretends to be mathematics.
18:29:41
stylewarning
A reminder that there exists CMU-INFIX for Lisp https://github.com/rigetticomputing/cmu-infix
18:30:07
pjb
So when you're fed up with all this bullshit, you just write (+ 2 (* 3 5)) or (* 5 (+ 2 3)) and go on with your life.
18:31:44
aeth
If I met someone with a degree from "CMU" I'd say "When did you get that? 20 years ago? Should've gone to Steel Bank instead."
18:33:08
aeth
shka_: Yeah, but it's not like it's one of the top computer science universities in the United States.
18:34:25
aeth
I think traditionally, MIT, CMU, Berkeley, and one other are considered "tied". Can't think of the last one at the moment.
18:36:16
aeth
e.g. https://www.usnews.com/best-graduate-schools/top-science-schools/computer-science-rankings
18:55:18
aeth
This was just on HN, about the history of T, and one of the early points a few paragraphs in is that the move to 32-bit changed everything in how you represent your data. http://www.paulgraham.com/thist.html