Fork me on GitHub

Is there any way to macro expand the bind vars of a let form? Something like:

(ns test.core)

(defmacro foo [a]
  `[~a :b])

(defmacro alet [a body]
  `(let (foo ~a) ~body))


I did try:

(defmacro alet [a body]
  (let [exp (macroexpand `(foo ~a)]
    `(let ~exp ~body)))
But then I get an error saying that macroexpand requires specifically ', and not `.


macroexpand doesn't require any kind of quoting

user=> (macroexpand (list  (symbol "+") 1 2))
(+ 1 2)
unless you are using clojurescript, where macroexpand is a macro that pretends it is a function


I am using (bootstrapped) clojurescript.


I see that clojure proper's macro expander would be okay with this.


SIgh, thanks.


It also looks like macroexpand is not supposed to actually be used in your code...just when experimenting at the repl.

Eugene Mosh19:09:20

Hello friends! 🙂 Is there idiomatic way to write lazy function which will convert n-th digit of Number to Char? (i.e. lazy evaluation of selected digit in decemical number) Thnx!

Ben Sless19:09:18

You can write your code normally and wrap it in a delay Why do you want to do it lazily?


qp=> (defn get-char [number] (let [s (str number)] (fn [n] (nth s n))))
qp=> (let [f (get-char 9876)]
       (map f [0 1 2 3]))
(\9 \8 \7 \6)
i’m not sure i’m following the lazy bit. Just compute on demand is one version of lazy


assemble how you like but (nth (str number) index) does what you want

Eugene Mosh19:09:54

> Why do you want to do it lazily? To work with realy big numbers :-)


I still don’t follow. But my example works just fine


if you don’t want to repeatedly str the number my get-char closes over the string representationo

Ben Sless19:09:50

I don't see why the number's size matters


but otherwise index access to a string is constant (i think)

Eugene Mosh19:09:23

Standart function nth are not lazy.


I’m still not following. Can you explain how you would make + lazy?


the only thing i can think of is like haskell where the expression is not computed until the result is “needed”. And Clojure offers nothing like that

Eugene Mosh19:09:44

I'm trying to solve problems from And here a lot of problems related to lazy evaluations of few last digits of some realy big number.

Eugene Mosh19:09:11

Guys I'm newbie, so don't be scary :-)


> the only thing i can think of is like haskell where the expression is not computed until the result is “needed”. And Clojure offers nothing like that Aren't delay and lazy sequences like that?

Ben Sless19:09:49

you can get a digit using just some math, mainly div and mod

Ben Sless19:09:10

nth digit from the end

(require '[clojure.math :as math])

(defn nth-digit [^long num ^long n]
  (mod (quot num (long (math/pow 10 n))) 10))

(nth-digit 123456 1)

🙂 1
Eugene Mosh19:09:12

For example: N equal to sum of 1 trillion of prime numbers. Find last digit of N (like ...4189 -> 9).

Ben Sless19:09:24

it won't work for bignums, you'll need to remove the type hints


my str version will happily work with bignums and give constant time results


isn't the str version linear time because of (str number)?


that’s done once and closed over


so depends. its amortized. but if you only get one digit then yes


right, it looks like one of the goals is to get one of the least significant digits quickly

😍 1
Ben Sless20:09:21

the fun question is - is there a way to get the nth digit without calculating everything?

😎 1
Ben Sless20:09:26

for example, if you need the nth digit of the sum of all primes, you can always mod the result 10^(n+1)

Ben Sless20:09:41

so you don't have to hold an insane sum of numbers

Ben Sless20:09:05

even further, is there an analytical expression for the nth digit of the sum of k primes?


i wonder what the internal representation of bignums, bigints are. It might have a string representation in there so the str call is just a constant field access and then .charAt is constant as well

Ben Sless20:09:55

probably byte arrays


i doubt there is a closed form expression for that sum

Ben Sless20:09:17

not for the sum itself, but what about a specific digit?


doubt even more but would love to hear the answer

Ben Sless20:09:48

I'm going to sleep before I get nerd sniped by Reimman

Eugene Mosh21:09:02

My question was stupid :-(

❤️ 1

absolutely not. it was an honest question and we had a great discussion


and it led to interesting places. everyone here is better off having thought about it, wrestled with it

👍 1
Eugene Mosh19:09:13

(defn digit [nth number] (SomeCode)) (digit 3 717934) -> 9