I think you're looking for something more like this:

(use '[clojure.contrib.str-utils :only (str-join)])

(def mystr "the quick brown fox")

(defn split-at-word
  [text keyword]
  (str-join " " (drop-while (partial not= keyword)
                            (re-seq #"\w+" text))))


user=> (split-at-word mystr "brown")
"brown fox"


A few points:

1. Functions return values by returning values, not by storing them in
global mutable variables.  Trust me on this.

2. Try to think of ways to solve your problem without using loop/
recur, but instead using higher-order constructs.  This is a valuable
skill to have, and a way of thinking about problem-solving that can
help you develop as a programmer even in other languages.

3. You were attempting to reassemble the seq of words into a string
and then take it apart again at every iteration -- why not just leave
it as a seq until you're finished, and reassemble it at the end?


On Sep 17, 10:15 am, Aridaman Pandit <pandit.arida...@gmail.com>
wrote:
> Hi
>
> Thanks for the reply. I understand the code that you have given.
> Actually I also wanted it to be stored into some variable (if not the
> same) everytime I run it.
> Suppose I am matching for keyword "brown" and I want to store
> everything after "brown". So for that purpose I wanted to chop off
> everything that comes before "brown" and keep the rest.
> Can you help me in doing that. I know its a bit silly thing but the
> thing is I am very new to it and still in the C/C++ like orientation.
> But the thing is how do I assign it some keyword or something so that
> I can call it as and when required.
>
> On Sep 17, 3:53 pm, Baishampayan Ghose <b.gh...@ocricket.com> wrote:
>
>
>
> > 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 <b.gh...@ocricket.com>
> > oCricket.com
>
> >  signature.asc
> > < 1KViewDownload
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to