If your new to Clojure, such a succinct solution might be a little
confusing... so I've broken islon's solution down into its parts:

> (defn string-reverse [s]
>   (reduce #(str %1 " " %2) (reverse (seq (. s (split " "))))))

The main part of your problem is that you want to pick out words. In
the solution provided by islon this is done with the standard java
string function "split".
(. s (split " "))
Is one way to use the built in java interop to essenitally do this:
***java***       string words[] = s.split(" ");
you can also write this as
(.split s " ")          *** see clojure java interop section for more
info

This is pretty typical in clojure, most of the low level string etc
stuff you just use the java API as it has a ready made solution.

Clojure part:
(reverse (seq
Pretty obvious really, the array of strings is converted to a sequence
which is reversed, which is what you wanted to do with the words.
reverse is a common operation used on sequences. It is interesting to
note that you don't need (seq at all really... so you can just leave
that out. There is a heap of support for sequence operations in
Clojure that make things really simple and concise
user=> (reverse (.split "you will become jedi" " "))
("jedi" "become" "will" "you")

This is probably all you want to do, but its nice to put it in a
function
(defn word-reverse [s]
   (reverse (.split s " ")))

Now you can call your new function:
user=> (word-reverse "well done padawan")
("padawan" "done" "well")

(reduce #(str %1 " " %2) (reverse....))
Hard on the eyes, but all this does is take the sequence of words and
glue them together into a single string... so that instead of
returning a list of words the function will return a string.
#(str %1 " " %2)   is a special form lambda function
ie: it is just a temporary inline function which is going to be passed
to another function
in this case the function takes 2 arguments and makes a string out of
them in the form
"%1 %2"  where %1 is arg1 and %2 is arg2
[http://clojure.org/reader] explains #(...)  in more detail
(reduce     is a function which takes a function as its first argument
and calls that function on a supplied sequence. See the api doc for a
more correct definition... but for our purposes the code says:
(forallwords temporyjoinwordfunction allwords)
So calling this results in a single string
user=> (reduce #(str %1 " " %2) (reverse (.split "you will become
jedi" " ")))
"jedi become will you"
Notice that instead of getting separate words, you get a full string.

Temporary functions and passing functions to functions is one of the
really powerful parts of any Lisp, so it is worth learning about it in
detail and not be tempted to gloss over it.

Regards,
Tim.




--~--~---------~--~----~------------~-------~--~----~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to