Aridaman, > I am very new to clojure and have worked previously on C/C++ > > I wanted to do some string operations, where I can match occurrence of > a particular string. However, I get this error everytime. > > java.lang.ClassCastException: clojure.lang.Var cannot be cast to > java.lang.CharSequence (myseq.clj:0) > > Here is the code, which is quite lame but is just made for learning > purpose. > > (def myseq "the quick brown fox") > (loop [word myseq] > (if (= (first (re-seq #"\w+" word)) "fox") (println "Yeah!!") > (recur (def myseq (apply str (interpose " " (rest (re-seq #"\w+" > myseq))))))))
Your syntax for loop/recur is completely wrong :)
I think you wanted to do something like this -
(def myseq "the quick brown fox")
(loop [s myseq]
(if (= (first (re-seq #"\w+" s)) "fox")
(println "Yeah!")
(recur (apply str (interpose " " (rest (re-seq #"\w+" s)))))))
In short, you don't def the value of myseq again when you recur.
Remember that Clojure is a (mostly) functional language where mutation
is discouraged. Whenever you see yourself mutating things, you should
double check the code.
def is for top-level variable declarations only. You can use let for
lexical bindings, that too when required.
Have fun :)
Regards,
BG
--
Baishampayan Ghose <[email protected]>
oCricket.com
signature.asc
Description: OpenPGP digital signature
