5 ms·
So do I. I prefer to type out `=` three times, for a simple reason: while not being more verbose than the `setf` example (3 additional tokens in both examples)
by loup-vaillant 10y ago
So do I. I prefer to type out `=` three times, for a simple reason: while not being more verbose than the `setf` example (3 additional tokens in both examples), it makes all 3 lines the same, which is more regular.
We could make all 3 lines the same with `setf`, but that would be a bit more verbose:
(setf x 10)
(setf y 20)
(setf z 30)
In a program that heavily use the assignment statement, I vote for the shorter form, `=`.
- kazinator 10y agoIt is absolutely more verbose. The Lisp form can store N constants into N variables in 2N+3 tokens: two parentheses, plus the variables and constants and one symbol. Assignment statements require 3N tokens: N vars, N constants, N assignment operators. That's assuming they don't need some mandatory separator or terminator like a semicolon. We break even when N = 3. For N > 3, 2N+3 < 3N. Anyway, this is hardly the main point of the article (and probably detracts from it).
- loup-vaillant 10y agoIndeed, my main point is not verbosity —it's regularity. I'd rather repeat `setf` in most cases in the name of regularity. That way we spot the semantic difference between lines more easily. You'd have to use the assignment heavily to justify implementing the `=` syntax, which is barely prettier than the regular `setf` syntax. Plus, you'd probably have to add some annotation anyway, partially defeating the point: (assign x = 10 y = 20 z = 30)
- kazinator 10y agoIf you repeat setf, you may need a progn to make it one expression. And parallel assignment is off the table without temporary variables: (if condition (psetf a c b a c b) (psetf a b c a b c)) :) P.S. of course I know rotatef would be better here.
- PrettyButUgly 10y agoI'm not much for Common Lisp, but I do think Scheme is the prettiest language in which people write ugly code. As such, while I am sympathetic to the virtues of s-expressions, this example is not going to win any hearts or minds. I don't much care for Python's performance or newer features, but your snippet screams for a comparison: if condition: a, b, c = c, a, b a, c, b = b, a, c That really seems much more clear than the gymnastics my mind has to do manually pairing the assignments inside of psetf. I won't argue that it's objectively better syntax, but I still think it is.
- gluggymug 10y agoYou forgot "else:"
- PrettyButUgly 10y agoFunny that. In my head, I parsed it as two (odd) statements under one if statement. More like when than if.
- kazinator 10y agoThat's why I mentioned rotatef: (rotatef a c b) ;; a <-- c <-- b ;; `--->----->-'
- qwertyuiop924 10y agoWell... (define-syntax assign-group! (ir-macro-transformer (lambda (x i c) (let ((vars (cadr x)) (vals (caddr x))) `(let ,(map (lambda (var val) '(var val)) vars vals) ,@(map (lambda (var) `(set! ,(i var) ,var)) vars)))))) Mind, I haven't tested this, because I don't have CHICKEN Scheme (the dialect this is written in) in front of me, and I may not have matched all the parens on the end, but it should work like this: (assign-group! (a b c) (c a b)) Giving you your nice assigment syntax you wanted. That's the nice thing about Lisps: if you don't like it, you can change it.
- Avshalom 10y agoon the other hand = is one character, and if it's not a legal character in identifiers then x=1 y=2 z=3 takes 2n characters (setf x 1 y 2 z 3) takes 2n+7 characters (plus however many we're using for the actual identifies and values but those'll be the same between examples)
- kazinator 10y agoLisp has an = function; so that binding is taken. Symbols in the keyword package are allowed to have function bindings, and so a synonym for setf can be called := (the symbol named "=" in the keyword package). [1]> (defmacro := (&rest args) `(setf ,@args)) := [2]> (defvar a) A [3]> (:= a 42) 42 [4]> a 42 [5]> (= a 42) T
- orbifold 10y agoThat is why you would typically create a struct, and then assign the whole struct at once. If you are regularly assigning > 20 variables at the same time, something is wrong with the language you are using.