0
votes

I have created this function make-seq. That if passed a seq list like '(true false) it returns it as is, but if passed a single false it returns (false).

(defn make-seq [data] (if seq? data) data (list data))

For some reason it always evaluates as false the if check.

(make-seq '(true false))

 => ((true false))

(make-seq true)
 => (true)

Not sure why it is doing that as running on the repl it works the test.

 (seq? '(true false))

  => true

Any reason why the defn make-seq logic is not working in the if?

Thanks

3

3 Answers

3
votes

Right now, your make-seq function is basically throwing away the (if seq? data) and data expressions, and only returning the final (list data).

You're missing an open-parens, and a close-parens for balance.

;                         ⌄                            ⌄
(defn make-seq [data] (if (seq? data) data (list data)))

Clojure linting tools like eastwood and kibit can help you detect misuses such as your original version of make-seq where you discard expressions, which could make sense if those expressions had side-effects, but yours don't.

1
votes

The if-statement syntax needed correction:

(defn make-seq [data] (if (seq? data) data (list data)))

(make-seq '(true false))
=> (true false) 

(make-seq true)
=> (true)
0
votes

In addition to the other answers, I would also suggest using indentation. Even for simple expressions like this, it helps the eye to recognize things faster & more reliably:

(defn make-seq
  [data]
  (if (seq? data)
    data
    (list data)))

(make-seq '(true false))     => (true false)
(make-seq   true)            => (true)