lundi 13 janvier 2020

Preventing runtime error if presented with the wrong type

I am currently trying to set up a simple function that reads in a list of integers and returns the square of each integer as follows:

(defn square-seq
  [s]
(if (string? s)
  (s = nil)
  (map #(* % %) s))
)

However i want the code to be able to handle string inputs and just default them to a numerical value to prevent a ClassCastException. Such as if i do

(square-seq ["Hello" 2 3])

Then the return value is:

(1 4 9) or (nil)

Not sure if i am overthinking it or just being dense but i can't figure how to get it to work for the life of me, Any help would be appreciated!

Aucun commentaire:

Enregistrer un commentaire