2 ms·
So here's something which I, as a Lisper, don't understand: Clojure has syntax for hash tables (the curly braces), why doesn't it use those for let-bindings, in
by tmtvl 2y ago
So here's something which I, as a Lisper, don't understand: Clojure has syntax for hash tables (the curly braces), why doesn't it use those for let-bindings, instead choosing to use vector syntax? (Of course I prefer standard Lisp syntax which can easily be extended for multiple-value and destructuring bindings:
(let ((a 1)
((b c) (list 2 3))
(d e (values 4 5)))
`#(,a ,b ,c ,d ,e))
;; => #(1 2 3 4 5)
- puredanger 2y agoMaps are unordered, but let bindings are sequential so order matters.
- neilyio 2y agoClojure has some very expressive destructuring syntax as well: https://clojure.org/guides/destructuring https://clojure.org/guides/destructuring
- didibus 2y agoWhat's so different to Clojure's ? (let [a 1 [b c] (list 2 3) [d e] [4 5]] `(~a ~b ~c ~d ~e)) ;; => (1 2 3 4 5)
- tmtvl 2y agoBut what would be the equivalent of: (let ((quotient remainder (floor x y))) (+ (* quotient z) (* remainder (floor z y)))) Where floor is a function which returns multiple values rather than a list or vector of results? Also, but this is entirely on me being an absolute dullard, I like having the brackets around each binding as a kind of guardrails for my mind to not lose track of where I am and what belongs where.
- didibus 2y agoIt's just: (let [[quotient remainder] (floor x y)] (+ (* quotient z) (* remainder (first (floor z y))))) Because in Clojure there's no multiple value bind, when you want to return many results from a function, you wrap it in a vector, and that means you than just destructure that vector. That also means, it's not smart enough to know if it's used in a single value context or multi value context, so you need to always get the value out of the vector, which is why I call first in that second call to floor.