0

In REPL, this code worked just fine and gives the answer 36

(do
  (load-string (str "(def yyy" 2 " 18)"))
  (* 2 yyy2))

While it wouldn't work when I introduced a for loop into it

(do
 (for [x [1 2 3]]
  (load-string (str "(def yyyy" x " 18)")))
 (* 2 yyyy2))

The error said unable to resolve symbol yyyy2, which should have been defined within load-string. Can some expert explain the reason for me? Thanks!

1 Answer 1

2

for produces a lazy sequence thus load-string expression won't be evaluated until you force its evaluation. You can force it by wrapping for in dorun or doall.

For cases where you only need the side effects you should use doseq or when you just need to iterate over a range of numbers dotimes might also be handy.

Sign up to request clarification or add additional context in comments.

2 Comments

Instead of wrapping for in dorun or doall, you should just replace for with doseq.
@SamEstep You're right - I have edited my answer. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.