Fork me on GitHub
#beginners
<
2022-09-22
>
leif16:09:12

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))

leif16:09:03

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 `.

hiredman16:09:49

macroexpand doesn't require any kind of quoting

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

leif16:09:18

I am using (bootstrapped) clojurescript.

leif16:09:20

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

leif16:09:22

SIgh, thanks.

leif16:09:14

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?

dpsutton19:09:57

qp=> (defn get-char [number] (let [s (str number)] (fn [n] (nth s n))))
#'nocommit.qp/get-char
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

dpsutton19:09:23

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 :-)

dpsutton19:09:11

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

dpsutton19:09:34

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

dpsutton19:09:51

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

Eugene Mosh19:09:23

Standart function nth are not lazy.

dpsutton19:09:29

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

dpsutton19:09:58

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 https://projecteuler.net/ 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 :-)

phronmophobic19:09:29

> 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

2
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

dpsutton19:09:01

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

phronmophobic19:09:33

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

dpsutton20:09:09

that’s done once and closed over

dpsutton20:09:31

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

phronmophobic20:09:14

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?

dpsutton20:09:32

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

dpsutton20:09:56

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?

dpsutton20:09:32

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

1
Eugene Mosh21:09:02

My question was stupid :-(

❤️ 1
dpsutton21:09:02

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

dpsutton21:09:16

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