This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-12-13
Channels
- # admin-announcements (208)
- # beginners (53)
- # boot (46)
- # cider (10)
- # cljs-dev (26)
- # cljsjs (10)
- # clojure (66)
- # clojure-dev (3)
- # clojure-russia (14)
- # clojurecup (5)
- # clojurescript (302)
- # cursive (22)
- # data-science (1)
- # datomic (10)
- # emacs (1)
- # events (2)
- # hoplon (91)
- # incanter (1)
- # ldnclj (3)
- # leiningen (1)
- # off-topic (2)
- # om (41)
- # re-frame (40)
- # reagent (78)
Why do I get this error message : CompilerException java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn
on this code
(defn number_to_digits
[number]
(loop
[n number
acc ()]
(if (= ( number 0))
acc
(recur (conj acc (rem n 10)) (n mod 10) )
)
)
)
Because you use number as a function
(number 0) is trying to do that. Don't get sloppy with your parens again đŸ˜‰
= is a function. Great, it may stand as first item in a list. Number is not so it has no permissions there
I'm great. A lot to do, new company, new work, less clojure but still a lot of fun
oke, I changed it and see this error : CompilerException java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to java.lang.Number
defn number_to_digits
[number]
(loop
[n number
acc ()]
(if (= number 0)
acc
(recur (conj acc (rem n 10)) (mod n 10) )
)
)
)
Look at the arguments you give recyt
Recut
Recur
Sorry
What are they when you define them and what are they when you actually give it to recur?
Tip: you're effectively calling (mod () 10) when the error occurs. I can understand why your repl is complaining
In which order do you give stuff to recur?
Good :)
Need to fix lunch now. Have fun
chips now a endless loop I think :
(defn number_to_digits
[number]
(loop
[n number
acc ()]
(if (= number 0)
acc
(recur (mod n 10) (conj acc (rem n 10)) )
)
)
)
You could add (println "n: " n "acc: " acc)
above your if
so you can see what's going on.
@mccraigmccraig: thanks, this is also working
(defn number_to_digits
[number]
(loop
[n number
acc ()]
(if (= n 0)
acc
(recur (quot n 10) (conj acc (rem n 10)) )
)
)
)
(defn number_to_digits
[n1 n2]
(let [outcome (* n1 n2)]
(loop
[n outcome
acc ()]
(if (= n 0)
acc
(recur (quot n 10) (conj acc (rem n 10)) )
)
)
))
@roelof off the top of my head (->> n (iterate #(quot % 10)) (take-while #(not (zero? %))) (map #(rem % 10)) reverse)
(well, only the loop part)
@val_waeselynck: thanks, What does the ->> do, I have not learned that
it's the thread-last macro. It basically rewrites the above code as (reverse (map #(rem % 10) (take-while #(not (zero? %) (iterate #(quot % 10) n)))))
@roelof: maybe you have not learned about macros yet?
a little , I have learned to make a macro which convert and apply things like this ( 2 + 3) to ( + 2 3) and outputs then 5
The threading macros are among those I use most often, ->>
for transforming collections and ->
for transforming maps typically
they let you write successive function calls left to right instead of inside out, which is quite nice.
if im right , the cartesian product of two sets is the first one from set one with the first one of set2
@roelof: yes, that's right. If you had two sets, A = {1,2} and B = {3,4}, then the cartesian product A x B would have {(1,3), (1,4), (2,3), (2,4)} as its elements.
@codonnell: then I have to think hard how to do something like that Maybe use the idea of a earlier exercise simpler closures
@roelof: play around a bit with for
@codonnell: if you mean this : http://conj.io/store/v1/org.clojure/clojure/1.7.0/clj/clojure.core/for that looks promosing