I want this function
(defn ret-lowest-str-len
"Computes the lengths of two strings. Returns default length -- len --
if len is <= the length of str-1 and len is <= length of str-2.
Else, returns smaller of length of str-1 and str-2."
[str-1 str-2 len]
(let [l1 (count str-1)
l2 (count str-2)]
(if (and (<= len l1) (<= len l2))
len
(if (< l1 l2)
l1
l2))))
to be able to have two argument signatures. The example shows str-1 str-2 and len (a fixed length). This was done, so that if a string was less than the fixed default of say 15, a length value would be returned that would not cause an overrun exception.
I’d like to be able to pass just str-1 and len without str-2, and I’m not quite sure how to do it.
I’m aware the code will have to change if l2 is not passed in. I’m wondering how to set up the arity. Any examples would be appreciated.
Thanks.
defn has two syntaxes
and
a common pattern is to have the lesser arity version fill in defaults and call into the higher arity case:
on a side note you could write a verry similar function using a variable number of arguments: