Not sure what kind of input you can get, but apply-ing list only on strings 
would decompose them, so your function can be written as:

(defn ls [x]
   (cond
     (string? x)
       (apply list (list x))
     :else
       (apply list x))))

as '(apply list nil)' will yield '().

Or, you can write it more lispy:

(defn ls [x]
  (apply list (if (string? x)
                     (list x)
                      x)))

On Thursday, March 8, 2012 7:28:12 PM UTC+1, Brandon Harvey wrote:
>
> Hi,
>
> I'm seeking a small & idiomatic function that will take input and ensure 
> it's wrapped in a list.  If nil, I'd like the list to be empty.  If the 
> input is already a list, I don't want to wrap it in another list layer.  So:
>
>   "hi"   => ("hi")
>   nil    => ()
>   ("hi") => ("hi")
>
>  (list '("hi"))     => (("hi"))  ; bad
>  (apply list "hi")  => (\h \i)   ; bad
>
> So I've ended up writing the function with a conditional, like so.  Is 
> there a tidier way?
>
> (defn ls [x] (cond (list? x) (apply list x) 
>                    (nil? x)  '() 
>                    :else (list x)))
>
>

-- 
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