I’m trying to touch all potential dealer hands in blackjack, but when I kept blowing the stack, I realized things weren’t as depth first as expected. So I tried similar code in ruby and it performed differently.
this code,
(def d [2 3 4 5 6 7 8 9 10 10 10 10 11])
(defn dig1 [lst depth tot]
(do
(print depth)
(if (< tot 17) (map #(dig1 (conj lst %) (+ depth 1) (+ tot %)) d)) ))
(dig1 [0] 0 0)
produces: 011111111111112222222222222…
I expected map to execute the function on d[0] and dig down rather than to see everything executed at a given level. I obviously don’t understand what’s going on. Do I need to make something lazy(er)? map produces lazy sequences, but apparently chunks them in groups of 32.
in contrast,
@d = [2,3,4,5,6,7,8,9,10,10,10,10,11]
def dig(lst, depth, tot)
p depth
@d.map{|e| dig(lst.dup.push(e),depth+1,tot+e)} if tot < 17
end
produces what I would expect: 0123456789999999999999888888888888
If anyone could tell me how to make the clojure output look like the ruby output, I’d appreciate it.
Thanks, John
Generally
mapisn’t used when you don’t want the returned values back and are only evaluating the sequence for side effects. Something likedoseqis preferable.Produces:
012345678999....