Beginner: & ldquo; For & rdquo; Loops in functions behave unexpectedly

I’ve been developing in Java and Perl for a long time but wanted to learn something new so I’ve begun looking into clojure. One of the first things I’ve tried was a solution for the Towers of Hanoi puzzle but I’ve been getting strange behavior on my pretty-print function. Basically, my for loop is never entered when I run it with ‘lein run’ but it seems to work fine when I run it from the repl. Here’s a stripped down example:

(ns test-app.core
  (:gen-class))

(defn for-print
  "Print the same thing 3 times"
  [ p-string ]
  (println (str "Checkpoint: " p-string))
  (for
    [x [1 2 3]]
    (printf "FOR: %sn" p-string)
))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (for-print "Haldo wurld!"))

When I run this with ‘lein run’ I only see the output from the “Checkpoint” println. If I remove that line I get no output at all. But, if I run ‘lein repl’ and then type (-main) it prints the string 3 times, as expected:

test-app.core=> (-main)
Checkpoint: Haldo wurld!
(FOR: Haldo wurld!
FOR: Haldo wurld!
FOR: Haldo wurld!
nil nil nil)
test-app.core=>

What’s going on here? I have a feeling that I’m approaching this the wrong way, trying to use my past Perl/Java mentality to write clojure. What would be an idiomatic way to run the same task a set number of times?

The for
loop is returning a lazy sequence that is evaluated only as needed.

When you run the program inside the repl
the for result is realized in order to display the result on screen.

But when you run using lein run
result is never used so the collection is not realized.

You have a couple alternatives:

1) use doall
outside the for
loop for force lazy sequence realization

Ex:

(defn for-print
 "Print the same thing 3 times"
 [ p-string ]
 (println (str "Checkpoint: " p-string))
 (doall (for
    [x [1 2 3]]
    (printf "FOR: %sn" p-string))))

2) since you’re only printing which is a side effect and not really creating a collection you can use doseq.

Ex:

(defn for-print
  "Print the same thing 3 times"
  [ p-string ]
  (println (str "Checkpoint: " p-string))
  (doseq [x [1 2 3]]
   (printf "FOR: %sn" p-string)))
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Beginner: & ldquo; For & rdquo; Loops in functions behave unexpectedly

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录