Hi!

Here's another way you could do it:

(defn say-hello
  ([names] (say-hello names #{}))
  ([names saluted]
     (reduce (fn [saluted name]
               (if (saluted name)
                 (do (println "Hello" name "!")
                     (conj saluted name))
                 (do (println "Welcome Back" name "!")
                     saluted)))
             saluted names)
     (println "Goodbye!")))

https://gist.github.com/johnwalker/750ad582d50dd3f2e09e

A few things to think about are the loop and recur macro. loop is a macro 
that Clojure uses since there is no support for tail recursion on the jvm. 
Since we're just iterating over the seq though, we can just use reduce. A 
minor stylistic detail is that hyphens are generally preferred over 
underscores. The unary function providing a default value for the second 
argument and calling the binary function is idiomatic. Of course, there are 
many ways to do this. Good luck!

On Wednesday, October 1, 2014 4:06:00 PM UTC-7, Nicolás F. wrote:
>
> I'm a clojure noob and im playing around with the language; made this fn 
> to have a taste of a couple of features. I think it's pretty concise but 
> i'm probably doing something "not the clojure way", So if you can teach me 
> a couple of things please go ahead ;D
>
> (defn say_hello
>   ([names] (say_hello names []))
>   ([names saluted]
>     (if (some #{(first names)} saluted)
>       (println "Welcome Back" (first names) "!")
>       (println "Hello" (first names) "!"))
>     (if (empty? (rest names))
>       (println "Goodbye") 
>       (say_hello (rest names) (cons (first names) saluted)))))
>
> (say_hello ["Peter" "Pablo" "John" "Peter"])
>
> ; Hello Peter !
> ; Hello Pablo !
> ; Hello John !
> ; Welcome Back Peter !
> ; Goodbye
> ; nil
>
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to