-3

I have a function that i need to take in a word and im trying to make a variable x the sorted version of word variable. Im not sure how i go about doing this... i am trying to pass it in as a parameter for a function but not working.

How do i pass in a "word" and make a variable within the function equal to a sorted version of "word" so i have two copies, the original word and the x version of word. So i can then go on to pass into a map i need to create.

(for [wordset '("one" "two" "three" "FouR" "wot" "Rheet" "nope" "#")]
    (transform2 wordset))

(defn transform2 [word x]
  (let [x (sort-string (str/lower-case word))]
   (prn x)))

(defn sort-string [s]
  (apply str (sort s)))

This is the error im getting back

CompilerException java.lang.RuntimeException: Unable to resolve symbol: x in this context, compiling:(NO_SOURCE_PATH:108:1)
Bean Shares
  • 49
  • 1
  • 6
  • why are you using quoted lists and string words rather than vectors of words or better, vectors of keywords? – johnbakers Jan 30 '16 at 22:08
  • Just im new to the language and i dont fully understand the full range available to me. Not sure how i would loop through vectors as of yet. And im relying on a for loop at the minute. Im trying to convert this to Clojure ->> http://pastebin.com/aRRdMCn4 – Bean Shares Jan 30 '16 at 22:12
  • 2
    You've already asked this same question here: http://stackoverflow.com/questions/35092605/clojure-into-array-iterate-over-array – jmargolisvt Jan 30 '16 at 22:19

2 Answers2

0

This expression is doing nothing for you:

(for [wordset '("one" "two" "three" "FouR" "wot" "Rheet" "nope" "#")]
    (transform2 wordset))

You are not placing this anywhere where the return value will be used. A for is not like an imperative for loop in other languages. It just creates a sequence and returns it, it is not modifying anything.

Also you should use vectors and prefer keywords, in general:

[:one :two :three :whatever]

Though that would change the semantics of what you're doing, so maybe you have strings coming in from somewhere else and need to use them? Otherwise, don't use strings yourself as identifiers: that's what keywords are for.

As for your other questions, it's not quite clear what you mean. You say you'd like to make a variable, but it's not clear what you mean by this, as the code you have isn't doing anything along those lines.

johnbakers
  • 24,158
  • 24
  • 130
  • 258
  • Yeah i need to use Strings i am trying to convert this into Clojure http://pastebin.com/aRRdMCn4 Eventually i will use the for expression but im having a nightmare covnerting this into Clojure. This is my first attempts at a functional language so im a total novice. – Bean Shares Jan 30 '16 at 22:30
0

Your transform2 accepts two arguments but you're passing only one. Try removing the x argument like below. Also, you may want to reverse the order you create the functions because they need to be defined before being used. (You may also use declare.)

(defn sort-string [s]
  (apply str (sort s)))

(defn transform2 [word]
  (let [x (sort-string (clojure.string/lower-case word))]
    (prn x)))

(for [wordset '("one" "two" "three" "FouR" "wot" "Rheet" "nope" "#")]
  (transform2 wordset))

Result will be:

"eno"
"otw"
"eehrt"
"foru"
"otw"
"eehrt"
"enop"
"#"
Brian
  • 967
  • 7
  • 12