Search code examples
clojurefunctional-programming

Mapping a boolean to a list in clojure


I am trying to get a list to evaluate to a true/false against a particular number. When I define the function, clojure defines it fine. But it is not doing what I want to do. Just learning clojure... so I am not very comfortable with the syntax yet... apologies if this is a noob question.

Ideally it would take in a list like (list 9 0 3) and evaluate it to (list true false true)

(defn myfunc [lst](map (> x 1) lst))

Solution

  • Here is the correct syntax:

    (defn greater-than-one?
      [x]
      (< 1 x))
    

    and then use it:

    (mapv greater-than-one? (list 9 0 3)) => [true false true]
    

    In your original format, you could also solve it like:

    (defn myfunc [lst]
      (map #(> % 1) lst))
    

    and use it:

    (myfunc (list 9 0 3)) => (true false true)
    

    You may find this template project helpful in getting started. Please be sure to see the list of documentation also.