1
votes

Just teaching myself clojure and wondering around:

I am trying to create a function that takes an argument and adds it to a string. Being a newbie, i dont know if i am doing it right or wrong but it is not working. I want it to say "Hello, Ron !"

(fn 
 [x] 
 ((str "hello, " %1 " !") x) "Ron")

This might sound basic

java.lang.RuntimeException: Unable to resolve symbol: % in this context, compiling:(NO_SOURCE_PATH:0)

2
What does your REPL say? That would tell you for sure. - jmargolisvt
@LeonGrapenthin yh i am doing that but i thought there was a special way of concatenating string just like java or javascript - Coding Enthusiast

2 Answers

3
votes

The %1 syntax is for use with anonymous function literals, like this:

#(str "hello, " %1)

In your case, the argument is named, so you can use it directly:

(fn [x] (str "hello, " x "!"))

You can also name the function itself:

(defn hello [name] (str "hello, " name "!"))
0
votes

You can either use a:

  1. named function

(defn hello [name] (str "hello, " name " !")) (hello "Ron")

  1. anonymous function

((fn [name] (str "hello, " name " !")) "Ron")

http://clojure.org/functional_programming